CD Notes
CD Notes
Parser is a compiler that is used to break the data into smaller elements coming from
lexical analysis phase.
A parser takes input in the form of sequence of tokens and produces output in the form
of parse tree.
Types of Parser:
Parser is mainly classified into 2 categories: Top-down Parser, and Bottom-up Parser.
These are explained as following below.
1. Top-down Parser:
Top-down parser is the parser which generates parse for the given input string with the
help of grammar productions by expanding the non-terminals i.e. it starts from the start
symbol and ends on the terminals. It uses left most derivation.
Further Top-down parser is classified into 2 types: Recursive descent parser, and Non-
recursive descent parser.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
It is also known as Brute force parser or the with backtracking parser. It basically
generates the parse tree by using brute force and backtracking.
2. Bottom-up Parser:
Bottom-up Parser is the parser which generates the parse tree for the given input string
with the help of grammar productions by compressing the non-terminals i.e. it starts
from non-terminals and ends on the start symbol. It uses reverse of the right most
derivation.
Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction
till it reaches the root node. Here, we start from a sentence and then apply production
rules in reverse manner in order to reach the start symbol. The image given below
depicts the bottom-up parsers available.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
(ii) LR parser:
LR parser is the bottom-up parser which generates the parse tree for the given string by
using unambiguous grammar. It follows reverse of right most derivation.
LR parser is of 4 types:
(a) LR(0)
(b) SLR(1)
(c) LALR(1)
(d) CLR(1)
It generates the parse tree form given grammar and string but the only condition is two
consecutive non-terminals and epsilon never appear in the right-hand side of any
production.
BOTTOM UP PARSING
o Shift reduce parsing is a process of reducing a string to the start symbol of a grammar.
o Shift reduce parsing uses a stack to hold the grammar and an input tape to hold the string.
o Sift reduce parsing performs the two actions: shift and reduce. That's why it is known as
shift reduces parsing.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
o Working-
(i) Shift-
In a shift action, the next symbol is shifted onto the top of the stack.
(ii) Reduce-
In a reduce action, the handle appearing on the stack top is replaced with the appropriate
non-terminal symbol.
(iii) Accept-
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Solution:
$ id – id * id $ Shift
$ id – id * id $ Reduce E → id
$E – id * id $ Shift
$E– id * id $ Shift
$ E – id * id $ Reduce E → id
$E–E * id $ Shift
$E–E* id $ Shift
$ E – E * id $ Reduce E → id
$E–E*E $ Reduce E → E x E
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
$E–E $ Reduce E → E – E
$E $ Accept
Parse Tree:
E - E
id E * E
id id
Parse the input string (a, (a, a)) using a shift-reduce parser.
Solution:
$ (a,(a,a))$ Shift
$( a,(a,a))$ Shift
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
$(L,S )$ Reduce L → L , S
$(L )$ Shift
$S $ Accept
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Parse Tree:
S
L L
S S S
( a , ( a , a ) )
Parse the input string int id, id; using a shift-reduce parser.
Solution:
$ int id , id ; $ Shift
$T id , id ; $ Shift
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
$ T id , id ; $ Reduce L → id
$TL , id ; $ Shift
$TL, id ; $ Shift
$ T L , id ;$ Reduce L → L , id
$TL ;$ Shift
$TL; $ Reduce S → T L;
$S $ Accept
Parse Tree:
S
T L
Int id , id ;
4. Considering the string “10201”, design a shift-reduce parser for the following
grammar-
S → 0S0 | 1S1 | 2
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
$ 10201$ Shift
$1 0201$ Shift
$10S0 1$ Reduce S → 0 S 0
$1S 1$ Shift
$1S1 $ Reduce S → 1 S 1
$S $ Accept
Parse Tree:
S
1 0 2 0 1
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Example:
S → aABe
A → Abc | b
B→d
Input String: abbcde
$a bbcde Shift
Here, Parser can’t decide whether to shift ‘c’ or to reduce ‘b’, because if ‘b’ is
reduced to A→b then there will be non-terminal at the top of the stack which can’t
further be reduced. Such Conflicts are known as Shift/Reduce Conflicts.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
$a bbcde Shift
Here, Parser can’t decide from which production is to be reduced i.e. from S→b or
A→b. Such Conflicts are known as Reduce/Reduce Conflicts.
RMD: S → aABe
aAde (B→d)
aAbcde (A→Abc)
abbcde (A→b)
Now with the help of this RMD we can do the stack representation.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Parse Tree:
S
a A B e
A b c d
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
o EXAMPLES:
1. Consider the following grammar-
E → EAE | id
A→+|x
Construct the operator precedence parser and parse the string id + id + id.
Solution:
E → E + E | E * E | id
id + * $
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Parse Tree:
E E
id id id + id
Construct the operator precedence parser and parse the string (a, (a, a)).
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Solution:
The terminal symbols in the grammar are { ( , ) , a , , }
We construct the operator precedence table as-
( ) , $
a
Stack Representation:
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Parse Tree:
S
L L
S S S
( a , ( a , a ) )
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
LR parsing is divided into four parts: LR (0) parsing, SLR parsing, CLR parsing and LALR
parsing.
V. LR (0) PARSER
Steps to solve LR (0) Parser are:
(i) Augment the given grammar.
Augmented grammar G` will be generated if we add one more production in the
given grammar G. It helps the parser to identify when to stop the parsing and
announce the acceptance of the input.
(ii) Draw canonical collection of LR (0) items.
An LR (0) item is a production G with dot at some position on the right side of the
production.
LR (0) items is useful to indicate that how much of the input has been scanned up
to a given point in the process of parsing.
In the LR (0), we place the reduce node in the entire row.
(iii)Number the Productions.
(iv) Create the Parsing Table.
The Parsing Table consists of two parts:
1. A Parsing Action Function
The Action Table specifies the actions of the parser (e.g., shift or reduce), for
the given parse state and the next token
✓ Rows are State Names;
✓ Columns are Terminals
❖ The LR Parser determines Sm, the state on top of the stack and ai, the Current
Input symbol.
❖ It then consults Action [Sm, ai] which can take one of four values:
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → AA
A → aA | b
Construct the LR (0) parser and parse the string aabb.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S`→ S
S → AA
A → aA | b
S` → •S
S → •AA
A → •aA
A → •b
S → AA ------------------ (1)
A → aA ------------------ (2)
A→b ------------------ (3)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Explanation:
o I0 on S is going to I1 so write it as 1.
o I0 on A is going to I2 so write it as 2.
o I2 on A is going to I5 so write it as 5.
o I3 on A is going to I6 so write it as 6.
o I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
o I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
o I4, I5 and I6 all states contain the final item because they contain • in the right most end.
So rate the production as production number with R means reduce.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
$0A2 b$ Shift
$0A2b4 $ Reduce A→b
$0A2A5 $ Reduce S→AA
$0S1 $ Accept
A A
a A b
a A
• First Function-
First(α) is a set of terminal symbols that begin in strings derived from α.
➢ Rules for Calculating First Function
▪ Rule-01:
For a production rule X → ∈,
First(X) = {∈}
▪ Rule-02:
For any terminal symbol ‘a’,
First(a) = { a }
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Calculating First(X)
Calculating First(Y2Y3)
Follow(B) = Follow(A)
▪ Rule-03:
For any production rule A → αBβ,
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → aBDh
B → cC
C → bC / ∈
D → EF
E→g/∈
F→f/∈
Solution:
FIRST FOLLOW
S b $
B c {First(D) – ∈} ∪ First(h)={ g , f , h }
C b, ∈ Follow(B) = { g , f , h }
F {f,∈} Follow(D) = { h }
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Solution:
We have: -
The given grammar is left recursive.
So, we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
S→A
A → aBA’
A’ → dA’ / ∈
B→b
C→g
Now, the first and follow functions are as follows-
FIRST FOLLOW
S First(A) = { a } $
A a Follow(S) = { $ }
A’ d, ∈ Follow(A) = { $ }
B b {First(A’) – ∈} ∪ Follow(A)={d , $}
C g Not Available
3. Calculate the first and follow functions for the given grammar-
S → (L) / a
L → SL’
L’ →, SL’ / ∈
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Solution:
The first and follow functions are as follows-
FIRST FOLLOW
S (,a { $ } ∪ { First(L’) – ∈ } ∪
Follow(L) ∪ Follow(L’) = { $ , , , ) }
L (,a )
L’ ,,∈ Follow(L) = { ) }
4. Calculate the first and follow functions for the given grammar-
S → AaAb / BbBa
A→∈
B→∈
Solution:
The first and follow functions are as follows-
FIRST FOLLOW
A ∈ First(a) ∪ First(b) = { a , b }
B ∈ First(b) ∪ First(a) = { a , b }
5. Calculate the first and follow functions for the given grammar-
E→E+T/T
T→T*F/F
F → (E) / id
Solution:
We have: -
The given grammar is left recursive.
So, we first remove left recursion from the given grammar.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
FIRST FOLLOW
E’ +, ∈ Follow(E) = { $ , ) }
T’ *, ∈ Follow(T) = { + , $ , ) }
F ( , id { First(T’) – ∈ } ∪ Follow(T) ∪
Follow(T’) = { * , + , $ , ) }
6. Calculate the first and follow functions for the given grammar-
S → ACB / CbB / Ba
A → da / BC
B→g/∈
C→h/∈
Solution:
The first and follow functions are as follows-
FIRST FOLLOW
S { First(A) – ∈ } ∪ { First(C) – ∈ } ∪ $
First(B) ∪ First(b) ∪ { First(B) – ∈ } ∪
First(a) = { d , g , h , ∈ , b , a }
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → AA
A → aA | b
Construct the SLR (1) parser and parse the string aabb.
Solution:
S`→ S
S → AA
A → aA | b
S` → •S
S → •AA
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → AA ------------------ (1)
A → aA ------------------ (2)
A→b ------------------ (3)
• In the parsing table as like LR (0) parser the entries of SHIFT operation and
NUMBERS will be done as it is but the entries of REDUCE operation will be
done on the basis of FOLLOW of left hand side of that production. Suppose,
the production is B → d so the entry of REDUCE operation will be done in
the FOLLOW (B).
FIRST FOLLOW
S a,b $
A a,b a,b,$
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Explanation:
o I0 on S is going to I1 so write it as 1.
o I0 on A is going to I2 so write it as 2.
o I2 on A is going to I5 so write it as 5.
o I3 on A is going to I6 so write it as 6.
o I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
o I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
o I4, I5 and I6 all states contain the final item because they contain • in the right most end.
So rate the production as production number with R means reduce in the follow of that
particular production.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
A A
a A b
a A
Construct the SLR (1) parser and parse the string id + id * id.
Solution:
S` → E
E→E+T
E→T
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id
E → E + T ------------------ (1)
E→T ------------------ (2)
T → T * F ------------------ (3)
T→F ------------------ (4)
F → id ------------------ (5)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
FIRST FOLLOW
E Id $,+
T Id $,+,*
F Id $,+,*
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
E + T
T T * F
F F id
Id id
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
LR (1) items: LR (1) item is a collection of LR (0) items and a look ahead symbol.
The look ahead is used to determine that where we place the final item.
The look ahead always add $ symbol for the argument production.
E → BB
B → cB | d
E’ → E
E → BB
B → cB | d
Now here always $ will be added in the lookahead of augmented grammar
E’ → .E, $
E → .BB, $ (Here we are opening E so the lookahead of
E → BB will be FIRST(E) i.e. $ which is coming
just after .E in E → .E)
B → .cB | .d, c|d
o EXAMPLES:
1. Consider the given grammar:
S → CC
C → cC | d
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S’ → S
S → CC
C → cC | d
Step (ii): - Create LR (1) items & lookahead and draw Canonical Collection of LR (1) items
S’ → .S, $
S → .CC, $
C → .cC , c|d
C → .d, c|d
I1 I5
S S’ → S., $ S → CC., $
I0 C I6
I2
S’ → .S, $ C S → C.C, $ c C → c.C, $ C
I9
S → .CC, $ C → .cC, $ C → .cC, $
C → cC., $
C → .cC, c|d C → .d, $ C → .d, $
d
C → .d, c|d c I3
C → d., $ I7
C → c.C, c|d
d C → .cC, c|d C
I4 C → cC., c|d I8
C → d., c|d
C → .d, c|d
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Now the STACK IMPLEMENTATION & PARSE TREE are constructed same as LR (0)
and SLR (1) Parser.
o EXAMPLES:
1. Consider the given grammar:
S → CC
C → cC | d
Step (i): - Augment the Given Grammar
S’ → S
S → CC
C → cC | d
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Step (ii): - Create LR (1) items & lookahead and draw Canonical Collection of LR (1) items
S’ → .S, $
S → .CC, $
C → .cC , c|d
C → .d, c|d
I1 I5
S S’ → S., $ S → CC., $
I0 C I6
I2
S’ → .S, $ C S → C.C, $ c C → c.C, $ C
I9
S → .CC, $ C → .cC, $ C → .cC, $
C → cC., $
C → .cC, c|d C → .d, $ C → .d, $
d
C → .d, c|d c I3
C → d., $ I7
C → c.C, c|d
d C → .cC, c|d C
I4 C → cC., c|d I8
C → d., c|d C → .d, c|d
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
I4 R3 R3
I5 R1
I6 S6 S7 9
I7 R3
I8 R2 R2
R2
Now in the above parsing table, we will see in which state the production is same but the
look-ahead is different. So, we get:
I3 + I6 = I36
I4 + I7 = I47
I8 + I9 = I89
Merge the States in the parsing table also, at the place of Shift function and State Name.
No changes on Reduce function. After merging, remove one row between the two similar
rows.
Now update the canonical collection by merging the look-ahead and construct the new
parsing table.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
I1 I5
S S’ → S., $ S → CC., $
I0 C I36
I2 S
I1 I5
S S’ → S., $ S → CC., $
I0 C
I2
S’ → .S, $ C S → C.C, $
S → .CC, $ C → .cC, $
C → .cC, c|d C → .d, $
C → .d, c|d c I36
C → c.C,
d c|d|$
C
I47 C → cC., c|d|$ I89
C → .cC,
C → d., c|d|$ c|d|$
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Now the STACK IMPLEMENTATION & PARSE TREE are constructed same as LR (0)
and SLR (1) Parser.
X. LEFT RECURSION
o A production of grammar is said to have left recursion if the leftmost variable of its
RHS is same as variable of its LHS.
o A grammar containing a production having left recursion is called as Left Recursive
Grammar.
o Elimination of Left Recursion
Left recursion is eliminated by converting the grammar into a right recursive
grammar.
If we have the left-recursive pair of productions-
A → Aα / β
(Left Recursive Grammar)
Then, we can eliminate left recursion by replacing the pair of productions with-
A → βA’
A’ → αA’ / ∈
(Right Recursive Grammar)
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
A → ABd / Aa / a
B → Be / b
Solution:
The grammar after eliminating left recursion is-
A → aA’
A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈
E→E+E/ExE/a
Solution:
The grammar after eliminating left recursion is-
E → aA
A → +EA / xEA / ∈
E→E+T/T
T→TxF/F
F → id
Solution:
The grammar after eliminating left recursion is-
E → TE’
E’ → +TE’ / ∈
T → FT’
T’ → xFT’ / ∈
F → id
S → (L) / a
L → L, S / S
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → (L) / a
L → SL’
L’ →, SL’ / ∈
S → S0S1S / 01
Solution:
The grammar after eliminating left recursion is-
S → 01A
A → 0S1SA / ∈
The grammar obtained after the process of left factoring is called as Left Factored
Grammar.
o EXAMPLES:
1. Do left factoring in the following grammar-
S → iEtS / iEtSeS / a
E→b
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
S → iEtSS’ / a
S’ → eS / ∈
E→b
2. Do left factoring in the following grammar-
Step-02:
A → aA’
A’ → AD / Bc
D→B/c
Step-02:
S → bSS’ / a
S’ → SaA / b
A → aS / Sb
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
o In LL1, first L stands for Left to Right and second L stands for Left-most Derivation.
1 stands for number of Look-ahead token used by parser while parsing a sentence.
o LL (1) parsing is constructed from the grammar which is free from left recursion,
common prefix, and ambiguity.
o LL (1) parser depends on 1 look ahead symbol to predict the production to expand
the parse tree.
o This parser is Non-Recursive.
o Steps to Parse a string using LL (1) or Predictive parser:
(i) Eliminate Left Recursion & Left Factoring.
(ii) Calculate FIRST & FOLLOW of the given grammar.
(iii) Construct the parsing table.
(iv) Stack Implementation
(v) Construct the Parse Tree.
o EXAMPLES:
1. Consider a given grammar:
E → E+T | T
T → T+F | F
F → (E) | id
Parse the string id + id * id using LL (1) parser
Solution:
Step (i): Eliminate Left Recursion
E → TE'
E' → +TE' | ε
T → FT'
T' → *FT' | ε
F → id | (E)
Step (ii): Calculate First & Follow
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
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)
E$ id + id * id $ E → TE’
TE’$ id + id * id $ T → FT’
FT’E’$ id + id * id $ F → id
id T’E’$ id + id * id $ T’ → ε
+TE’$ + id * id $ T → FT’
FT’E’$ id * id $ F → id
*FT’E’$ * id$ F → id
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
idT’E’ $ id$ T’ → ε
E’$ $ E’ → ε
$ $ Accept
T E’
F T’ + T E’
id ε F T’ ε
id * F T’
id ε
Solution:
Step (i): Eliminate Left Recursion
S → iEtSS’ | a
S’→ Es | ε
E→b
Step (ii): Calculate First & Follow
S’→ Es | ε e, ε e, $
E→b
b t
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
a b e i t $
S S→a S → iEtSS’
S’ S → Es S' → ε
S' → ε
E E→b
Since there are two entries in the single block of parsing table so the above grammar
is not LL (1) grammar.
EXAMPLES:
1. Consider the grammar G
S → cAd
A→ ab | a
Construct a parse tree for the input string w=cad using Recursive Descent Parser.
Input pointer
Step (i): Elaborate the start symbol
S
c A d
descent pointer
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Input pointer
Step (ii): Now move descent pointer also and check whether there is terminal or non-
terminal. If there is non-terminal, then elaborate.
S
c A d
a b
descent pointer
Now compare the input pointer & descent pointer. If both are matching, then move
forward.
w = cad
Input pointer
c A d
a b
descent pointer
Now here input pointer & descent pointer are not matching. So, backtracking will be
used.
Step (iii): Backtrack and use another production
w = cad
Input pointer
S
c A d
descent pointer
Now compare input pointer & descent pointer. So, it matched now move forward.
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida
Input pointer
S
c A d
a
descent pointer
Yes, the given string can be parsed and the final parse tree is shown below:
c A d
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida