Categories

Compiler design-1

CSE - Compiler design - Questions and solutions-1
Question:-
Option (A)
n1 is necessarily less than n2
Option (B)
n1 is necessarily equal to n2
Option (C)
n1 is necessarily greater than n2
Option (D)
None of the above
Correct Option:
B
Question Solution:
SLR parser has n1 states for a grammar G LALR parser has n2 states for a grammar G. The states of SLR and LALR parser are the states of corresponding states in a deterministic finite automata which recognizes the viable prefixes and both deterministic finite automate contains the equal number of states so n1 = n2.
Question:-
Option (A)
Always be evaluated
Option (B)
Be evaluated only if the definition is L-attributed
Option (C)
Be evaluated only if the definition has synthesized attributes
Option (D)
Never be evaluated
Correct Option:
C
Question Solution:
Every S(Synthesized) - attributed definition is L - attributed. For implementing inherited attributed during bottom-up parsing , extends to some, but not LR grammars. Consider the following example
Production Semantic Rule
S ® L L. count: = 0
L ® L11 L. count: = L. count: +1
L ® E print (L. count)

In the example above the no terminal L in L ® E inherits the count of the number of 1’s generated by S. Since the production L ® E is the first that a bottom-up burser would reduce by, the translator at the time can’t know the number of 1’s in the input. So in a bottom-up evaluation of a syntax directed definition, inherits attributed can’t be evaluated if the definition is L-attributed in the given example. So we can say that L-attributed definition is based on simple LR(1) grammar, but it can’t be implemented always but inherited attributes can be evaluated only if the definition has synthesized attributes.
Question:-
Option (A)
9 + 5 + 2
Option (B)
9 5 + 2 +
Option (C)
9 5 2 + +
Option (D)
+ + 9 5 2
Correct Option:
B
Question Solution:
For the input ‘9 + 5 + 2’ the translation scheme is 95 + 2 + shown below:

Question:-
Option (A)
X = Y + Z
Option (B)
t1 = Y + Z; Xt1
Option (C)
t1 = Y; t2 = t1 + Z; X = t2
Option (D)
t1 = Y; t2 = Z; t3 = t1 + t2; X = t3
Correct Option:
D
Question Solution:
3-address sequence code generated by definition for X = Y + Z is clearly.
t1 = Y; t2 = Z; t3 = t1 + t2 ; X = t3
because in 3-address we have two operands and one operator and these operands are different.
Question:-
Option (A)
115, 220
Option (B)
25, 220
Option (C)
25, 15
Option (D)
115, 105
Correct Option:
D
Question Solution:
In static scoping, the resolutions of variable and values is done at compile time only
So P(x) is called with P(100 + 5) = P(105)
So print (x) will give value 105.