1. Gyanastra
  2. Compiler Design
  3. Shift-Reduce and Operator Precedence Parsing
  4. Operator Precedence Parsing and Operator Grammars
Free lesson, no account needed 6 min read 11 practice questions after it
Asked in past papers4 times
  • GATE CSE20041987
  • ISRO2015
  • UGC NET2014

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:
    1. No two variables (non-terminals) are adjacent to each other.
    2. No epsilon (ε\varepsilon) productions.
  • Why "no two variables adjacent": in a real language such as C, two identifiers are never written side by side — AB is not the same as A×BA \times B.
    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:

  1. Look at every production.
  2. If any production has two non-terminals side by side (adjacent), the grammar is not an operator grammar.
  3. If any production is an epsilon production, the grammar is not an operator grammar.
  4. If neither problem exists, the grammar is an operator grammar.

Converting a non-operator grammar into an operator grammar:

  1. Find the non-terminal that sits adjacent to another non-terminal.
  2. Expand (substitute) that intermediate non-terminal's productions into the production where it appears.
  3. 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: E→E+E∣E∗E∣idE \to E + E \mid E * E \mid id
  • In E→E+EE \to E + E and E→E∗EE \to E * E, the two EE'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: E→E A E∣idE \to E\,A\,E \mid id, A→+∣∗A \to + \mid *
  • In E→E A EE \to E\,A\,E, the non-terminal EE is adjacent to the non-terminal AA (two variables side by side).
  • Conclusion: not an operator grammar, even though it generates the same language as Example 1.
  • Conversion — expand AA in place:
    • E→E+EE \to E + E
    • E→E∗EE \to E * E
    • E→idE \to id
  • The result is the operator grammar of Example 1.

Example 3 — grammar with SS and AA (NOT an operator grammar)

  • Grammar:
    • S→S A S∣aS \to S\,A\,S \mid a
    • A→b S b∣bA \to b\,S\,b \mid b
  • In S→S A SS \to S\,A\,S, the non-terminal SS is adjacent to the non-terminal AA (in A→b S bA \to b\,S\,b the terminals bb separate the variable SS, so only this production violates the rule). Two variables are adjacent.
  • Conclusion: not an operator grammar.
  • Conversion — expand AA (A→b S b∣bA \to b\,S\,b \mid b) inside S→S A SS \to S\,A\,S:
    • S→S b S b SS \to S\,b\,S\,b\,S (putting b S bb\,S\,b in place of AA)
    • S→S b SS \to S\,b\,S (putting bb in place of AA)
    • S→aS \to a
    • A→b S b∣bA \to b\,S\,b \mid b
  • In the resulting grammar no two variables are adjacent (here bb is a terminal, so b S bb\,S\,b 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

GrammarFormOperator grammar?Reason
Example 1E→E+E∣E∗E∣idE \to E + E \mid E * E \mid idYesNon-terminals separated by terminals + / *; no epsilon production
Example 2E→E A E∣idE \to E\,A\,E \mid id, A→+∣∗A \to + \mid *NoEE adjacent to AA in E→E A EE \to E\,A\,E
Example 3S→S A S∣aS \to S\,A\,S \mid a, A→b S b∣bA \to b\,S\,b \mid bNoSS adjacent to AA

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 (E→E A E∣idE \to E\,A\,E \mid id, A→+∣∗A \to + \mid * versus E→E+E∣E∗E∣idE \to E + E \mid E * E \mid id).

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
Pages 1–2 of 2