Sub topic 15
Operator Precedence Parsing, Error Recovery, and Attributes
Assume you know context-free grammars, stacks, parse trees, and basic shift-reduce parsing. This chapter connects operator precedence decisions with practical error handling and semantic evaluation.
Operator precedence parsing
Operator precedence parsing is a bottom-up method for expression grammars. It uses precedence relations between terminals to decide whether to shift the next input symbol or reduce a handle on the stack.
For terminals a and b, the usual relations are:
| Relation | Meaning |
|---|---|
| `a ⋖ b` | Shift `b`; `b` starts a handle |
| `a ⋗ b` | Reduce; the handle ends before `b` |
| `a ≐ b` | The symbols belong to the same handle boundary |
The parser keeps terminals and grammar symbols on a stack, but precedence comparisons use the nearest terminal on the stack. It shifts when the stack terminal has lower precedence than the next input terminal. It reduces when the stack terminal has higher precedence.
For arithmetic expressions, multiplication should bind more tightly than addition. Thus, when the stack ends in + and the next input operator is *, the parser shifts. When the stack ends in * and the next input operator is +, it reduces the multiplication first.
An operator-precedence grammar generally has no adjacent nonterminals on the right side of a production and does not use epsilon productions. These restrictions make terminal relationships useful, but they also limit the grammars the method can parse.
Syntax error detection and recovery
A syntax error occurs when the parser cannot find a valid precedence relation, table entry, or reduction. For example, two operands in a row may produce an undefined relation.
A simple recovery strategy is panic-mode recovery:
- Report the unexpected token.
- Discard input until a synchronizing token such as
;,), or$appears. - Pop stack symbols until parsing can continue.
- Resume from the synchronizing point.
Recovery may skip some valid input, but it prevents one error from stopping the entire compilation. A parser should also avoid repeatedly reporting the same error without consuming input.
Syntax-directed definitions
A syntax-directed definition, or SDD, attaches attributes and semantic rules to grammar productions. An attribute is information associated with a grammar symbol.
A synthesized attribute is computed from children and moves upward. An inherited attribute is obtained from a parent or a sibling and moves downward or sideways.
For E → E1 + T, a synthesized value rule might be:
E.val = E1.val + T.val
An SDD using only synthesized attributes is S-attributed. An L-attributed definition permits inherited attributes, provided each inherited attribute depends only on the parent and symbols to its left.
The parse tree determines the dependencies. Evaluation is valid only when every attribute is computed after the attributes it needs.
Exercise
In an expression parser, should 2 + 3 * 4 reduce the multiplication before the addition, and what kind of attribute rule can compute the final value?
Show answer
Yes. Multiplication has higher precedence, so the parser reduces 3 * 4 first. A synthesized rule such as E.val = E1.val + T.val can compute the value bottom-up.