Compiler Design and Construction - Old Questions
1. Differentiate between top-down and bottom-up parsing methods. Construct SLR parse table for the following grammar.
Difference between top-down and bottom-up parsing methods
Top-down Parsing | Bottom-up Parsing |
It is a parsing strategy that first looks at the highest level of the parse tree and works down the parse tree by using the rules of grammar. | It is a parsing strategy that first looks at the lowest level of the parse tree and works up the parse tree by using the rules of grammar. |
Top-down parsing attempts to find the left most derivations for an input string. | Bottom-up parsing can be defined as an attempt to reduce the input string to start symbol of a grammar. |
In this parsing technique we start parsing from top (start symbol of parse tree) to down (the leaf node of parse tree) in top-down manner. | In this parsing technique we start parsing from bottom (leaf node of parse tree) to up (the start symbol of parse tree) in bottom-up manner. |
This parsing technique uses Left Most Derivation. | This parsing technique uses Right Most Derivation. |
Its main decision is to select what production rule to use in order to construct the string. | Its main decision is to select when to use a production rule to reduce the string to get the starting symbol. |
A top-down parser can be easily structured and formed. | It is difficult to produce a bottom-up parser |
It is less powerful. | It is more powerful. |
Error detection is easy. | Error detection is difficult. |
Now,
Given grammar:
1. S → aETe
2. E → Ebc
3. E → b
4. T → d
The augmented grammar of given grammar is,
S‘ → S
S → aETe
E → Ebc
E → b
T → d
Next, we obtain the canonical collection of sets of LR(0) items, as follows,
I0 = closure({S'→•S}) = {S'→•S, S→ •aETe }
goto(I0, S) = {S'→S•}= I1
goto(I0, a) = closure({S→ a•ETe}) = {S→ a•ETe, E → •Ebc, E → •b}= I2
goto(I2, E) = closure({S→ aE•Te, E → E•bc}) = {S→ aE•Te, E → E•bc, T → •d}= I3
goto(I2, b) = closure({E → b•}) = {E → b•} = I4
goto(I3 , d) = closure({T → d•}) = I5
goto(I3 , b) = closure({E → Eb•c}) = {E → Eb•c}= I6
goto(I3 , T) = closure({S→ aET•e}) = {S→ aET•e} = I7
goto( I6 , c) = closure({E → Ebc•}) = {E → Ebc•}= I8
goto(I7 , e) = closure({S→ aETe•}) = {S→ aETe•}= I9
FOLLOW set for non-terminals are,
FOLLOW(S) = {$}
FOLLOW(E) = {b, d}
FOLLOW(T) = {e}
Now, the SLR parsing table is,
States/ Terminals |
Action Table |
Goto Table |
|||||||
a |
b |
c |
d |
e |
$ |
S |
E |
T |
|
0 |
S2 |
|
|
|
|
|
1 |
|
|
1 |
|
|
|
|
|
Accept |
|
|
|
2 |
|
S4 |
|
|
|
|
|
3 |
|
3 |
|
S6 |
|
S5 |
|
|
|
|
7 |
4 |
|
R3 |
|
R3 |
|
|
|
|
|
5 |
|
|
|
|
R4 |
|
|
|
|
6 |
|
|
S8 |
|
|
|
|
|
|
7 |
|
|
|
|
S9 |
|
|
|
|
8 |
|
R2 |
|
R2 |
|
|
|
|
|
9 |
|
|
|
|
|
R1 |
|
|
|