0% found this document useful (0 votes)
56 views22 pages

LL(1) Parsing Techniques Guide

Uploaded by

neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views22 pages

LL(1) Parsing Techniques Guide

Uploaded by

neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Compiler Constructions

Zulfiqar Ali
UIT University
Week 11 – Parsing
• LL(1) Parsing
LL(1) Pasring
• LL(1) Parsing: Here the 1st L represents that the
scanning of the Input will be done from the Left
to Right manner and the second L shows that in
this parsing technique, we are going to use the
Left most Derivation Tree. And finally, the 1
represents the number of look-ahead, which
means how many symbols are you going to see
when you want to make a decision.
Essential conditions for LL(1)
• The grammar is free from left recursion.
• The grammar should not be ambiguous.
• The grammar has to be left factored in so that
the grammar is deterministic grammar.
• Find First() and Follow() of the Grammar
• Creating LL(1) Parsing Table
Algorithm to construct LL(1) Parsing Table
• Step 1: First check all the essential conditions
mentioned above and go to step 2.
• Step 2: Calculate First() and Follow() for all
non-terminals.
• Step 3: Make a parser table.
• Step 4: Illustrate LL(1) Parse Tree
LL(1) Example 1
• Let the grammar
– S' → S$
S → xYzS | a
Y → xYz | y
• This grammar is satisfying conditions, Not-Left Recursive, Left Factored,
we will need to find First() and Follow() and then Parse Table.
First() Follow()
S’ {x, a} {$}
S {x, a} {$}
Y {x, y} {z}

• LL(1) Parsing Table


a x y z $
S’ S' → S$ S' → S$
S S→a S → xYzS
Y Y → xYz Y→y
LL(1) Parsing Example 2
• Consider we have Grammar
– S ->aABb
A ->c | €
B ->d | €
• This grammar is Right Recursive, and not need
to left factorization.
Find its First() and Follow()
First Follow
S a $
A c, ε d, b
B d, ε b

a b c d $
S S->aABb
A A->ε A->c A->ε
B B->ε B->d
a b c d $
S S->aABb
A A->ε A->c A->ε
B B->ε B->d
Example 2 LL(1)
• Consider the Grammar
– S -> (L) | a
L -> SL‘
L' -> )SL' | ε
• Here intial conditions are true, that it is Right
Recursive, Left Factored, we need to find
First() and follow()
First() and Follow()

First Follow
S (,a $, )
L (,a )
L’ ), ε )
Creating LL(1) Parse Table
( ) a $
S S -> (L) S -> a
L L -> SL’ L -> SL’
L’->(SL’
L’
L’->ε

Its Grammar is satisfies all condtions but still its LL(1) parse tree can’t be illustrated
because there multiple Production is same cell.
LL(1) Example 3
• Consider the Grammar
– S --> A | a
A --> a
• Above grammar is satisfying initial conditions
• Here are the first and follow

First Follow
S –> A/a {a} {$}
A –>a {a} {$}
LL(1) Parsing Table

a $
S S –> A, S –> a
A A –> a
LL(1) Example 4
• Consider the Grammar
– E --> TE'
E' --> +TE' | ε
T --> FT'
T' --> *FT' | ε
F --> id | (E)
• This Grammar is satisfying all the condition
• Now Find First() and Follow()
Calculate first() and follow().

First Follow
E –> TE’ { id, ( } { $, ) }
E’ –> +TE’/ ε { +, ε } { $, ) }
T –> FT’ { id, ( } { +, $, ) }
T’ –> *FT’/ ε { *, ε } { +, $, ) }
F –> id/(E) { id, ( } { *, +, $, ) }
Creating Parse Tree

id + * ( ) $
E E –> TE’ E –> TE’
E’ E’ –> +TE’ E’ –> ε E’ –> ε
T T –> FT’ T –> FT’
T’ T’ –> ε T’ –> *FT’ T’ –> ε T’ –> ε
F F –> id F –> (E)
Reference
• Compilers: Principles, Techniques, and Tools, A. V.
Aho, R. Sethi and J. D. Ullman, Addison-Wesley, 2nd
ed., 2006.
– Chapter – 2.3
• https://www.geeksforgeeks.org/removing-direct-
and-indirect-left-recursion-in-a-grammar/
THANK YOU

You might also like