Given the following expression grammar:
Which of the following is true?
View worked solution
Answer: B
In a context-free grammar representing expressions, the precedence of operators is determined by their position in the derivation hierarchy:
- Operators defined in non-terminals closer to the start symbol (higher in the grammar) have lower precedence.
- Operators defined in non-terminals further from the start symbol (lower in the grammar, closer to the operands) have higher precedence because they group more tightly.
Let's analyze the given grammar:
Here, is the start symbol, which defines the operators and . The non-terminal is lower in the hierarchy (since derives ) and defines the operator .
Since is at a lower level in the grammar than , the operator has higher precedence than both and .
Example: Consider the expression . The parse tree for this expression is:
E
/|\
E * F
| /|\
F F - F
| | |
id id id
In this tree, the subtraction () is grouped as a sub-tree under and must be evaluated before the multiplication () at . This structurally demonstrates that has higher precedence than .
Free practice · Your answers on this page are not saved.
