Previous Year Compiler Design Solved Gate Question by Kanodia
Previous Year Compiler Design Solved Gate Question by Kanodia
Previous Year Compiler Design Solved Gate Question by Kanodia
com
YEAR 2001
Question. 1
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
time.
(C) Linking is insecure
(D) Cryptographic procedures are not available for dynamic linking
SOLUTION
Question. 3
Assume that the SLR parser for a grammar G has n1 states and the
LALR parser for G has n2 states. The relationship between n1 and n2
is
(A) n1 is necessarily less than n2
(B) n1 is necessarily equal to n2
(C) n1 is necessarily greater than n2
(D) None of the above
Page 2
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
SOLUTION
SLR parsue is less range of context free languages than LALR but
still both n1 & n2 are same for SLR & LALR respectively.
Hence (B) is correct option.
Question. 5
(1) True for statically typed languages where each variable has fixed
type. Similarly (4) is also correct.
(2) True, in un-typed languages types of values are not defined.
But option (C) is false, since in dynamically typed language variables
have dynamically changing types but not that they have no type.
Hence (C) is correct option.
Page 3
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
Question. 7
Given grammar
S " EtSSl
Sl " eS
E "b
Terminals
N.T
S
S " iEtSSl
S"
Sl
Sl " eS
Sl "
Sl "
E
E"b
Predictive Parsing table.
So this table presents predictive parsing for dangling if else & shows
ambiguity
M [Sl, e] = {Sl " eS, Sl " }
M [Sl, $] = {Sl " }
Hence (D) is correct option.
Question. 8
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
Given grammar
S " CC
C " cC d
it cant be LL since C " cC is recursive. LR (1) also known as CLR
parser, and every CF grammar is CLR grammar.
So (A) is false but (C) & (D) can be correct.
This grammar is CLR and also reducible to LALR without any
conflicts. So (D) is false.
Only need to check for SLR (1) or LR (0)
This grammar is not SLR.
Hence (C) is correct option
Question. 9
(B) 9 5 + 2 +
(C) 9 5 2 ++
(D) ++ 9 5 2
SOLUTION
S " TR
R " + T {pr int (' + ');} R
T " num {print (num.val);}
Given string 9 + 5 + 2
S " TR
Page 5
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
T + TR
T+T+T
9+T+T
9+5+T
9+5+2
{print (+);}
{print (+);}
{print (9);}
{print (5);}
{print (2);}
So ++ 952 is printed
Hence (D) is correct option.
Question. 10
"
newtemp ();
gen(t
. place
. place;);
.place
t}
.place
.place;}
Here, gen is a function that generates the output code, and newtemp
is a function that returns the name of a new temporary variable on
every call. Assume that t1 s are the temporary variable names
generated by newtemp.
For the statement X: = Y + Z , the 3-address code sequence generated
by this definition is
(A) X = Y + Z
(B) t1 = Y + Z; X t1
(C) t1 = Y; t2 = t1 + Z; X = t2
(D) t1 = Y; t2 = Z; t3 + t2; X = t3
SOLUTION
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
) }
Question. 11
SOLUTION
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
(C) 25, 15
SOLUTION
In dynamic scoping, the local values are considered & variables are
initialized at run time.
Since
x = i + j & in P (x)
i = 200 & j = 20
x = 200 + 20 = 220
& printing
(x + 10 )
x = i + j + 10
= 10 + 5 + 10 = 25
Hence (B) is correct option
Question. 13
Page 8
(D) 2 2 2
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
SOLUTION
1.
Px = newQ ();
2.
Qy = newQ ();
3.
Pz = newQ ();
4.
x : f (1); print 2 # i = 2
5.
((P) y) : f (1);
6.
z : f (1) print 2 # i = 2
but line 5. will print 2 because typecast to parent class cant prevent
over ridding. So function f (1) of class Q will be called not f (1) of
class P .
Hence (D) is correct option.
Question. 14
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
operator grammar? P, Q, R are non-terminals, and r, s, t are terminals
.
(i) P " QR
(ii) P " Q s R
(iii) P "
(iv) P " Q t R r
SOLUTION
(I)
Question. 16
(B) Compile-time
(C) Link-time
(D) Load-time
SOLUTION
The two modules needed to be linked since definition exist & M2 &
M1 refers it. So during linking phase M1 links to M2.
Hence (C) is correct option.
Question. 17
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
(D) 40
SOLUTION
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
YEAR 2005
Question. 19
(B) Left-recursive
(C) right-recurisve
(D) an operator-grammar
SOLUTION
The grammar is definitely left & right recursive but it is not suitable
for predictive parsing because it is ambiguous.
Hence (A) is correct option.
Question. 20
(B) n, E + n and E + E # n
(C) n, n + n and n + n # n
(D) n, E + n and E # n
SOLUTION
Given grammar
E " E+n
E " E#n
E "n
String = n + n # n
Right sentential so right most non terminal will be used.
E " E#n
{E " E # n}
E+n#n
{E " E + n}
n+n#n
{E " n}
So during reduction the order is reverse.
So
{E " n, E " E + n, E " E # n}
Hence (D) is correct option.
Question. 21
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
S " (S)| a
Let the number of states in SLR(1), LR(1) and LALR(1) parsers for
the grammar n1 n2 and n3 respectively. The following relationship
holds good
(B) n1 = n3 < n2
(A) n1 < n2 < n3
(C) n1 = n2 = n3
(D) n1 $ n3 $ n2
SOLUTION
The no. of states for SLR (1) & LALR (1) are equal so n1 = n 3 , but
CLR (1) or LR (1) will have no. of states greater than LALR & LR (0)
both.
Hence (B) is correct option.
Question. 22
There are no lexical errors for C because all the wrong spelled keywords
would be considered as identifiers until the syntax is checked.
So the compiler would give syntax errors.
Hence (C) is correct option.
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
number
E
E # E
;
Eval
E .val
E .val
number val
E .VAL E .val
E .VAL E .val
Question. 23
The above grammar and the semantic rules are fed to a yacc tool
(which is an LALR(1) parser generator) for parsing and evaluating
arithmetic expressions. Which one of the following is true about the
action of yacc for the given grammar?
(A) It detects recursion and eliminates recursion
(B) It detects reduce-reduce conflict, and resolves
(C) It detects shift-reduce conflict, and resolves the conflict in favor
of a shift over a reduce action
(D) It detects shift-reduce conflict, and resolves the conflict in favor
of a reduce over a shift action
SOLUTION
Yace tool is used to create a LALR (1) parser. This parser can detect
the conflicts but to resolve the conflicts it actually prefers shift over
reduce action.
Hence (C) is correct option.
Question. 24
Assume the conflicts part (a) of this question are resolved and an
LALR(1) parser is generated for parsing arithmetic expressions as
per the given grammar. Consider an expression 3 # 2 + 1. What
precedence and associativity properties does the generated parser
realize?
(A) Equal precedence and left associativity; expression is evaluated
to 7
(B) Equal precedence and right associativity, expression is evaluated
to 9
(C) Precedence of 'x' is higher than that of +, and both operators
are left associative; expression is evaluated to 7
Page 15
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
(D) Precedence of ' # ' is higher than that of # , and both operators
are left associative; expression is evaluated to 9
SOLUTION
" S*E
"E
" F+E
"F
" id
S " S * .E
(ii)
E " F. + E
(iii)
E " F + .E
Given the items above, which two of them will appear in the same set
in the canonical sets-of-items for the grammar?
(A) (i) and (ii)
SOLUTION
Page 16
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
Question. 26
SOLUTION
Non Terminal
id
S " FR
F " id
R ") S R "!
R "!
Question. 27
(D) 2 3 4 + *
SOLUTION
Input string 2 ) 3 + 4
Page 17
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
S " ER
FR
idR
{print (2)}
id ) ER
{print ())}
id ) F + ER {print (+)}
id ) id + ER {print (3)}
id ) id + FR
id ) id + idR {print (4)}
id ) id + id
So 2 )+ 3 4 are printed
Hence (B) is correct option.
Question. 28
for
for
if i
#i
}
}
}
Which one to the following false?
(A) The code contains loop-in variant computation
(B) There is scope of common sub-expression elimination in this code
(C) There is scope strength reduction in this code
(D) There is scope of dead code elimination in this code
SOLUTION
All the statements are true except option (D) since there is no dead
code to get eliminated.
Hence (D) is correct option.
Question. 29
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
L = (ai bi | i ! j}?
(A) S " AC | CB
C " aCb | a | b
A " aA |
B " Bb |
(B) S " aS | Sb | a | b
(D) S " AC | CB
C " aCb | !
A " aA | a
B " bB | b
SOLUTION
The grammar
S " AC CB
C " aCb !
A " aA a
B " bB b
Consider string aaabb
S " AC
AaCb
AaaCbb
Aaabb
aaabb
But string aabb
S " AC
And this string is not derivable.
Hence (D) is correct option.
Question. 30
SOLUTION
It is very clear from the previous solution that the no. of steps required
Page 19
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
depend upon the no. of a's & b's which ever is higher & exceeds by 2
due to S " AC CB & C "!
So max (l, m) + 2
Hence (A) is correct option.
YEAR 2007
Question. 31
SOLUTION
Clearly LR & LALR are not top down they are bottom up passers.
Also not operator precedence parser.
But yes recursive descent parser is top down parser. Starts from start
symbol & derives the terminal string.
Hence (A) is correct option.
Question. 32
SOLUTION
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
Question. 33
SOLUTION
= a+b
= c+d
= e t2
= t1 t 2
Assume that all operands are initially in memory. The final value of
the computation should be in memory. What is the minimum number
of MOV instructions in the code generated for this basic block?
(A) 2
(B) 3
(C) 5
(D) 6
Page 21
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
SOLUTION
Question. 35
(B) aabbbb
(C) aabbab
(D) abbbba
SOLUTION
aabbab
S " aB
" aaBB
" aabSB
" aabbAB
" aabbab
Hence (C) is correct option.
Page 22
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
Question. 36
For the correct answer string to Q. 9 how many derivation trees are
there?
(A) 1
(B) 2
(C) 3
(D) 4
SOLUTION
Question. 37
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
SOLUTION
Handles are the part of sentential form, & they are identified as the
right side of any given production which will be used for reduction in
the net step.
Hence (D) is correct option.
Question. 38
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
SOLUTION
I.
CS Topicwise 2001-2010
Compiler Design
www.gatehelp.com
whose complex are less than (n3)
II
(B) I and IV
SOLUTION
I.
SOLUTION
Symbol table is used for storing the information about variables and
their attributes by compiler.
Hence (B) is correct option.
Question. 43
www.gatehelp.com
CS Topicwise 2001-2010
Compiler Design
SOLUTION
Given grammar
S " aSA
S " bS
S "c
This grammar is not ambiguous so it is LL (1) also LR (1) grammar
since all grammars are LR (1).
Hence (C) is correct option.
**********
Page 27