Ronali - Compiler - Design - Midsem - Solution
Ronali - Compiler - Design - Midsem - Solution
Question No Question CO
Mappi
ng
Q.No:2 A) Show the output for each phase for the expression S CO1,
= a + b * c + 20 with a neat and clean diagram of CO2
translation of the given statement( where the variables
a ,b,c are of floating type and S is of integer type).
Ans:
B) Remove left recursion from the following grammar:
S -> A / a / BAC
A -> B / b
B -> S / c
C -> d
Ans.
X-> xYw / xZC => X-> xX′, X′->Yw / ZC
Y -> x / xy / xyZy / xYy
Z -> zzw / zwZ / z / ε => Z -> zZ′, Z′->zw / wZ / ε
Ans.
Remove left-recursion from S -> A / a / SAC / cAC
S->AS′ / aS′ /cACS′
S′->AC / ∈
A -> S / b / c
C -> d
Remove left-recursion
S->aS′T/cSCS′T /bS′T /cS′T/ cbCS′T/ ccCS′T
T->S′/∈
C -> d
Q.No:3 A) Show the output for each phase for the expression S = CO1,
X / Y - Z + 20.0 with a neat and clean diagram of CO2
translation of the given statement( where the variables
X ,Y are of floating type and Z , S are of integer type).
Ans:
B) Remove left recursion from the following grammar:
S -> Xa / Sa / b
X -> Xc / Se / d
Ans-
S -> XaS' | bS'
S'->aS' | ε
X -> SeX' | dX'
X' -> cX' | ε
S->aS'''
S'''->bS'' | g
S''->cS'| f
S'-> d | e
S->aS'
S'->A | bS''
S''->Aa | cA | A
A-> ab| b
S->aS'
S'-> ab| b | bS''
S''->aba | ba|cab|cb | ab| b
Q.No:4 A) Show the output for each phase for the expression a= CO1,
b*c/d+e-60.0 with a neat and clean diagram of translation CO2
of the given statement( where the variables b ,c and d are
of floating type and a , e are of integer type).
Ans:-
B) Remove left recursion from the following grammar:
X -> Ya / Xa / c
Y -> Yb / Xb / d
Ans:-
C ) Left factor the following grammar:
X-> xYw / xZ
Y -> x / xy
Z -> zzw / wwZ / z / w
Ans:-
Q.No:5 A) Compute the FIRST and FOLLOW sets of each non-terminal CO2,
in the grammar given bellow. Also construct the LL(1) parsing CO3
table.
S->aABh
A->cC
C->bC|ε
B->ED
E->g|ε
D->f|ε
Ans:-
First Functions-
First(S) = { a }
First(A) = { c }
First(C) = { b , ∈ }
First(B) = { First(E) – ∈ } ∪ First(D) = { g , f , ∈ }
First(E) = { g , ∈ }
First(D) = { f , ∈ }
Follow Functions-
Follow(S) = { $ }
Follow(A) = { First(B) – ∈ } ∪ First(h) = { g , f , h }
Follow(C) = Follow(A) = { g , f , h }
Follow(B) = First(h) = { h }
Follow(E) = { First(D) – ∈ } ∪ Follow(B) = { f , h }
Follow(D) = Follow(B) = { h }
a b c g f h $
S S→
aABh
A A→
cC
B B→B→
EDED
C C→ C→∈ C→ C→
bC ∈ ∈
D D→f D→
∈
E E→g E→ E→
∈ ∈
E 0E*AX
X YX |
Y +A | %0
A 1B
BA01 |
void E();
void X();
void Y();
void A()
void B();
void advance();
char input[10];
int lookahead;
int main()
{
printf("\nEnter the string to be parsed: ");
gets(input);
lookahead=0;
E();
printf("\n\n\t\tString successfully parsed.\n");
return 0;
}
void E()
{
if (input[lookahead]=='0')
{
advance();
if (E())
{
if (input[lookahead]=='*')
{
advance();
if (A())
X();
}
}
}
}
void X()
{
if(Y())
X();
else
return true;
}
void Y()
{
if (input[lookahead]=='+')
{
advance();
A();
}
else if (input[lookahead]=='%')
{
advance();
if (input[lookahead]=='0')
return true;
}
}
void A()
{
if (input[lookahead]=='1')
{
advance();
B();
}
}
void B()
{
if (A())
if (input[lookahead]=='0')
if (input[lookahead]=='1')
return true;
}
void advance()
{
if (input[lookahead]=='\0')
error();
lookahead++;
}
Q.No:6 A) Compute the FIRST and FOLLOW sets of each non-terminal CO2,C
in the grammar given bellow. Also construct the LL(1) parsing O3
table.
S-> aAC/Bb
A - >cD
B ->f/g
C ->h/i
D ->bE/ε
E ->eD/dD
B) Consider the following grammar
S -> xXy / yY
X -> Xx / ε
Y -> Yy / ε
Write down the procedures for the non terminals of the given
grammar to make a recursive descent parser .
Ans:
Q. No:7 A) Construct the LL(1) parse table and verify whether it is LL(1) CO2,C
or not. O3
S-> ACB / CbB / Ba
A -> da / BC
B -> g / ε
C -> h / ε
B) Consider the following grammar
S -> A $
A -> - A B | id C
B -> - B | ""
C -> "" | . id
Write down the procedures for the non terminals of the given
grammar to make a recursive descent parser .
Ans:
Controller of Examinations