Compiler Design · Shift-Reduce and Operator Precedence Parsing
Operator Precedence Parsing and Operator Grammars
Overview
Operator precedence parsing is a bottom-up parsing method used mainly to handle mathematical operators in a compiler.
It works only on a restricted class of grammars called operator grammars, and it drives parsing with an operator relation (precedence) table built over the terminals.
Its distinguishing strength is that it is the only parser that can parse some ambiguous grammars.
Key concepts
- Operator precedence parser: a bottom-up parser that handles mathematical operators and parses input using an operator relation (precedence) table.
- Operator grammar: a grammar that defines mathematical operations and obeys two restrictions:
- No two variables (non-terminals) are adjacent to each other.
- No epsilon () productions.
- Why "no two variables adjacent": in a real language such as C, two identifiers are never written side by side —
ABis not the same as .
Two variables are always separated by an operator, which is a terminal, so the generating grammar must never place two non-terminals adjacent. - Ambiguity is allowed: an operator grammar may be ambiguous, and that is no problem. The operator precedence parser is the only parser that can parse some ambiguous grammars, using the operator relation table.
- Operator relation table: a precedence table over the terminals, always including the end marker
$, used to perform the parsing.
How it works
Checking whether a grammar is an operator grammar:
- Look at every production.
- If any production has two non-terminals side by side (adjacent), the grammar is not an operator grammar.
- If any production is an epsilon production, the grammar is not an operator grammar.
- If neither problem exists, the grammar is an operator grammar.
Converting a non-operator grammar into an operator grammar:
- Find the non-terminal that sits adjacent to another non-terminal.
- Expand (substitute) that intermediate non-terminal's productions into the production where it appears.
- After expansion, terminals separate the non-terminals, so no two variables remain adjacent.
Worked examples
Example 1 — the standard expression grammar (is an operator grammar)
- Grammar:
- In and , the two 's are separated by the terminal
+or*. No two variables are adjacent, and there is no epsilon production. - Conclusion: this is an operator grammar.
Example 2 — the same language with an extra non-terminal (NOT an operator grammar)
- Grammar: ,
- In , the non-terminal is adjacent to the non-terminal (two variables side by side).
- Conclusion: not an operator grammar, even though it generates the same language as Example 1.
- Conversion — expand in place:
- The result is the operator grammar of Example 1.
Example 3 — grammar with and (NOT an operator grammar)
- Grammar:
- In , the non-terminal is adjacent to the non-terminal (in the terminals separate the variable , so only this production violates the rule). Two variables are adjacent.
- Conclusion: not an operator grammar.
- Conversion — expand () inside :
- (putting in place of )
- (putting in place of )
- In the resulting grammar no two variables are adjacent (here is a terminal, so introduces no adjacent variables), so it is an operator grammar.
- This resulting operator grammar is ambiguous, but the operator precedence parser can still parse it using the operator relation table.
Comparison
| Grammar | Form | Operator grammar? | Reason |
|---|---|---|---|
| Example 1 | Yes | Non-terminals separated by terminals + / *; no epsilon production | |
| Example 2 | , | No | adjacent to in |
| Example 3 | , | No | adjacent to |
Revision summary
- Operator precedence parser = bottom-up parser, used mainly for mathematical operators.
- Operator grammar conditions: (1) no two non-terminals adjacent, (2) no epsilon productions.
- Reason for condition (1): in real code two identifiers are never side by side — they are always separated by an operator (terminal).
- To convert a non-operator grammar, expand/substitute the non-terminal that sits adjacent to another non-terminal.
- An operator grammar may be ambiguous; the operator precedence parser is the only parser that can parse some ambiguous grammars.
- Parsing is driven by an operator relation table built over the terminals, with
$always included. - Two grammars can generate the same language while only one of them is an operator grammar (, versus ).
Previous-year exam insights
- An operator grammar must have no production whose right side contains two adjacent non-terminals, and no ε-production (a production of the form A → ε).
Either violation alone disqualifies the grammar.
Asked inGATE 2004 - Operator precedence parsing is a bottom-up (shift-reduce) parsing technique; it is not top-down and does not use backtracking.
Asked inGATE 1987 - Operator precedence relations are defined only between terminals (plus the end marker $): a ⋖ b, a ≐ b, a ⋗ b; the parser uses a stack and repeatedly reduces the handle delimited by ⋖ and ⋗.
- Operator precedence grammars must be unambiguous and must not have ε-productions; the precedence relations must be uniquely defined (no terminal pair may have two different relations), otherwise the table has conflicts.
- Operator precedence parsing cannot handle unary minus or other operators whose precedence/associativity is context-dependent, and it cannot detect all syntax errors precisely (it detects errors only when no relation exists between a terminal pair).
- A grammar with a production of the form A → αBβ where B is a non-terminal adjacent to another non-terminal can often be made an operator grammar by substituting B's productions; ε-productions must be eliminated (e.g., by substitution) before the grammar qualifies.
Asked inGATE 2004