Chapter 2

Grammar preparation and predictive parsing

Predictive parsing chooses a production before recognising its body. Compute nullable, FIRST and FOLLOW information to a fixed point before judging a grammar or filling its parsing table.

2.1 Parse structure, precedence and ambiguity

Past papers · CS 2014–2026 (23 papers): 6 questions, 10 marks

Write a context-free grammar as G=(V,Σ,P,S)G=(V,\Sigma,P,S): nonterminals VV, terminals Σ\Sigma, productions PP and start symbol SS. A parse tree includes grammar structure and punctuation; an abstract syntax tree (AST) keeps meaningful operators and operands. Its edges describe structure, not possible execution transfers.

A leftmost derivation always expands the leftmost nonterminal. A rightmost derivation expands the rightmost one. LL parsing scans left to right and constructs a leftmost derivation; LR parsing constructs a rightmost derivation in reverse. Two distinct parse trees for one terminal string prove ambiguity. Two arbitrary derivation orders alone do not: independent expansions can lead to the same tree.

Grammar shapeGrouping encoded, when other alternatives are suitably separated
E→E−T∣TE\to E-T\mid TRepeated subtraction groups left
E→T−E∣TE\to T-E\mid TRepeated subtraction groups right
E→E+T∣TE\to E+T\mid T, T→T∗F∣FT\to T*F\mid FMultiplication is deeper and binds more tightly
E→E+E∣E∗E∣idE\to E+E\mid E*E\mid idDoes not by itself fix precedence or associativity

Precedence chooses which operator binds first; associativity resolves repeated operators at the same level. Always use a question's table, even when it reverses normal arithmetic conventions. For a parse tree, identify each operator's operand subtrees and reconstruct parentheses.

A dangling else grammar is ambiguous if the else can attach to either unmatched if. A parser's “shift on else” convention attaches it to the nearest unmatched if; this is a disambiguation policy, not a proof that the written grammar is unambiguous. To repair the grammar, distinguish fully matched statements from statements with an unmatched if and check both categories recursively.

2.2 Left recursion and left factoring

Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks

A grammar is left recursive if some nonterminal AA derives AαA\alpha in one or more steps. Direct recursion appears in one rule; indirect recursion can pass through several nonterminals. Nullable prefixes can hide recursion, so inspect what can become the leftmost nonterminal, not just the first printed symbol.

For ordinary grammars without troublesome nullable cycles, order the nonterminals; substitute earlier nonterminals' alternatives into later rules before removing immediate recursion. More generally, inspect nullable left-corner reachability to find hidden cycles. Removing recursion preserves the terminal language but does not automatically preserve semantic-action placement or an expression's evaluation order.

2.3 Nullable, FIRST and FOLLOW

Past papers · CS 2014–2026 (23 papers): 4 questions, 6 marks

A symbol/string is nullable if it derives ϵ\epsilon. FIRST of a string contains its possible first terminals, plus ϵ\epsilon if the whole string is nullable. FOLLOW of a nonterminal contains terminals that can immediately follow it in a sentential form, and the end marker when it can end a sentence.

2.4 LL(1) table and predictive execution

Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks

The two Ls mean left-to-right scanning and leftmost derivation; 1 is one lookahead token. For each nonterminal and lookahead, a predictive parser must have at most one production choice. It uses no backtracking.

To execute a table-driven parser, start with the end marker below SS. A terminal at stack top must match the lookahead. A nonterminal is replaced using MM; push the body in reverse order so its first symbol is processed first. An ϵ\epsilon body pushes nothing and consumes no input. Accept only when both input and stack reach their end markers.

In recursive descent, a procedure represents a nonterminal; a predictive implementation chooses a production using lookahead. Left recursion makes naive recursive descent recurse without consuming input. Left factoring and removal of left recursion are preparation steps, not sufficient tests for LL(1). Conflicts can remain through nullable alternatives or overlapping FIRST sets.

Panic-mode recovery can discard tokens until a synchronising symbol is reached. FOLLOW sets are common synchronising candidates for nonterminals. Recovery policy is separate from the grammar's acceptance rule and may skip otherwise meaningful input.

Last-minute recall

  • Ambiguity requires two trees for the same sentence.
  • Read precedence from grouping, not the operator's familiar meaning.
  • Left factoring delays a decision; it does not prove LL(1).
  • FIRST may contain epsilon; FOLLOW never does.
  • Nullable alternatives use FOLLOW in the predictive table.
  • Every nullable prefix matters in FIRST and FOLLOW computations.
  • An epsilon expansion consumes no input.

Revise offline: the whole Compiler Design handbook as one PDF

43 pages · 2.0 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections