
(in simple language with definitions, steps, diagrams, table construction, and key exam points)
(in simple language with definitions, steps, diagrams, table construction, and key exam points)
An LL(1) grammar is a grammar that can be parsed:
π So, LL(1) means:
βPredict the correct production using only 1 input symbol lookahead.β
LL(1) parser is a top-down predictive parser with no backtracking.
A grammar is LL(1) if:
β Bad:
E β E + T | T
β Fixed:
E β T E'
β Bad:
A β ab | ac
β Fixed:
A β aA'
A' β b | c
For:
A β Ξ± | Ξ²
Grammar is LL(1) if:
FIRST(Ξ±) β© FIRST(Ξ²) = β
FIRST(Ξ±) β© FOLLOW(A) = β
π No ambiguity + no overlap in FIRST/FOLLOW = LL(1)
Predictive parsing is a top-down parsing method that:
A Non-Recursive Predictive Parser uses:
π It does NOT use recursion (unlike recursive descent).
Input Buffer β [Stack + Parsing Table] β Output
$S$Initialize stack:
$ S
Read input string with $
Repeat:
E β T E'
E' β + T E' | Ξ΅
T β id
FIRST(E) = {id}
FIRST(E') = {+, Ξ΅}
FIRST(T) = {id}
FOLLOW(E) = {$}
FOLLOW(E') = {$}
FOLLOW(T) = {+, $}
| Non-Terminal | id | + | $ |
|---|---|---|---|
| E | E β T E' | ||
| E' | E' β + T E' | E' β Ξ΅ | |
| T | T β id |
Input:
id + id $
| Stack | Input | Action |
|---|---|---|
| $E | id+id$ | E β T E' |
| $E'T | id+id$ | T β id |
| $E'T | id+id$ | match id |
| $E' | +id$ | E' β +T E' |
| $E'T+ | +id$ | match + |
| ... | ... | continues |
β Accept when both reach $
β No backtracking β Fast parsing β Easy implementation β Deterministic
β Cannot handle left recursion β Cannot handle ambiguous grammar β Requires grammar transformation
π Very frequently asked:
| Feature | LL(1) Grammar | Predictive Parsing |
|---|---|---|
| Meaning | Grammar type | Parsing technique |
| Lookahead | 1 symbol | 1 symbol |
| Method | Rules | Stack + table |
| Backtracking | No | No |
| Requirement | FIRST & FOLLOW | Parsing table |
| Efficiency | High | High |
| Use | Grammar design | Syntax analysis |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.