Given the following expression grammar: E -> E * F | F + E | F F -> F - F | id…
2000
Given the following expression grammar:
E -> E * F | F + E | F
F -> F - F | id which of the following is true?
- A.
* has higher precedence than +
- B.
– has higher precedence than *
- C.
+ and — have same precedence
- D.
+ has higher precedence than *
Attempted by 19 students.
Show answer & explanation
Correct answer: B
Given Grammar:
E → E * F | F + E | F
F → F - F | id
Rule for Finding Precedence
In expression grammars:
Operators defined in lower-level non-terminals have higher precedence.
Operators defined in higher-level non-terminals have lower precedence.
Here:
E → E * F | F + E | F
Operators at E level:
*
+
And:
F → F - F | id
Operator at F level:
-
Since F is evaluated before being used in E, the operator - binds more tightly than both * and +.
Example:
a * b - c
The grammar forces:
a * (b - c)
not
(a * b) - c
Therefore, - has higher precedence than *.