1. Gyanastra
  2. Compiler Design
  3. Shift-Reduce and Operator Precedence Parsing
  4. Constructing the Operator Precedence Relation Table
Free lesson, no account needed 7 min read 2 practice questions after it

Compiler Design · Shift-Reduce and Operator Precedence Parsing

Constructing the Operator Precedence Relation Table

Overview

The operator precedence relation table (or operator precedence table) is built from a grammar and records, for every ordered pair of terminals (operators), which one has higher precedence.
The operator precedence parser uses this table to parse input.
Even though the grammar is ambiguous and states no precedence, the table explicitly encodes the precedence rules — that is why the parser is called an operator precedence parser.

Key concepts

  • Operator precedence relation table: a table recording, for every ordered pair of terminals, which operator has higher precedence; used by the operator precedence parser.
  • Terminals (operators): id, +, *, $. The end marker $ is always included.
  • Rows and columns: rows are the left operator, columns are the right operator; each cell compares the row operator with the column operator.
  • id precedence: id always has the highest precedence over every other operator.
  • $ precedence: $ always has the least precedence under every other operator.
  • id vs id: never compared, because two identifiers never appear side by side in a valid input.
  • Left associativity: op > op for the same operator means the operator is left associative (the left occurrence executes first) — not that one occurrence has higher precedence than another.
  • * vs +: * has higher precedence than +.
  • $ vs $: marks successful completion of parsing.

How it works

  1. List the terminals (operators): id, +, *, $ (dollar is always included).
  2. Build the table with rows and columns equal to these terminals; rows on the left, columns on the right.
  3. Fill each cell by comparing the two operators:
    • id vs id: left empty / not needed (two identifiers never appear side by side).
    • id vs any other operator: id wins (highest precedence).
    • Any operator vs id: the operator has less precedence than id.
    • + vs +: + is left associative, so the left + gets higher precedence → + > +.
    • + vs *: * has higher precedence → + < *.
    • + vs $: $ has least precedence → + > $.
    • * vs +: * has higher precedence → * > +.
    • * vs *: * is left associative, so the leftmost * gets higher precedence → * > *.
    • * vs $: * has higher precedence → * > $.
    • $ vs +: $ has lower precedence → $ < +.
    • $ vs $: successful termination of the parse.
Figure. Rows are left operators and columns are right operators. The cases fill each cell, including operator vs id, + vs *, left-associative same operators, and $ vs $ termination.

Formulas

  • id > b for all b ≠ id — id has the highest precedence over every other operator.

  • a > $ for all a ≠ $ — $ has the least precedence under every other operator.

  • op > op — for the same left-associative operator op, the left occurrence wins.

  • * > + and + < * — the higher-precedence operator wins over the lower.

  • id vs id is never compared — two identifiers never appear side by side.

  • (Symbols: a, b are terminals; op is a left-associative operator.)

Worked examples

Operator precedence relation table for the grammar with terminals id, +, *, $ (rows = left operator, columns = right operator):

id+*$
id—>>>
+<><>
*<>>>
$<<<✓

Cell-by-cell reasoning:

CellComparisonResultReason
id, idid vs idnot comparedTwo identifiers never come side by side
id, +id vs +id > +id always has highest precedence
+, id+ vs id+ < idid has highest precedence
*, id* vs id* < idid has highest precedence
$, id$ vs id$ < idid has highest precedence
+, ++ vs ++ > ++ is left associative → left + wins
+, *+ vs *+ < ** has higher precedence
+, $+ vs $+ > $$ has least precedence
*, +* vs +* > +* has higher precedence
*, ** vs ** > ** is left associative → leftmost * wins
*, $* vs $* > $$ has least precedence
$, +$ vs +$ < +$ always lower precedence
$, $$ vs $successful terminationEnd of input reached

Reading the precedence from the table: * gets the highest precedence compared to +, and both + and * are left associative, which is why the left-side + and left-side * get higher precedence. The operator precedence parser then uses this table to parse input such as id + id * id.

Revision summary

  • $ is always included in the terminal set of the table.
  • Rows = left operator, columns = right operator.
  • id always has the highest precedence; $ always has the least.
  • id vs id is never compared — two identifiers never appear side by side.
  • op > op means left associativity (left occurrence executes first), not that one occurrence outranks the other.
  • * has higher precedence than +.
  • $ vs $ marks successful completion of parsing.
  • The grammar is ambiguous and states no precedence; the table supplies it — hence the name operator precedence parser.

Previous-year exam insights

  • An operator precedence grammar must be unambiguous, have no empty (ε) productions, and have no production whose right side contains two adjacent nonterminals; otherwise the precedence relations are not uniquely defined.
  • The precedence relations are defined using FIRST and LAST terminals: for a production A → ... a B ... (a terminal, B nonterminal), set a < FIRST(B);
    for A → ... B a ..., set LAST(B) > a;
    for A → ... a b ... with adjacent terminals, set a = b (equal precedence).
  • The ≐ (equal) relation is for terminals that belong together in one handle, such as matching '(' ≐ ')' (and $ ≐ $ at acceptance); it is not used for operators of equal precedence.
    For two operators of equal precedence, a left-associative pair gets ⋗ (reduce first, op ⋗ op) and a right-associative pair gets ⋖ (op ⋖ op).
  • The operator precedence parser uses a stack of terminals/nonterminals plus the input;
    it shifts when the top-of-stack relation is < or =, and reduces (pops a handle) when the relation is >;
    it accepts when the stack is $ and input is $, and errors on a blank entry.
  • Operator precedence parsing cannot handle unary minus or operators with the same symbol used at different precedence levels, and it fails on grammars where two nonterminals can be adjacent; such grammars must be rewritten first.
  • The number of entries in the operator precedence table is n² for n terminals (including $), and the table is not a full parsing table (it does not identify which production to reduce by); a separate handle-finding/precedence-function mechanism is needed.
Pages 1–2 of 2