Professional Documents
Culture Documents
Name: Muneeb Ahmed ID: 8751
Name: Muneeb Ahmed ID: 8751
ID: 8751
Question 1)
Grammar: XYa|cYb|Zb|cZa
Yq|
|XaZZ
f|q
A: Show that the given grammar is not suitable for LALR (1) but suitable for LR (1):
Parsing Table:
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:
Ans)
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)
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
Parse tree:
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:
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
F.val=3 Number.lexval=5
Number.lexval=3
E -> E1 + E2
Semantic Rule:
E val
E1 val + E2 val
D
T L
Real
L
L / Id 2 Id3
Id1
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:
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
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 ]