Sub topic 2
Ambiguity and how to resolve it
This chapter assumes you know context-free grammars, derivations, parse trees, and basic set notation. It covers how grammars become ambiguous, how to rewrite them so a parser can use them, and how LL(1) and LR(0) parsers read a string.
Ambiguity
A grammar is ambiguous when some string has two or more parse trees. Take E -> E + E | id. The string id + id + id has two trees, one grouping it as (id + id) + id and one as id + (id + id). A parser needs one tree, so the grammar must change or the ambiguity must be resolved by precedence and associativity rules.
Removing left recursion and left factoring
A rule is left-recursive when it starts with its own symbol, as in A -> A α | β. Top-down parsers loop forever on such rules. Replace them with:
A -> β A'A' -> α A' | ε
Left factoring removes common prefixes. S -> if E then S | if E then S else S becomes S -> if E then S S' and S' -> else S | ε.
Computing FIRST and FOLLOW
FIRST(X) is the set of terminals that can begin a string derived from X. FOLLOW(A) is the set of terminals that can appear immediately after A. Add $ to FOLLOW of the start symbol.
LL(1) grammars and predictive parsing tables
Take the grammar E -> T E', E' -> + T E' | ε, T -> id. Then FIRST(E) = {id}, FIRST(E') = {+, ε}, and FOLLOW(E') = {$}. The LL(1) table has one entry per cell:
| Nonterminal | id | + | $ |
|---|---|---|---|
| E | E -> T E' | ||
| E' | E' -> + T E' | E' -> ε | |
| T | T -> id |
A grammar is LL(1) only if no cell holds two productions.
Recursive descent parsers
A recursive descent parser writes one function per nonterminal. This version checks id + id:
tokens = ["id", "+", "id", "$"]
pos = 0
def match(t):
global pos
if tokens[pos] != t:
raise SyntaxError(f"expected {t}, found {tokens[pos]}")
pos += 1
def E(): # E -> T E'
T()
Ep()
def Ep(): # E' -> + T E' | epsilon
if tokens[pos] == "+":
match("+")
T()
Ep()
def T(): # T -> id
match("id")
E()
print("accepted" if tokens[pos] == "$" else "rejected")Shift-reduce parsing and handles
A shift-reduce parser keeps a stack and reads input left to right. It either shifts the next token onto the stack or reduces the top of the stack by a production. The handle is the substring that matches the right side of the production being reduced. Using E -> E + T | T and T -> id, the input id + id proceeds as follows:
| Stack | Input | Action |
|---|---|---|
| $ | id + id $ | shift |
| $ id | + id $ | reduce T -> id (handle: id) |
| $ T | + id $ | reduce E -> T (handle: T) |
| $ E | + id $ | shift |
| $ E + | id $ | shift |
| $ E + id | $ | reduce T -> id (handle: id) |
| $ E + T | $ | reduce E -> E + T (handle: E + T) |
| $ E | $ | accept |
LR(0) item sets and SLR(1) tables
An LR(0) item is a production with a dot marking how much has been seen. The augmented grammar adds E' -> E. The canonical collection of item sets forms the states of the parser:
flowchart TD
I0["I0: E' -> .E, E -> .E+T, E -> .T, T -> .id"]
I1["I1: E' -> E., E -> E.+T"]
I2["I2: E -> T."]
I3["I3: T -> id."]
I4["I4: E -> E+.T, T -> .id"]
I5["I5: E -> E+T."]
I0 -->|E| I1
I0 -->|T| I2
I0 -->|id| I3
I1 -->|+| I4
I4 -->|T| I5
I4 -->|id| I3caption: LR(0) states for the grammar E -> E + T | T, T -> id.
alt: A flowchart with six states. I0 leads to I1 on E, to I2 on T, and to I3 on id. I1 leads to I4 on plus. I4 leads to I5 on T and back to I3 on id.
SLR(1) uses FOLLOW sets to decide reductions. Here FOLLOW(T) = {+, $}, so state I2 reduces E -> T only on + and $. A conflict occurs when a state needs both a shift and a reduce on the same lookahead, or two reductions on the same lookahead.
Exercise
Is the grammar S -> a S | a LL(1)?
Show answer
No. FIRST(a S) and FIRST(a) both contain a, so the table cell M[S, a] would hold two productions. Left factoring gives S -> a S' and S' -> S | ε, which is LL(1).