You are on page 1of 21

NAME: MUNEEB AHMED

ID: 8751
Question 1)
Grammar: XYa|cYb|Zb|cZa
Yq|
|XaZZ
f|q
A: Show that the given grammar is not suitable for LALR (1) but suitable for LR (1):

I0: 0 X’ -> .X, $/a


1 X - > .Y a, $/a
2 X - > . c Y b, $/a
3 X - > . Z b, $/a
4 X - > . c Z a, $/a
5 Y - > . q, a
6 Y - > . ∈, a
7 Y - > . X a Z, a
8 Z - > . f, b
9 Z - > . q, b

{I0, X} - > I1:


X - > X . , $/a
Y - > X . a Z, a

{I0, Y} - > I2:


X - > Y . a, $ /a

{I0, Z} - > I3:


X - > Z . b, $ / a

{I0, c } - > I4:


X - > c . Y b, $ / a
X - > c . Z a, $ / a
Y - > . q, b
Y - > . ∈, b
Y - > . X a Z, b
Z - > . f, a
Z - > . q, a
X - > .Y a, a
X - > . c Y b, a
X - > . Z b, a
X - > . c Z a, a

{I0, q} - > I5:


Y->q.,a
Z->q.,b

{I0, ∈} - > I6:


Y - > ∈. , a
{I0, f} - > I7:
Z - > f. , b

{I1, a} - > I8:


Y->Xa.Z,a

{I2, a} - > I9:


X - > Y a . , $/a

{I3, b} - > I10:


X->Zb.,$/a

{I4, Y} - > I11:


X - > c Y . b, $ / a
X - > Y. a, a

{I4, Z} - > I12:


X - > c Z . a, $ / a
X - > Z . b, a

{I4, X} - > I 13:


Y - > X . a Z, b

{I4, c} - > I 14:


X - > c . Y b, a
X - > c . Z a, a
Y - > . q, a
Y - > . ∈, a
Y - > . X a Z, a
Z - > . f, a
Z - > . q, a
X - > .Y a, a
X - > . c Y b, a
X - > . Z b, a
X - > . c Z a, a

{I4, q} - > I15:


Y-> q.,a
Z->q.,a

{I4, f} - > I16:


Z - > f ., a

{I4, ∈ } - > I17:


Y-> ∈.,b

{I8 , Z} - > I18:


Y->Xa Z.,a

{I11 , b } - > I19:


X->cYb.,$/a

{I11, a} - > I20:


X->Ya.,a

{I12, a} - > I21:


X->cZ a.,$/a

{I12, b} - > I22:


X-> Z b., a

{I13, a} - > I23:


Y - > X a . Z, b
X - > Y . a, a

{I14 , Y} -> I24:


X - > c Y . b, a
X - > Y . a, a

{I14 , Z} -> I25:


X - > c Z . a, a
X - > Z . b, a

{I14 , q} -> I15:

{I14 , f} -> I16:

{I14 , ∈} -> I26:


Y-> ∈.,a

{I14 , c} -> I14:

{I23, Z} - > I27:


Y->X a Z., b

{I23, a} - > I28:


X->Ya.,a

{I24, b} -> I29:


X - > c Y b ., a

{I24, a} -> I30:


X - > Y a ., a
{I25, a} - > I31:
X - > c Z a ., a
{I25, b} -> I32:
X - > Z b .,

Parsing Table:

State ACTION GOTO

A B c Q ∈ f $ X’ X Y Z
0 R5 R9 S4 S5 S6/R6 S7/R 1 2 3
8
W1 S8/R1 R/1
2 S9
3 S10
4 S14 S15 S17 S16 S13 S11 S12
5
6
7
8 18
9
10
11 S20 S19
12 S21 S22
13 S23
14 S14 S15 S26 S16 24 25
15
16
17
18
19
20
21
22
23 S28 27
24 S30 S29
25 S31 S32

There are many conflicts so that his grammar is not suitable for LALR(1) parsing.
B: Now modify the grammar, so that it becomes suitable for LALR (1). Make sure that the
modified grammar should results the same set of strings as the given grammar.

Ans)
LALR(1) Parsing table:

State Actions GOTO


a C b Q f $ X’ X Y Z
0 S4 S2 S6 3 1 5
1 S7
2 R4/R7 R4/R7
3 S8
4 S4 S2 S6 3 9 10
5 S11
6 R6 R6
7 acc acc
8 S13 S6 12
9 S7 S14
10 R3
11 R2
12 R5 R5
13 R7 R7
14 R1

Removing epsilon from above grammar

State Actions GOTO


A C b q F $ X’ X Y Z
0 S4 S2 S6 3 1 5
1 S7
2 R7 R7
3 S8
4 S4 S2 S6 3 9 10
5 S11
6 R6 R6
7 Acc acc
8 S13 S6 12
9 S7 S14
10 R3
11 R2
12 R5 R5
13 R7 R7
14 R1
C: Create the stack table to parse the given input string: c q a a f

Ans)

LR(1) Parsing Table:

State Actions GOTO


A c B q F $ X’ X Y Z
0 S3 S5 S6 1 2 3
1 S7 accept
2 S8
3 S14 S11 S13 12 9 10
4 S15
5 R8
6 R7
7 S18 S17
8 R1 R1 16
9 S20 S19
10 R4 S21 R4
11 R5 R8 R8
12 S22
13 R7 R7 R7
14 S14 S25 S26 12 23 24
15 R3 R3
16 R5
17 R7
18 R8
19 R2 R2
20 R1
21 R3
22 S28 S26 27
23 S20 S29
24 R4 S21
25 R8 R8
26 R7 R7
27 R6 R6
28 R8 R8
29 R2
By looking at the above LR(1) parsing table

Stack Input Action


$0 cqaaf$ S3
$c3 qaaf$ S11
$ c 3 q 11 aaf$ R5
$c3Y aaf$ 9
$c3Y9 aaf$ S20
$ c 3 Y 9 a 20 af$ R1
$c3X af$ 12
$ c 3 X 12 af$ S22
$ c 3 X 12 a 22 f$ S26
$ c 3 X 12 a 22 f 26 $

D: Fill the LALR(1) parsing table with suitable error codes / messages and identify the error
in the given input string: c b a f a q a c q
Ans)

LALR(1) Parsing table:

State Actions GOTO


a C b f Q $ X’ X Y Z
0 R5 S3 S5 S6 1 2 4
1 S7 Acc
2 S8
3 R5 S3 R5 S5 S6 11 9 10
4 S4 S12
5 R7 R7 R7
6 R8 R8 R8
7 S5 S6 13
8 R1 R1
9 S8 S14
10 R4 S12 R4
v11 S7
12 R3 R3
13 R6 R6
14 R2 R2
By looking at above LALR(1) parsing table :

Stack Input Action


$0 cbafaqacq$ S3
$0 c 3 bafaqacq$ R5
$0 c 3 Y bafaqacq$ 9
$0 c 3 Y 9 bafaqacq$ S14
$0 c 3 Y 9 b 14 afaqacq$ R2
$0 X afaqacq$ 1
$0 X 1 afaqacq$ S7
$0 X 1 a 7 faqacq$ S5
$0 X 1 a 7 f 5 aqacq$ R7
$0 X 1 a 7 Z aqacq$ 13
$0 X 1 a 7 Z 13 aqacq$ R6
$0 Y aqacq$ 2
$0 Y 2 aqacq$ S8
$0 Y 2 a 8 qacq$ Error

Question: 2 For a given grammar:


X0X0|1X1|Y
Y1|0

A) Create syntax directed definition (SDD) to get the value and the length of the string

Ans)

Production:
X->0X0
X->1X1
X->Y
Y->1
Y->0
Semantic rules:
X.len = len + 1;
X.val = digit1.lexval
X1.len = len + 1; X1.val = digit2.lexval
X.val = Y.val
Y.val = digit.lexval
Y.val = digit.lexval

By doing len = len + 1 on every digit node we can calculate length

Parse tree:

Input String: 11000


X Syn
1 Inh 1
X1
x Inh y
Error Y Error

0
B) SDD should also generate an error message if the given string is not acceptable by the
grammar.

Ans)

Production:
X->0X0 |dXd
X->1X1|dXd
X->Y
Y->1
Y->0
..

Semantic rules:
X1.len = len + 1; X1.val = digit1.lexval
X.depth = depth + 1; X2.val = digit2.lexval
X.val = Y.val
Y.val = 1
Y.val = 0

Actions:
{Print(error)} (if X - > digit other than 0 and 1)
{Print(error)} (if X - > digit other than 0 and 1)

Parse tree:
Input String: 11xy0

X Syn
1 Inh 1
X1
x Inh y
Y
Error Error
0

C) Convert the SDD into an equivalent L-attributed SDD and identify each attribute.

Ans)

Production:
X->0X0
X->1X1
X->Y
Y->1
Y->0
Semantic rules
X.val = X1.syn
X1.inh = Y.val
Y.syn = X1.val
X1.syn = X.val
X.val = Y.val
Y.val = 1
Y.val = 0

Parse tree:
Input String: 11000

X Syn
Syn 1 Inh 1 digit.lexval = 1
digit.lexval=1 X1
0 Inh 0 digit.lexval = 0
digit.lexval = 0 Y
0 Syn
digit.lexval = 0

D: Convert the L-attributed SDD into an equivalent syntax directed translation (SDT)

Ans)
Since SDT merge production and semantic rules so,

Actions:
X - > 0 X 0 { X.val = 0 X1.val 0}
X - > 1 X 1 { X.val = 1 X1.val 1}
X - > Y {X.val = Y.val }
Y - > 1 {Y.val = 1}
Y - > 0 {Y.val = 0 }

Parse tree:
Input String: 11000

X Syn
Syn 1 Inh 1 digit.lexval = 1
digit.lexval=1 X1
0 Inh 0 digit.lexval = 0
digit.lexval = 0 Y
0 Syn
digit.lexval = 0
QUESTION 3:

A: Create a variable declaration grammar that accepts: single/multiple declaration of variables


(e.g int x, y; float a, char s), single/multiple dimensional of arrays (e.g float d[12][3], char p[10]),
pointer type variables (e.g. int *ptr) and record (e.g. STRUCT type) type variables.

Ans)
If (peek holds a digit) {
v = 0;
do {
v = v * 10 + integer value of digit peek;
peek = next input character;
} while (peek holds a digit);
return token (num , v)
}
struct Item
{
int itemNo;
char desc[41];
float price;
};
B: Now consider that the integer takes 4 bytes, float takes 8 bytes and char takes 2 bytes. Create
an SDD that generate the total bytes allocated by the given input declaration statement. (e.g int
s[10][3] allocates 120 bytes in memory).

Ans)
L

E.val=19 newline

E.val=15 + T.val=4

T.val=15 F.val=4

T.val=3 * F.val=15 Number.lexval=4

F.val=3 Number.lexval=5

Number.lexval=3

C: Create dependency graph for the input string: record {int


x;
float y[10];} data[5];
Dependency graph example
Ans)
Production:

E -> E1 + E2

Semantic Rule:

E.val := E1.val + E2.val


Wherever this rule appears in the parse, tree we draw:

E val

E1 val + E2 val

D
T L
Real
L

L / Id 2 Id3

Id1

Parse tree for real ID1 , ID2 , ID3

D Dummy node by
T in L call to addtype()
Real Type in
entry
L

in L / Id2 Id3
entry
Id1
entry
D: Now convert the SDD into an equivalent SDT.
Ans)

S^48

T U

[3] U^1
int
[4] U

E
QUESTION 4:

A: Consider syntax of Switch-Case statement of any programming language and create an


equivalent L-attributed SDD for chosen grammar.

Ans)
Switch-Case syntax:
switch expression
begin
case value : statement
case value : statement
...
case value : statement
default : statement
end

L Attribute SDD:
1) switch E
2) begin
3) case V1 : S1
4) case V2 : S2
...
n-2) case Vn-1 : Sn-1
n-1) default : Sn
n) end

B: Consider an unambiguous expression grammar that accepts parenthesis, +, ̶ , ×, ÷ and


unary minus (-) operators; Create SDD that generates directed acyclic graph (DAG) or syntax
tree.

Ans)
If exp1 and exp2 are arithmetic expressions, then so are the following:

exp1 - exp2
exp1 + exp2
exp1 * exp2
exp1 / exp2
–exp1
( exp1 )
Here is the corresponding CFG:
exp --> INTLITERAL
exp --> exp MINUS exp
exp --> exp PLUS exp
exp --> exp MULTIPLE exp
exp --> exp DIVIDE exp
exp --> UMINUS exp
exp --> LPAREN exp RPAREN
FOR EXAMPLE PARSE TREE OF: x = a + b – c × ( d ÷ e )
=

X *

( / )
-

+ c d e

a b

C: Consider the grammar in part (B) and rewrite SDD to generate Three-Address Code that
accepts the following input string: x = a + b – c × ( d ÷ e )

Ans)

t1 = d ÷ e
t2 = UMINUS c
t3 = a + b + t2
t4 = t1 * t3
x = t4
D: Consider the following Semantic rules:

Ans
Now translate the following assignments:
i) y = a [ i ] [ j ] + b [ c [ k ] ] [ j ]

E.addr=t

Y = E.addr=t

L.addr=t L.addr = t +

L.addr=t
a [ i ] [ j ]

L.addr=t
L.addr=t

b [ c [ k ] ] [ j ]

ii) y = c [ k ] + b [ a [ c [ i ] ] + 2 ]
E.addr = t

Y = E.addr = t

+
E.addr = t

E.addr = t
c [ k ]

b [ E.addr=t ]

a [ E.addr=t ]

c [ i ]

You might also like