1. Syntax Analysis
LL Parser
Objectives:
• Be able to understand LL parsing methods
• Be able to find First sets and Follow sets parsing
• Be able to check LL Parsing properties
3. Top-down versus bottom-up
• Top-down parser:
– starts at the root of derivation tree and fills in
– picks a production and tries to match the input
– may require backtracking
– some grammars are backtrack-free (predictive)
• Bottom-up parser:
– starts at the leaves and fills in
– starts in a state valid for legal first tokens
– as input is consumed, changes state to encode possibilities
(recognize valid prefixes)
– uses a stack to store both state and sentential forms
4. Predictive Parsing
• If a top down parser picks the wrong production, it
may need to backtrack
• Alternative is to look ahead in input and use
context to pick correctly
• Fortunately, large classes of CFGs can be parsed
with limited lookahead
• Most programming languages constructs fall in
those subclasses
4
5. Predictive Parsing
• Eliminate left recursion from grammar
• Left factor the grammar
• Compute FIRST and FOLLOW
• Two variants:
– Recursive (recursive-descent parsing)
– Non-recursive (table-driven parsing)
5
6. LL Parsing Methods
• LL parsing methods read the tokens from Left
to right and parse them top-down according
to a Leftmost derivation.
• To build the parsing table, we need the notion
of nullability and the two functions
– FIRST
– FOLLOW
7. Nullability
• A nonterminal A is nullable if
A * .
• Clearly, A is nullable if it has a production
A .
• But A is also nullable if there are, for example,
productions
A BC.
B A | aC | .
C aB | Cb | .
8. Nullability
• In other words, A is nullable if there is a
production
A ,
or there is a production
A B1B2…Bn,
where B1, B2, ..., Bn are nullable.
9. Nullability
• In the grammar
E T E'
E' + T E' | .
T F T'
T' * F T' | .
F (E) | id | num
E' and T' are nullable.
• E, T, and F are not nullable.
10. Nullability
• In the grammar
S → aBDh
B → cC
C → bC / ∈
D → EF
E → g / ∈
F → f / ∈
Which symbol/s is/are null-able?
11. Predictive Parsing
FIRST Sets:
For some rhs a G, define
FIRST(a) as the set of tokens
that appear as the first symbol
in some string that derives
from a.
11
13. FIRST Set
• FIRST(a) = { the set of terminals that begin all
strings derived from a }
FIRST(a) = {a} if a T
FIRST() = {}
FIRST(A) = Aa FIRST(a)
for Aa P
FIRST(X1X2…Xk) =
if for all j = 1, …, i-1 : FIRST(Xj) then
add non- in FIRST(Xi) to FIRST(X1X2…Xk)
if for all j = 1, …, k : FIRST(Xj) then
add to FIRST(X1X2…Xk)
13
14. Example: FIRST
• Let the grammar be
E T E'
E' + T E' | .
T F T'
T' * F T' | .
F (E) | id | num
16. FOLLOW
• FOLLOW(A) = { the set of terminals that can
immediately follow nonterminal A }
FOLLOW(A) =
for all (B a A ) P do
add FIRST()-{} to FOLLOW(A)
for all (B a A ) P and FIRST() do
add FOLLOW(B) to FOLLOW(A)
for all (B a A) P do
add FOLLOW(B) to FOLLOW(A)
if A is the start symbol S then
add $ to FOLLOW(A)
16
17. Find First and Follow sets
CFG
expr expr + term | term
term term * factor | factor
factor number | ( expr )
Reformed
EE+T/T
TT*F/F
F(E)/id
Left-recursion/Left-factoring removed
?
20. LL(1) Grammar
• A grammar G is LL(1) if it is not left recursive and for
each collection of productions
A a1 | a2 | … | an
for nonterminal A the following holds:
1. FIRST(ai) FIRST(aj) = for all i j
2. if ai * then
2.a. aj * for all i j
2.b. FIRST(aj) FOLLOW(A) =
for all i j
20
Sif C then S else S | if C then S
21. Non-LL(1) Examples
21
Grammar Not LL(1) because:
S S a | a Left recursive
S a S | a FIRST(a S) FIRST(a)
S a R |
R S | For R: S * and *
S a R a
R S |
For R:
FIRST(S) FOLLOW(R)
22. Example Table
22
E T ER
ER + T ER |
T F TR
TR * F TR |
F ( E ) | id
A a
FIRST(
a)
FOLLOW(
A)
E T ER ( id $ )
ER + T
ER
+
$ )
ER
T F TR ( id + $ )
TR * F
TR
*
+ $ )
TR
F ( E ) ( * + $ )
F id id * + $ )
23. Example Table
23
E T ER
ER + T ER |
T F TR
TR * F TR |
F ( E ) | id
id + * ( ) $
E E T ER
E T
ER
ER
ER + T
ER
ER ER
T T F TR
T F
TR
TR TR
TR * F
TR
TR TR
F F id
F ( E
)
24. Summary
• Predictive parsing requires
– Removal of Left Recursion
– Left factored CFG
• LL 1 Parsing requires
– First sets
– Follow sets