Compiler Design Unit-2
Compiler Design Unit-2
Parser
The process of transforming the data from one format to another is called Parsing. This process
can be accomplished by the parser. The parser is a component of the translator that helps to
organize linear text structure following the set of defined rules which is known as grammar.
Example:-
Top-down Parsing:-
When the parser starts constructing the parse tree from the start symbol and then tries to
transform the start symbol to the input, it is called top-down parsing.
The idea of recursive-descent parsing is extremely simple. We view the grammar rule for a non-
terminal A as a definition for a procedure that will recognize an A. The right hand side of the
grammar rule for A specifies the structure of the code for this procedure , the sequence of
terminals and non-terminal in a choice correspond to matches of the input and calls other
procedure ,while choices correspond to alternatives (switch case or if statement) within the
code.
Ex:-
main()
{
E();
if(l==”$”)
printf (“parsing successful”);
}
E()
{
if(l==”i”)
{
match (“i”);
𝐸′ ();
}
}
𝐸′ ( )
{
if(l==”+”)
{
match (“+”);
match (“i”);
𝐸′ ( );
}
else
return;
}
Algorithms:-
repeat
let X be the top stack symbol and “a” the symbol pointed by pointer
if X is a terminal or $ then
if X=a then
pop X from the stack and move pointer forward
else
error ();
else
if M[X,a]= X→Y1Y2...................... Yk then begin
pop X from the stack
push Yk,Yk-1………………Y1 onto the stack with Y1 on top
output the production X→Y1Y2 ..................... Yk
else
error ();
Until X=$
Constructing LL(1) Parsing Table (Predective Parsing):-
Method:-
First Follow
S→ABCD {b,c} {$}
A→b│ϵ {b,ϵ} {c}
B→c {c} {d}
C→d {d} {e}
D→e {e} {$}
Ex.
Parse the string id+id*id with the grammar below by using LL(1) parsing techniques or
predictive parsing techniques.
E→𝑇𝐸′
𝐸′ → +𝑇𝐸′ │𝜖
T→F𝑇′
𝑇′ →∗ 𝐹𝑇 ′ │𝜖
F→id│(E)
Solution:-
id + * ( ) $
E E→𝑇𝐸′ E→𝑇𝐸′
𝐸′ 𝐸′ → +𝑇𝐸′ 𝐸′ → 𝜖 𝐸′ → 𝜖
T T→F𝑇′ T→F𝑇′
𝑇′ 𝑇′ → 𝜖 𝑇′ →∗ 𝐹𝑇 ′ 𝑇′ → 𝜖 𝑇′ → 𝜖
F F→id F→(E)
Working:-
In Bottom-up parsing we start with sentence and try to apply the production rules in reverse
order to finish up with the start symbol of the grammar. This corresponds to starting at leaves of
the parse tree, and working back to root. Bottom–up parsing is also known as shift-reduce
parsing.
Shift-Reduce Parsing: - Shift-reduce parsing works on two steps: Shift step and Reduce step.
Shift step: The shift step indicates the increment of the input pointer to the next input
symbol that is shifted.
Reduce Step: When the parser has a complete grammar rule on the right-hand side
and replaces it with RHS.
Ex.-
Parse the string abbcde by following grammar using Shift -Reduce technique.
S→aABe
A→Abc│b
B→d
Solution:-
abbcde→aAbcde→aAde→aABe→S
Handle:-
A handle is a substring that matches the right hand side of a production and replacing RHS by
LHS must be step in the reverse rightmost derivation that ultimately leads to the start symbol. If
replacing a string does not ultimately lead to the start symbol it cannot be handle.
Ex.
E→E+E│E*E│id (operator grammar)
Operator-precedence table:-
id + * $
id > > >
+ < > < >
* < > > >
$ < < <
Algorithms:-
set i/p pointer to the first symbol of w$
repeat forever
if $ is on top of the stack and i/p points to $ then
return
else
let a be the topmost terminal symbol on the stack and let b be the symbol
pointed to by i/p pointer
if a<b or a=b then
push b onto the stack advance i/p pointer to the next i/p symbol.
else if a>b then
pop the stack until the top stack terminal is related by < to the terminal
most recently popped
else
error
Ex.
Parse the string id+id*id by the operator precedence parsing techniques.
E→E+E
E→E*E
E→id
Note: - Disadvantages of operator precedence table is there are n2 entries in the precedence
table, where n is no of operator.
Compilers using operator-precedence parser need not store the table of precedence relation. The
table can be encoded by two precedence function f and g that map terminal symbol to integers.
1. f(a)<g(b) whenever a<b
2. f(a)=g(b) whenever a=b
3. f(a)>g(b) whenever a>b
fid g* f+ g+ f$
fid g* f+ g+ f$ f$
Function Table:-
id + * $
f 4 2 4 0
g 5 1 3 0
Note:-
Function table represent the same information which operator precedence table representing
with less storage.
Disadvantages:-
Algorithms:
begin
for each production A→B1B2 ............... Bn
for i=1 to n-1
if Bi and Bi+1 are both terminal then
Bi=Bi+1
if i<=n-2 and Bi and Bi+2 are both terminal and Bi+1 is non-terminal then
Bi=Bi+2
if Bi is terminal and Bi+1 is non-terminal then
for all a in LEADING(Bi+1)
set Bi<a
if Bi is non-terminal and Bi+1 is termnal then
for all a in TRAILING(Bi)
set a>Bi+1
end
Ex.
Compute the leading and trailing for non-terminal E,T,F in following grammar
E→E+T│T
T→T*F│F
F→(E)│id
Leading Trailing
E {+ , * , ( , id } { + , * , ) , id }
T {* , ( , id } { * , ) , id }
F { ( , id } { ) , id }
Question:-
LR Parsing:-
A large class of grammar can be parsed using LR(k) parsers. Here L is for left to right scanning
of the input string. R is for constructing the right most derivation in the reverse and k represents
number of input symbols under look-ahead pointer used to making parsing decisions.
LR(0)Item:-
Item for a grammar G is a production of G with a dot (.) at some position of the right side.
That is if we have a production A→ aBC in grammar G than items of G are
A→ .a BC
A→ a. BC
A→ a B.C
A→ a BC.
Closure operation:-
If I is a set of item for a grammar G, then closure (I) is the set of items constructed from I by the
two rules:
Parser Action:-
1. if Action [sm , ai] =shift s , the parser executes a shift move, entering the configuration
(s0x1s1x2…………….xmsm ai s, ai+1 ................ an$)
I1
𝑆′ → 𝑆.
I5 S→AA.
I2
I0 S→A.A
A
A→.aA│.b
𝑆′ →. 𝑆
S→.AA
A→.aA│.b a
a
I3 A→aA.
A
S→A.A
b A→.aA│.b I6
I
A→.b
Working:-
A
A
a a b b
Ex. Parse the string “aabb” by SLR(1) parsing technique?
S→AA
A→aA│b
I1
𝑆′ → 𝑆.
I5 S→AA.
S
I2
I0
A S→A.A
A→.aA│.b
𝑆′ →. 𝑆
S→.AA
A→.aA│.b
a
a a
I3 A→aA.
A
S→A.A
b A→.aA│.b I6
b
b
I4
A→.b
Action GOTO
A B $ A S
0 S3 S4 2 1
1 Accept
2 S3 S4 5
3 S3 S4 6
4 r3 r3 r3
5 r1
6 r2 r2 r2
Working:-
A
A
a a b b
Parse Tree
Ex. Draw the canonical collection of LR(0) item of the following grammar
E→E+T
E→T
T→T*F
T→F
F→ (E)
F→ id
Solution:-
LR (1) item:-
S→AA
A→aA│b
CLR(1) Table:-
Action Goto
State a b $ S A
0 S3 S4 1 2
1 Accept
2 S6 S7 5
3 S3 S4 8
4 r3 r3
5 r1
6 S6 S7 9
7 r3
8 r2 r2
9 r2
Working:-
A
A
a a b b
Parse Tree
Ex. Parse the string “aabb” by LALR(1) parsing techniques considering the following
grammar.
S→AA
A→aA│b
LALR(1) table:-
Action Goto
State A b $ S A
0 S36 S47 1 2
1 Accept
2 S36 S47 5
36 S36 S47 89
47 r3 r3 r3
5 r1
89 r2 r2 r2
Working:-
RR Conflict:-In a parsing table, if a cell has 2 different reduce moves then reduce-reduce
conflict occurs.
Ex.
S→AaAb
S→BbBa
A→ϵ
B→ϵ
S→AaAb.
𝑺′ → 𝑺.
S→AaA.b
S→A.aAb S→Aa.Ab
A→.
𝑺′ →. 𝑺
S→.AaAb
S→.BaBb
A→.
S→B.aBb
B→.
S→Bb.Ba
B→.
S→BbB.a S→BbBa.