250+ TOP MCQs on L-Attributed Definition and Answers

Compilers Multiple Choice Questions & Answers (MCQs) on “L-Attributed Definition”.

1. The grammar A ->AA |( A)| ε is not suitable for predictive-parsing because the grammar is ___________
a) Ambiguous
b) Left-recursive
c) Right-recursive
d) An operator-grammar

Answer: a
Clarification: The grammar is ambiguous.

2. Consider the grammar.

E -> E + n | E # n | n

For a sentence n + n, form of the reduction are __________
a) n, E + n and E + n # n
b) n , E + n and E + E # n
c) n , n + n and n + n # n
d) n , E + n and E # n

Answer: d
Clarification: During reduction the order is reverse. So {E ” n , E ” E + n, E ” E # n}.

3. 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.
a) n1 < n2 < n3
b) n1 = n3 < n2
c) n1 = n2 = n3
d) n1 $ n3 $ n2

Answer: b
Clarification: The no. of states for SLR(1) & LALR(1) are equal so n 1 = n3, but CLR(1) or LR(1) will have no. of states greater than LALR & LR(0) both.

4. Consider line 3. Identify the compiler’s response about this line while creating the object-module

Int main()
{ int 1,N;
Fro (I,1 N,1);
}

a) No compilation error
b) Only a lexical error
c) Only syntactic errors
d) Both lexical and syntactic errors

Answer: c
Clarification: There are no lexical errors for C because all the wrong spelled keywords.

5. 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

Answer: c
Clarification: Yacc tool is used to create a LALR(1) parser.

6. 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
d) Precedence of ‘ # ‘ is higher than that of ‘#’, and both operators are left associative; expression is evaluated to 9

Answer: b
Clarification: The grammar has equal precedence and it is also ambiguous.

7. Consider the following grammar.

S > S * E 
S > E 
E > F + E 
E > F 
F > id

Consider the following LR(0) items corresponding to the grammar above.

(i)	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)
b) (ii) and (iii)
c) (i) and (iii)
d) None of the mentioned

Answer: c
Clarification: If S -> S ): E is in LR(0) then E ” F +: E will also be there because both of them has ‘ : ‘ before E.

8. Consider the following grammar:

S > FR 
R > * S | ε 
F > id

In the predictive parser table, M , of the grammar the entries M [ S, id] and M [ R,$] respectively.
a) {S ” FR} and {R ” ε}
b) {S ” FR} and {}
c) {S ” FR} and {R ” * S}
d) {F ” id} and {R ” ε}

Answer: a
Clarification: The predictive parser table is given as. Non Terminal ) id $ S S -> FR F
F -> id R
R ->) S
R ->! R ->!
So at M [ S, id] = { S ” FR} M [ R,$] = {R “!}

9. Consider the following translation scheme.

S > ER 
R > * E{print{’ * ’);
R | f 
E > F + E{print(’ + ’); | F F > (S) | id{print(id.value);}

Here id is a taken that represents an integer and id. value represents the corresponding integer value. For an input ‘2 * 3 + 4’, this translation scheme prints?
a) 2 * 3 + 4
b) 2 * + 3 4
c) 2 3 * 4 +
d) 2 3 4 + *

Answer: b
Clarification: Input string 2 ) 3 + 4 S ” ER FR idR {print(2)} id)ER {print())} id) F+ER {print(+)}id) id + ER {print(3)} id) id ) id +id So 2 )+ 3 4 are printed.

10. Consider the following C code segment.

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

Answer: d
Clarification: All the statements are true except option last since there is no dead code to get eliminated.

Leave a Reply

Your email address will not be published. Required fields are marked *