Module 4
Module 4
Chapter 1
Recursive Descent Parsing
• A parser that uses collection of recursive procedures
for parsing the given input string is called Recursive
Descent Parser.
$E +id2*id3$ Shift +
$E $ Accept
2. Consider the grammar
STL;
Tint| float
LL,id | id
Perform the shift reduce parsing on the input string
‘‘ int id,id;’’
Shift Reduce Parsing
Stack Input Buffer Parsing Action
$ int id,id;$ Shift
$int id,id;$ Reduce by Tint
$T id,id;$ Shift
First(S)={( a}
First (L)={( a}
First (L’)={, ε}
Follow(S)={$ , )}
Follow(L)={)}
Follow(L’)={)}
3.Consider the following grammar
SiEtS | iEtSeS |a
Eb
Compute first and follow sets
• As the production rule
SiEtS | iEtSeS |a
Needs to be left factored.
Rewrite the grammar as
• SiEtSS’ |a
• S’eS | ε
• Eb
• SiEtSS’ |a
• S’eS | ε
• Eb
First(S)={i,a}
First (S’)={e, ε}
First (E)={b}
Follow(S)={e,$}
Follow(S’)={e,$}
Follow(E)={t}
Problem 4:
Calculate the first and follow set for
the following
EE+T |T
TT*F |F
F(E) |id
Step 1: Remove Left Recursion
EE+T |T
AAα|β
ETE’
E’+TE’|Є
Step 1: Remove Left Recursion
TT*F |F
AAα|β
TFT’
T’*FT’|Є
The right recursive grammar is
E TE’
E’+TE’|Є
TFT’
T’*FT’|Є
F(E) |id
Step 2:
FIRST(E)={ ( id}
FIRST(E’)={ + Є}
FIRST (T)={ ( id}
FIRST(T’)= {* Є}
FIRST (F)={( id}
Step 3:
FOLLOW(E)={ $ ) }
FOLLOW(E’)={ $ )}
FOLLOW (T)={ + $ )}
FOLLOW(T’)= {+ $ )}
FOLLOW (F)={* + $ )}
LL(1) Parsing
Here the 1st L represents that the scanning of the Input will be done
from Left to Right manner and second L shows that in this Parsing
technique we are going to use Left most Derivation Tree and finally
the 1 represents the number of look ahead, means how many symbols
are you going to see when you want to make a decision.
Now, after computing the First and Follow set for each Non-Terminal
symbol we have to construct the Parsing table. In the table Rows will
contain the Non-Terminals and the column will contain the Terminal
Symbols.
All the Null Productions of the Grammars will go under the Follow
elements and the remaining productions will lie under the elements of
First set.
LL(1) Parser
Steps to construct predictive LL(1) Parser table
First(S)={a,b}
First (A)={ε}
First (B)={ε}
Follow(S)={$}
Follow(A)=(a,b)
Follow(B)={a,b}
• Step2: The LL(1) Parsing table is
a b $
S SAaAb SBbBa
A Aε Aε
B B-->ε Bε
First(S)={( a}
First (L)={( a}
First (L’)={, ε}
Follow(S)={$ , )}
Follow(L)={)}
Follow(L’)={)}
• Step2: The LL(1) Parsing table is
( ) a , $
S S(L) Sa
L LSL’ LSL’
L’ L’ε L’,SL’
• As you can see that all the null productions are put under the follow set of that
symbol and all the remaining productions are lie under the first of that symbol.
Since there is no multiple entries , this grammar is LL(1).
3.Show that the following grammar is LL(1) or not
SiEtS | iEtSeS |a
E b
First(S)={i,a}
First (S’)={e, ε}
First (E)={b}
Follow(S)={e,$}
Follow(S’)={e,$}
Follow(E)={t}
• Step2: The LL(1) Parsing table is
i a e b $ t
S SiEtSS’ Sa
S’ S’eS S’ε
S’ε
E Eb
EE+T |T
TT*F |F
F(E) |id
Solution:
FIRST(E)={ ( id}
FIRST(E’)={ + Є}
FIRST (T)={ ( id}
FIRST(T’)= {* Є}
FIRST (F)={( id}
Step 3:
FOLLOW(E)={ $ ) }
FOLLOW(E’)={ $ )}
FOLLOW (T)={ + $ )}
FOLLOW(T’)= {+ $ )}
FOLLOW (F)={* + $ )}
id + * ( ) $
Step 4: LL(1) Table
E E TE’ E TE’
E’ E’+TE’ E’Є E’Є
T TFT’ TFT’
T’ T’Є T’*FT’ T’Є T’Є
F Fid F (E)
Grammar is LL(1)
Problem 4:
Check the following grammar is LL(1) or not
SiCtSE |a
EeS| Є
Cb
Step 1: Free from left recursion and
grammar with no common prefix
Step 2:
FIRST(S)={ i a}
FIRST(E)={ e Є}
FIRST (C)={ b}
Step 3:
FOLLOW(S)= First (E)
={$, e}
FOLLOW(E)=Follow(S)
={$, e}
FOLLOW(C)={ t}
i a e $ b t
S SiCtSE Sa
E EeS EЄ
EЄ
C Cb
Multiple productions
Grammar is not LL(1)
Problem 5:
S (L) | a
L SL’
L’ )SL' | ε
( ) a $
S S(L) Sa
L LSL’ LSL’
L’ L’)SL’
L’Є
Multiple productions
Grammar is not LL(1)
LR PARSING
• The most prevalent type of bottom-up parser today is
based on a concept called LR(k) parsing; the \L" is
for left-to-right scanning of the input, the \R" for
constructing a rightmost derivation in reverse,
and the k for the number of input symbols of
lookahead that are used in making parsing decisions.
Why LR Parsers
✓ Shift
take one of four values:
✓ Reduce
✓ Accept
✓ Error
GOTO Table
❖ The GOTO table specifies which state to put on top of the
S → AB
A→a
B→b
Solution:
S` → •S
S → •AA
A → •aA
A → •b
0 State:
Add Augment production to the I0 State and Compute the Closure
I0 = S` → •S
S → •AA
Add all productions starting with "A" in modified I0 State because "•" is followed by the non-terminal. So, the
I0 State becomes.
I0= S` → •S
S → •AA
A → •aA
A → •b
I1= S` → S•
• S → ABC
• A can get values from S, B and C. B can take
values from S, A, and C. Likewise, C can take
values from S, A, and B.
Example
• Synthesized Attributes:
• Inherited Attributes:
PARSE TREE
Parse tree is the graphical representation of symbol. The
symbol can be terminal or non-terminal.
•In parsing, the string is derived using the start symbol. The root of
the parse tree is that start symbol.
• E E +T Rule 1
• EE-T Rule 2
• ET Rule 3
• TT*F Rule 4
• T->F Rule 5
• Fa Rule 6
• F(E) Rule 7
• Non-terminals are syntactic variables that denote sets of
strings.Symbols on left hand side of the production rule are Non
Terminal Symbols . In the above example E,T,F are Non terminal
Symbols
• Terminals are the basic symbols from which strings are formed.
• In the above example + , * , - , a, ( , ) are terminal symbols
Parse Tree
Example :Considering the following grammar-
E→E+T|T
T→TxF|F
F → ( E ) | id
Features :
• High level specification
• Hides implementation details
• Explicit order of evaluation is not specified
Annotated Parse Tree
• Let us assume an input string 4 * 5 + 6 for computing synthesized
attributes. The annotated parse tree for the input string is :
Dependency graph
1.Design the dependency graph for the following
EE1 +E2
EE1*E2
2. Design the dependency graph for the following
Tnt T.type:=integer
Tfoat T.type:=float
Tchar T.type:=char
Tdouble T.type:=double
ListList1, id List1.in=List.in
Enter_type(id.entry,List.in)
Listid Enter_type(id.entry,List.in)
Dependency graph
Construction of syntax tree for expression
• mknode(op,left,right):
It creates a node with the field operator having operator
as label and two pointers to the left and right.
• mkleaf(id,entry):
It creates an identifier node with label id and a pointer to
symbol tables is given by entry.
• mkleaf(num,value):
It creates node for number with label num and val is
value of that number.
Example 1: Construct the syntax tree for
the expression x*y-5+z