10-11-12-13-Top Down Parser
10-11-12-13-Top Down Parser
2
Parsing Techniques
• Parsing technique is used to check tokens generated are satisfying the grammatical
rules of the language or not, if they satisfy then corresponding Parse tree is
generated otherwise error is reported to error handler.
• Based on the methodology adopted for the generation of parse tree, parsing
techniques can be categorized in to two categories:
– Top down parser
– Bottom parser
3
Parsing Techniques
4
Parsing Techniques
• Consider the grammar G1 = ({E, F, T}, {a, (,), ∗,+}, P, E) where the productions are:
E→E+T
E→T
T→T∗F
T→F
F → (E)
F→a
and the string x = a + a ∗ a
5
Parsing Techniques
• Top-down method: rules are considered in the same order as a leftmost derivation
E⇒E+T
E⇒T+T
E⇒F+T
E⇒a+T
E⇒a+T∗F
E⇒a+F∗F
E⇒a+a∗F
E⇒a+a∗a
6
Parsing Techniques
• Bottom-up method: rules are considered in the same order as a reverse rightmost
derivation
a+a∗a
⇐F+a∗a
⇐T+a∗a
⇐E+a∗a
⇐E+F∗a
⇐E+T∗a
⇐E+T∗F
⇐E+T
⇐E
7
Top Down Parsing Techniques
8
Top Down Parsing with Backtracking
9
Top Down Parsing with Backtracking
10
Top Down Parsing with Backtracking Algorithm
• SaAd
• Abc|b
11
Top Down Parsing with Backtracking Algorithm
• SaAd Procedure S ()
{
• Abc|b if (input symbol = ‘a’) then
{
advance();
if A() then
{
if (input symbol=‘d’) then
{
advance();
declare success;
}
else error;
}
else error;
}
else error
}
12
Top Down Parsing with Backtracking Algorithm
Procedure A ()
{
• SaAd Procedure S ()
if (input symbol = ‘b’) then
{
• Abc|b if (input symbol = ‘a’) then
{
advance();
{
if (input symbol=‘c’) then
advance();
{
if A() then
advance();
{
declare success;
if (input symbol=‘d’) then
}
{
else error;
advance();
}
declare success;
if (input symbol = ‘b’) then
}
{
else error;
advance();
}
declare success;
else error;
}
}
else error;
else error
}
}
13
Top Down Parsing with Backtracking- Limitations
14
Top Down Parsing without Backtracking
• In this category, once the production rule is applied, it can’t be undone. The parser
which follows top down parsing without backtracking are:
– Recursive Descent Parser
– Predictive Parser
– LL(1) parser
• Need to remove Left recursion and Left factoring from grammar before doing top
down parsing without backtracking.
15
Recursive Descent Parser
• It is defined as a parser that uses various recursive procedures to process the input
string with no backtracking.
• It can be easily implemented using a language which is recursive in nature.
• The first symbol of the string on R.H. S. of a production will uniquely determines the
correct alternative to choose.
• In this technique, there is no need for the procedures to return an indication of
success or failure since the calling procedure has no scope of trying another
alternate. Rather on failure, an error correcting routine is called.
16
Recursive Descent Parser
17
Recursive Descent Parser
18
Recursive Descent Parser
• It is a top down backtracking less parser which generates the parse tree in LL(1)
manner.
• There should be no left recursion and left factoring in the grammar.
23
Components of Predictive Parser
• Input tape: which contains the string to be parsed, only terminals are there in the
input tape. $ symbol is there at the end of the string which indicates that string has
finished.
• Stack: which contains sequence of grammar symbols (terminals and non terminals
both) preceded by $ symbol. $ indicates the bottom of the stack.
• Parsing Table: It is a two dimensional array M[A, a] where A is representing the non
terminal and a is representing the terminal or $ symbol.
• Program: Parser is controlled by a program.
24
How Program controls Parser
• If current input symbol and top stack symbol are $ then parser halts and announce
successful completion of parsing.
• If current input symbol and top stack symbol matches but both are not $ then parser
pop off the top stack symbol and advances the input pointer to next input symbol.
• If top of the stack is Non terminal (X) and present input symbol is ‘a’ then program
consults entry M [X,a] in the parsing table and take the action accordingly.
– If this entry is an error entry then parser calls the error recovery routine.
– If M[X,a]={XUvW} then parser replaces the X on top of the stack by WvU where U will be top.
• Initial configuration of parser is Stack: $S Input: w$
• To fill the entries of parsing table, we use First() and Follow() functions.
25
Steps for finding First
• First(α) represents the set of the terminal symbols which occurs as first
symbol in the strings derived from α where α is any string of grammar
symbols.
– If Xa then First(X)=a
– If Xλ then First(X)= λ
– First of terminal will be that terminal itself
– If Xaα then First(X)=a
– If XY1Y2Y3…Yk then
• First(X)=First(Y1) ;if First(Y1) does not contain λ
• First(X)=First(Y1)-{λ} U First(Y2);if First(Y1) contain λ and First(Y2) does not contain λ
• First(X)=First(Y1)-{λ} U First(Y2)-{λ} U First(Y3);if First(Y1) and First(Y2) contain λ and First(Y3) does not
contain λ
26
Steps for finding Follow
• Follow(A) is the set of the terminals which occur immediately after
(follow) the non-terminal A in the strings derived from the starting
symbol
– $ is in Follow(X) if X is start symbol
– If A αBβ and β≠ λ then
• If first(β) does not contain λ then Follow(B)={First(β)}
• If first(β) contains λ then Follow(B)={First(β)}-{λ} U Follow(A)
– If A αB then Follow(B)={Follow(A)}
27
Numerical
• Find first and follow for the following grammar
– SAaA|BbB
– AbB
– B ɛ
28
Numerical
• Find first and follow for the following grammar
– SAaA|BbB
– AbB
– B ɛ
– First(B)=ɛ
– First(A)=b
– First(S)=b
29
Numerical
• Find first and follow for the following grammar
– SAaA|BbB
– AbB
– B ɛ
– First(B)=ɛ Follow(B)={$, a, b}
– First(A)=b Follow(A)={$, a}
– First(S)=b Follow(S)={$}
30
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id
31
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id
32
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id
– First(E)={(,id}
– First(T)={(,id}
– First(F)={(,id}
33
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id
– First(E)={(,id} Follow(E)={$,+,)}
– First(T)={(,id} Follow(T)={$,+,),*}
– First(F)={(,id} Follow(F)={$,+,),*}
34
Numerical
• Find first and follow for the following grammar
– SL=R
– SR
– L*R
– Lid
– RL
35
Numerical
• Find first and follow for the following grammar
– SL=R
– SR
– L*R
– Lid
– RL
– First(S)={*,id}
– First(L)={*,id}
– First(R)={*,id}
36
Numerical
• Find first and follow for the following grammar
– SL=R
– SR
– L*R
– Lid
– RL
– First(S)={*,id} Follow(S)={$}
– First(L)={*,id} Follow(L)={$,=}
– First(R)={*,id} Follow(R)={$,=}
37
Numerical
• Find first and follow for the following grammar
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
38
Numerical
• Find first and follow for the following grammar
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
– First(E)={(,id}
– First(T)={(,id}
– First(F)={(,id}
– First(E`)={+, ɛ}
– First(T`)={*, ɛ}
39
Numerical
• Find first and follow for the following grammar
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
– First(E)={(,id} Follow(E)={),$}
– First(T)={(,id} Follow(T)={+,),$}
– First(F)={(,id} Follow(F)={+,*,),$}
– First(E`)={+, ɛ} Follow(E`)={),$}
– First(T`)={*, ɛ} Follow(T`)={+,),$}
40
Rules for filling the entries in Parsing Table
• For each production rule A->α in the grammar do the following:
– For each terminal a in First(α) add Aα in M[A, a]
– If ɛ is in First(α), and b is in Follow(A) then add Aα to M[A, b]
– If ɛ is in First(α), and $ is in Follow(A) then add Aα to M[A, $]
– All the remaining entries in the table are error.
41
Algorithm-Predictive Parser
• Input: String to be parsed and parsing table M for Grammar
• Output: Either stack will become empty or it will give an error
• Steps:
– While(Stack is not empty)do
– {
• Let X be top stack symbol and let a be next input symbol
• If (X is terminal or $) then
– If(X=a) then
» Pop X from stack and remove a from the input
– Else
» Error()
• Else
– If M[X,a]=AY1Y2…Yn then
» Pop X and Push YnYn-1…Y1 onto stack
– Else
» Error()
–} 42
Numerical
• Construct predictive parsing table for the following grammar and check whether
string id+id*id is accepted or not.
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
43
Numerical
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
44
Numerical
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
45
Numerical
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id
46
Non INPUT
Terminal
SYMBOLS
Numerical
id + * ( ) $
– ETE` E
– E`+TE`|ɛ E’
– TFT` T
– T`*FT`|ɛ T’
– F(E)|id
F
47
Non INPUT
Terminal
SYMBOLS
Numerical
id + * ( ) $
– E`+TE`|ɛ E’ E +TE’ E’ ε E’ ε
48
Non INPUT
Terminal
SYMBOLS
Numerical
id + * ( ) $
– E`+TE`|ɛ E’ E’ +TE’ E’ ε E’ ε
49
MATCHED STACK INPUT ACTION
E$ id+id * id$
TE’$ id+id * id$ E->TE’
FT’E’$ id+id * id$ T->FT’
– ETE`
id T’E’$ id+id * id$ F->id
– E`+TE`|ɛ
id T’E’$ +id * id$ Match id
– TFT`
id E’$ +id * id$ T’->Є
– T`*FT`|ɛ
id +TE’$ +id * id$ E’-> +TE’
– F(E)|id
id+ TE’$ id * id$ Match +
Non INPUT SYMBOLS
Termin id+ FT’E’$ id * id$ T-> FT’
al
id+ idT’E’$ id * id$ F-> id
id + * ( ) $ id+id T’E’$ * id$ Match id
E E TE’ E TE’ id+id * FT’E’$ * id$ T’-> *FT’
E’ E +TE’ E’ ε E’ ε id+id * FT’E’$ id$ Match *
T T FT’ T FT’
id+id * idT’E’$ id$ F-> id
id+id * id T’E’$ $ Match id
T’ T’ ε T’ *FT’ T’ ε T’ ε
id+id * id E’$ $ T’-> Є
F F id F (E)
id+id * id $ $ E’-> Є 50
LL(1) Grammar
• The algorithm for constructing a predictive parsing table can be applied to any
grammar G to produce a parsing table M.
• For some grammars however, M may have some entries that are multiply-defined.
• If G is left recursive or ambiguous, then M will have at least one multiply-defined
entry.
• A grammar whose parsing table has no multiply-defined entries is said to be LL(1).
51
LL(1) Grammar
• LL(1) grammars have several distinctive properties.
– Parsing table has no multiply-defined entries is said to be LL(1).
– No ambiguous or left recursive grammar can be LL(1).
– A grammar G is LL(1) iff whenever Aα|β are two distinct productions of G, the following
conditions hold :-
• FIRST(α) , FIRST(β) must be disjoint.
52
LL(1) Grammar
G1 = ({E, F, T}, {a, (,), ∗,+}, P, E) where the productions are:
E → E + T|T
T → T ∗ F|F
F → (E)|a
53
LL(1) Grammar
G1 = ({E, F, T}, {a, (,), ∗,+}, P, E) where the productions are:
E → E + T|T
T → T ∗ F|F
F → (E)|a
54
LL(1) Grammar
G2 = ({S, A}, {a, b,c,d}, P, S) where the productions are:
S → cAd
A → ab|a
55
LL(1) Grammar
G2 = ({S, A}, {a, b,c,d}, P, S) where the productions are:
S → cAd
A → ab|a
56
LL(1) Grammar
G3 = ({E, E′, T, T′, F}, {a, ∗, +,(,)}, P, E) with the set of productions P:
E → TE′ T → F T′ F → (E)|a
E′ → +TE′| ε T′ → ∗F T′| ε
57
LL(1) Grammar
G3 = ({E, E′, T, T′, F}, {a, ∗, +,(,)}, P, E) with the set of productions P:
E → TE′ T → F T′ F → (E)|a
E′ → +TE′| ε T′ → ∗F T′| ε
It is LL(1) grammar
58
LL(1) Grammar
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
59
LL(1) Grammar
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
60
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
First (S)={a}
First (B)={b, ε}
61
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
62
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
63
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
64
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
65
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε
It is LL(1) grammar,
now do the parsing of the string abba
66
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
67
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
68
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
69
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
70
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
71
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
72
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)
75
Dr Anurag Jain
Assistant Professor (SG), Virtualization Department, School of Computer Science,
University of Petroleum & Energy Studies, Dehradun - 248 007 (Uttarakhand)
Email: [email protected] , [email protected]
Mobile: (+91) -9729371188