You are on page 1of 7

ASSIGNMENT # 3 (CLO2) B.S.(C.S.

) – 7
Submission date: 28-04-2022 (10 points)
Q.1
a) For the grammar S → 0 S 1 | 0 1
Give bottom-up parses for the input string: 000 1 1 1

b) For the grammar S→S S + | S S * | a


Give bottom-up parses for the input string: aaa * a + +
--------------------------------------------------------------------------
Q. 2
Consider the following grammar:
E →(L) | a
L → L, E| E
Part-I
a) Construct the DFA of LR(0) items for this grammar.
b) Construct the SLR(1) parsing table for this grammar.
c) Show the parsing stack and the actions of an SLR(1) parser for the input string
( ( a ), a , (a , a ) ).
d) Is this grammar an LR (0) grammar? If not, describe the LR(0) conflict. If so,
construct the LR(0) parsing table, and describe how a parse might differ from an
SLR(1) parse.
Part-II
a) Construct the DFA of LR(1) items for this grammar.
b) Construct the general LR(1) parsing table.
c) Construct the DFA of LALR(1) items for this grammar.
d) Construct the LALR(1) parsing table.
e) Describe any difference that might occur between the actions of a general LR(1)
parser and an LALR(1) parser.

Q.3
Consider the following grammar:
S → S(S) | ∈
Part-I
a) Construct the DFA of LR(0) items for this grammar.
b) Construct the SLR(1) parsing table for this grammar.
c) Show the parsing stack and the actions of an SLR(1) parser for the input string
( ( ) ( ) ).
d) Is this grammar an LR (0) grammar? If not, describe the LR(0) conflict. If so,
construct the LR(0) parsing table, and describe how a parse might differ from an
SLR(1) parse.
Part-II
a) Construct the DFA of LR(1) items for this grammar.
b) Construct the general LR(1) parsing table.
c) Construct the DFA of LALR(1) items for this grammar.
d) Construct the LALR(1) parsing table.
e) Describe any difference that might occur between the actions of a general LR(1)
parser and an LALR(1) parser.
---------------------------------------------------------------------------------------
Q.4
Consider the following grammar of simplified statement sequences:
stmt-sequence → stmt-sequence ; stmt | stmt
stmt → s
Part-I
a) Construct the DFA of LR(0) items for this grammar.
b) Construct the SLR(1) parsing table for this grammar.
c) Show the parsing stack and the actions of an SLR(1) parser for the input string
s ; s ; s.
d) Is this grammar an LR (0) grammar? If not, describe the LR(0) conflict. If so,
construct the LR(0) parsing table, and describe how a parse might differ from an
SLR(1) parse.
Part-II
a) Construct the DFA of LR(1) items for this grammar.
b) Construct the general LR(1) parsing table.
c) Construct the DFA of LALR(1) items for this grammar.
d) Construct the LALR(1) parsing table.
e) Describe any difference that might occur between the actions of a general LR(1)
parser and an LALR(1) parser.
------------------------------------------------------------------------------------
Q.5
Consider the following grammar:
E →(L) | a
L → E L | E
Part-I
a) Construct the DFA of LR(0) items for this grammar.
b) Construct the SLR(1) parsing table for this grammar.
c) Show the parsing stack and the actions of an SLR(1) parser for the input string
( ( a ) a (a a ) ).
Part-II
d) Construct the DFA of LALR(1) items by propagating the lookaheads through the
DFA of LR(0) items.
e) Construct the LALR(1) parsing table.

Q. 6
Consider the following grammar:
declaration → type var-list
type → int | float
var-list → identifier , var-list | identifier
Part-I
a) Rewrite it in a form more suitable for bottom-up parsing.
b) Construct the DFA of LR(0) items for the re-written grammar.
c) Construct the SLR(1) parsing table for the re-written grammar.
d) Show the parsing stack and the actions of an SLR(1) parser for the input string int x,
y, z using the table of part (c).
Part-II
a) Construct the DFA of LALR(1) items by propagating the lookaheads through the
DFA of LR(0) items of part(b).
b) Construct the LALR(1) parsing table for the rewritten grammar.
--------------------------------------------------------------------------------------
Q.7
Consider the following grammar.
E→E+T | T
T→T*F | F
F → ( E ) | id
a) Configure the LR(0) automaton for the above expression grammar.
b) Construct SLR(1) parsing table for the above expression grammar.
c) Configure the shift-reduce parser for the above grammar on the input id * id + id.
-----------------------------------------------------------------------------------------
Q.8
i) Consider the following grammar
S' → S
S→aAdǀbBdǀaBeǀbAe
A→c
B→c
Show that the above grammar is LR(1) but not LALR(1).
ii)
a. show that the following grammar is not LR(1):
A→ aAa| ∈
b. Is this grammar ambiguous? Why or why not ?
Q. 9
Trace the behavior of a panic mode error recovery mechanism for the string (*2. using
the following parsing table. (As is discussed on page 246. Example 5.19) the DFA is
given on page 233.(Louden book)
State INPUT Goto
Number ( + - * ) $ command exp term Factor
0 s5 s6 1 2 3 4
1 accept
2 r1 r1 s7 s8 r1 r1 r1
3 r4 r4 r4 r4 s9 r4 r4
4 r6 r6 r6 r6 r6 r6 r6
5 r7 r7 r7 r7 r7 r7 r7
6 s5 s6 10 3 4
7 s5 s6 11 4
8 s5 s6 12 4
9 s5 s6 13
10 s7 s8 s14
11 r2 r2 r2 r2 s9 r2 r2
12 r3 r3 r3 r3 s9 r3 r3
13 r5 r5 r5 r5 r5 r5 r5
14 r8 r8 r8 r8 r8 r8 r8
-------------------------------------------------------------------------
Q. 10. Consider the following grammar G:

P→ Q )
Q → Q , R | ( R, R
R → { num , num }

i) Construct the LR(0) items with DFA states in each case.


ii) Construct an LR(0) Action-Goto parsing table for the above grammar.
-------------------------------------------------------------------------------------------
Q. 11
Given the following CFG grammar G = ({S,A,B}, S, {a, b, x), P) with P:
(1) S → A
(2) S → xb
(3) A → aAb
(4) A → B
(5) B → x
For this grammar answer the following questions:
a) Compute the set of L.A.L.R(1) items for this grammar and the corresponding DFA. Do not
forget to augment the grammar with the default initial production S'→ S$ as the production(0).
b) Construct the corresponding LR(1) parsing table.
c) Would this grammar be LR(0)? Why or why not? (Note: you do not need to construct the set
of LR(0) items but rather look at the ones you have already made for LR(1) to answer this
question.
d) Show the stack contents, the input and the rules used during parsing for the input string
w = axb$
e) Would this grammar be suitable to be parsed using a top-down LL parsing method? Why?
-----------------------------------------------------------------------------------------------------
Q.12 (Ullman sethi book, page 263)
Consider the following augmented grammar:
S' → S
S→CC
C→cC| d
a) Draw the GOTO graph of LR(1) for the above grammar.
b) Make the Canonical parsing table for the grammar.
c) Make the LALR(1) parsing table for the above grammar.
--------------------------------------------------------------------------------
Q. 13 Consider the following grammar:
SAA
A aA|b
i) Construct the DFA of Canonical CLR(1) items for the above grammar.
ii) Construct the CLR(1) parsing table for the above DFA.
iii) Construct the DFA of L.A.L.R.(1) items for the above grammar.
iv) Construct the L.A.L.R.(1) parsing table for the above DFA.
------------------------------------------------------------------------------------------
Q.14
i) Show that the following grammar:

S →A a A b ǀ B b B a
A→∈
B →∈
is LL(1) but not SLR(1).

ii) Show that the following grammar:


S →S A ǀ A
A→a
is SLR(l) but not LL(1) .
------------------------------------------------------------------------------------
Q.15
i) Show that the following grammar
S → Aa ǀ bAc ǀ dc ǀ bda
A→d
is LALR(l) but not SLR(l) .

ii) Show that the following grammar


S → Aa ǀ bAc ǀ Bc ǀ bBa
A→d
B→d
is LR(l) but not LALR(l).
Q. 16
The following is an ambiguous grammar:
S → AS ǀ b
A → SAǀ a
Construct for this grammar its collection of sets of LR(0) items. If we try to build an LR-parsing
table for the grammar, there are certain conflicting actions. What are they?
Suppose we tried to use the parsing table by non-deterministically choosing a possible action
whenever there is a conflict. Show all the possible sequences of actions on input abab.
Q. 17 (Ex. 4.58 of ulman book)
Consider the grammar
S' → S (Augmented rule)
S→aAdǀbBdǀaBeǀbAe
A→c
B→c
which generates the four strings acd, ace, bcd, and bce. It can be checked that the grammar is
LR(l) by constructing the sets of items. Upon doing so, we find the set of items
{[A → c., d], [B → c. , e]} valid for viable prefix ac and
{[A → c. , e] , [B → c., d]} valid for bc.
Neither of these sets has a conflict, and their cores are the same. However, their union, which is
A → c. , d/e
B → c., d/e
generates a reduce/ reduce conflict, since reductions by both A → c. and B → c. are called for on
inputs d and e.
Construct LR(1) and LALR(1) items.
Q.18
a) Show that the following grammar
S → Aa ǀ bAc ǀ dc ǀ bda
A →d
is LALR( l ) but not SLR(l).

b) Show that the following grammar


S → Aa ǀ bAc ǀ Bc ǀ bBa
A→d
B→d
is LR( l ) but not LALR( l ) .
---------------------------------------------------------------------------------------------------------
Q.19
Consider the expression grammar.
E → E + E | E * E | ( E ) | id
On the erroneous input id +), Show the sequence of configuration entered by the parser.
Fill the LR Parsing Table with error Routines. The error routines are as follows:

I0: E' → . E I5: E → E *. E


E→.E+E E→.E+E
E→.E*E E→.E*E
E→.(E) E→.(E)
E → . id E → . id

I1: E' → E. I6: E → ( E .)


E → E .+ E E→E.+E
E → E .* E E→E.*E
I2: E → (. E ) I7: E → E + E.
E→.E+E E→E.+E
E→.E*E E→ E.*E
E→.(E)
E → . id I8: E → E * E.
E→E.+E
I3: E → id . E→E.*E

I4: E → E + . E I9: E → ( E ).
E→.E+E
E→.E*E
E→.(E)
E → . id

Figure: Sets of LR(0) items for an augmented expression grammar.


-------------------------------- THE END ---------------------------------------------------

You might also like