RkCD-Chapter 4 - Syntax Analysis
RkCD-Chapter 4 - Syntax Analysis
Bottom Up Parsing:
Bottom-up parser builds a derivation by working from the input sentence back towards the start symbol
S. Right most derivation in reverse order is done in bottom-up parsing.
S→r0→r1→r2→- - - →rn-1→rn→sentence
<<<<<<--------------------------------------------
Assuming the production, A→β, to reduce ri ri-1 match some RHS β against ri then replace β
with its corresponding LHS, A.
In terms of the parse tree, this is working from leaves to root. Bottom up parsing is also known as
shift-reduce parsing. Bottom up parsing is used to construct a parse tree for an input string. In the
bottom up parsing, the parsing starts with the input symbol and construct the parse tree up to the
start symbol by tracing out the rightmost derivations of string in reverse.
Production
• E→T
• T→T*F
• T → id
• F→T
• F → id
Parse Tree representation of input string "id * id" is as follows:
• Sift reduce parsing performs the two actions: shift and reduce. That's why it is known as shift
reduces parsing.
• At the shift action, the current symbol in the input string is pushed to a stack.
• At each reduction, the symbols will replaced by the non-terminals. The symbol is the right side
of the production and non-terminal is the left side of the production.
Example:
Grammar:
• S → S+S
• S → S-S
• S → (S)
• S→a
Input string:
a1-(a2+a3)
Parsing table:
Parsing Action
• Both end of the given input string, add the $ symbol.
• Now scan the input string from left right until the ⋗ is encountered.
• Scan towards left over all the equal precedence until the first left most ⋖ is encountered.
• Everything between left most ⋖ and right most ⋗ is a handle.
• $ on $ means parsing is successful.
Example
Grammar:
1.E → E+E/E*E/id
Given string: w = id + id * id
On the basis of above tree, we can design following operator precedence table:
id + * $
$E $ Accept
3. LR Parser:
LR parsing is one type of bottom up parsing. It is used to parse the large class of grammars.
In the LR parsing, "L" stands for left-to-right scanning of the input.
"R" stands for constructing a right most derivation in reverse.
"K" is the number of input symbols of the look ahead used to make number of parsing decision.
LR parsing is divided into four parts: LR (0) parsing, SLR parsing, CLR parsing and LALR
parsing.
LR Parser Algorithm:
The LR algorithm requires stack, input, output and parsing table. In all type of LR parsing, input,
output and stack are same but parsing table is different.
1 $0 aabb$ Shift a3
7 $0A2 b$ Shift b4
10 $0S1 $ Accept
Explanation:
• I0 on S is going to I1 so write it as 1.
• I0 on A is going to I2 so write it as 2.
• I2 on A is going to I5 so write it as 5.
• I3 on A is going to I6 so write it as 6.
• I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
• I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
• 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.
Productions are numbered as follows:
1. S → AA ... (1)
2. A → aA ... (2)
3. A → b ... (3)
• I1 contains the final item which drives(S` → S•), so action {I1, $} = Accept.
• I4 contains the final item which drives A → b• and that production corresponds to the production number
3 so write it as r3 in the entire row.
• I5 contains the final item which drives S → AA• and that production corresponds to the production
number 1 so write it as r1 in the entire row.
• I6 contains the final item which drives A → aA• and that production corresponds to the production
number 2 so write it as r2 in the entire row.
Parsing the Input String:
3.b. SLR (1) Parsing:
SLR (1) refers to simple LR Parsing. It is same as LR(0) parsing. The only difference is in the parsing
table. To construct SLR (1) parsing table, we use canonical collection of LR (0) item.
In the SLR (1) parsing, we place the reduce move only in the follow of left hand side.
Various steps involved in the SLR (1) Parsing:
I. For the given input string write a context free grammar
II. Check the ambiguity of the grammar
III. Add Augment production in the given grammar
IV. Create Canonical collection of LR (0) items
V. Draw a data flow diagram (DFA)
VI. Construct a SLR (1) parsing table
SLR (1) Table Construction:
The steps which use to construct SLR (1) Table is given below:
If a state (Ii) is going to some other state (Ij) on a terminal, then it corresponds to a shift move in the
action part.
If a state (Ii) is going to some other state (Ij) on a variable then it correspond to go to move in the Go to
part.
If a state (Ii) contains the final item like A → ab• which has no transitions to the next state then the
production is known as reduce production. For all terminals X in FOLLOW (A), write the reduce
entry along with their production numbers.
Example
1. S -> •Aa
2. A->αβ•
1. Follow(S) = {$}
2. Follow (A) = {a}
SLR ( 1 ) Grammar
S→E
E→E+T|T
T→T*F|F
F → id
III. Add Augment production in the given grammar:
Add Augment Production and insert '•' symbol at the first position for every production in G
S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id
IV. Create Canonical collection of LR (0) items:
I0 State:
Add Augment production to the I0 State and Compute the Closure
I0 = Closure (S` → •E)
Add all productions starting with E in to I0 State because "." is followed by the non-terminal. So,
the I0 State becomes
I0 = S` → •E
E → •E + T
E → •T
Add all productions starting with T and F in modified I0 State because "." is followed by the non-
terminal. So, the I0 State becomes.
I0= S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id
I1= Go to (I0, E) = closure (S` → E•, E → E• + T)
I2=Go to (I0, T) = closure (E → T•T, T• → * F)
I3= Go to (I0, F) = Closure ( T → F• ) = T → F•
I4= Go to (I0, id) = closure ( F → id•) = F → id•
I5= Go to (I1, +) = Closure (E → E +•T)
Add all productions starting with T and F in I5 State because "." is followed by the non-terminal.
So, the I5 State becomes
I5 = E → E +•T
T → •T * F
T → •F
F → •id
Go to (I5, F) = Closure (T → F•) = (same as I3)
Go to (I5, id) = Closure (F → id•) = (same as I4)
I6= Go to (I2, *) = Closure (T → T * •F)
Add all productions starting with F in I6 State because "." is followed by the non-terminal. So,
the I6 State becomes
I6 = T → T * •F
F → •id
Go to (I6, id) = Closure (F → id•) = (same as I4)
I7= Go to (I5, T) = Closure (E → E + T•) = E → E + T•
I8= Go to (I6, F) = Closure (T → T * F•) = T → T * F• V.
Drawing DFA:
$0 id*id+id$ Shift 4
$0E1 $ Accepted
Explanation:
First (E) = First (E + T) First (T)
First (T) = First (T * F) First (F)
First (F) = {id}
First (T) = {id}
First (E) = {id}
Follow (E) = First (+T) {$} = {+, $}
Follow (T) = First (*F) First (F)
= {*, +, $}
Follow (F) = {*, +, $}
• I1 contains the final item which drives S → E• and follow (S) = {$}, so action {I1, $} = Accept
• I2 contains the final item which drives E → T• and follow (E) = {+, $}, so action {I2, +} = R2,
action {I2, $} = R2
• I3 contains the final item which drives T → F• and follow (T) = {+, *, $}, so action {I3, +} = R4,
action {I3, *} = R4, action {I3, $} = R4 • I4 contains the final item which drives F → id•
and follow (F) = {+, *, $}, so action {I4, +} = R5, action {I4, *} = R5, action {I4, $} = R5
• I7 contains the final item which drives E → E + T• and follow (E) = {+, $}, so action {I7, +} =
R1, action {I7, $} = R1
• I8 contains the final item which drives T → T * F• and follow (T) = {+, *, $}, so action {I8, +} =
R3, action {I8, *} = R3, action {I8, $} = R3.
3.c. CLR (1) Parsing:
CLR refers to canonical lookahead. CLR parsing use the canonical collection of LR (1) items to
build the CLR (1) parsing table. CLR (1) parsing table produces the more number of states as
compare to the SLR (1) parsing.
In the CLR (1), we place the reduce node only in the lookahead symbols.
Various steps involved in the CLR (1) Parsing:
I. For the given input string write a context free grammar
II. Check the ambiguity of the grammar
III. Add Augment production in the given grammar
IV. Create Canonical collection of LR (0) items
V. Draw a data flow diagram (DFA)
VI. Construct a CLR (1) parsing table
LR (1) item
LR (1) item is a collection of LR (0) items and a look ahead symbol.
LR (1) item = LR (0) item + look ahead
The look ahead is used to determine that where we place the final item. The
look ahead always add $ symbol for the argument production.
Example
CLR ( 1 ) Grammar
1. S → AA
2. A → aA
3. A → b
Add Augment Production, insert '•' symbol at the first position for every production in G and also
add the lookahead.
1. S` → •S, $
2. S → •AA, $
3. A → •aA, a/b
4. A → •b, a/b I0 State:
Add Augment production to the I0 State and Compute the Closure
I0 = Closure (S` → •S)
Add all productions starting with S in to I0 State because "." is followed by the non-terminal. So,
the I0 State becomes
I0 = S` → •S, $
S → •AA, $
Add all productions starting with A in modified I0 State because "." is followed by the
nonterminal. So, the I0 State becomes.
I0= S` → •S, $
S → •AA, $
A → •aA, a/b
A → •b, a/b
I1= Go to (I0, S) = closure (S` → S•, $) = S` → S•, $
I2= Go to (I0, A) = closure ( S → A•A, $ )
Add all productions starting with A in I2 State because "." is followed by the non-terminal. So,
the I2 State becomes
I2= S → A•A, $
A → •aA, $
A → •b, $
I3= Go to (I0, a) = Closure ( A → a•A, a/b )
Add all productions starting with A in I3 State because "." is followed by the non-terminal. So,
the I3 State becomes
I3= A → a•A, a/b
A → •aA, a/b
A → •b, a/b
Go to (I3, a) = Closure (A → a•A, a/b) = (same as I3) Go
to (I3, b) = Closure (A → b•, a/b) = (same as I4)
I4= Go to (I0, b) = closure ( A → b•, a/b) = A → b•, a/b
I5= Go to (I2, A) = Closure (S → AA•, $) =S → AA•, $
I6= Go to (I2, a) = Closure (A → a•A, $)
Add all productions starting with A in I6 State because "." is followed by the non-terminal. So,
the I6 State becomes
I6 = A → a•A, $
A → •aA, $
A → •b, $
Go to (I6, a) = Closure (A → a•A, $) = (same as I6)
Go to (I6, b) = Closure (A → b•, $) = (same as I7)
I7= Go to (I2, b) = Closure (A → b•, $) = A → b•, $
I8= Go to (I3, A) = Closure (A → aA•, a/b) = A → aA•, a/b
I9= Go to (I6, A) = Closure (A → aA•, $) = A → aA•, $
Drawing DFA:
CLR (1) Parsing table:
C Compiler