You are on page 1of 4

Compiler Design Test 1

Number of Questions: 25 Section Marks: 30

Directions for questions 1 to 25: Select the correct alterna- 8. Which of the following action is not performed by an
tive from the given choices. LL parser?
1. Which of the following implementation of Lexical ana- (i) Matching top of the parser stack with next input
lyzer is easy to modify and faster? token.
(i) Lexical-analyzer by hand (ii) Predicting a production and apply it in a deriva-
(ii) Lexical-analyzer generator tion step.
(A) (i) only (B) (ii) only (iii) Accept and terminate the parsing of a sequence of
(C) Both (i) and (ii) (D) Neither (i) nor (ii) tokens.
(iv) Reporting an error message.
2. Which of the following tasks are performed by a
(A) (i), (iv) (B) (iii), (iv)
Lexical analyzer?
(C) (ii), (iii) (D) None of the above
(i) Identification of Lexemes
(ii) Stripping-out Comments 9. Consider the following grammar:
(iii) Stripping-out white spaces S → AcB
(iv) Correlating error messages generated by the com- A → aA | e
piler with the source program. B → bBS | e
(A) (i), (ii) (B) (i), (ii), (iii) For this grammar, FOLLOW(B) is
(C) (iii), (iv) (D) (i), (ii), (iii), (iv) (A) {a, $} (B) {a, c}
(C) {a, c, $} (D) {c}
3. Which of the following statement is TRUE?
(i) Every LL(k) language is deterministic context- 10. Which of the following statements is FALSE?
free language. (A) Every LR(0) grammar is in SLR (1).
(ii) Every deterministic context free language is LL(k) (B) Every SLR (1) grammar is in LR (0).
language. (C) Every SLR(1) grammar is a canonical LR(1)
(A) (i) only (B) (ii) only grammar.
(C) Both (i) and (ii) (D) Neither (i) nor (ii) (D) Every LR(1) grammar is not necessarily SLR(1).
4. Which of the following grammar is LL(3) grammar but 11. Which of the following grammars for {an bn+k| n, k ∈ N}
not LL(2) and not LL(1)? are in LL(1)?
(A) S → aS|b (B) S → aaS | ab| b (i) S → PQ
(C) S → aaaS | aab | ab | b (D) None of the above P → aPb | e
Q → bQ | e
5. On errorneous input,
(ii) S → aSb| T
(A) LALR parser makes more moves than LR parser.
T → bT| e
(B) LR parser makes more moves than LALR parser.
(A) (i) only (B) (ii) only
(C) LALR parser makes equal number of moves as an
(C) Both (i) and (ii) (D) Neither (i) nor (ii)
LR parser.
(D) Unpredictable 12. Based on which of the following parameters does a
6. What kind of conflict does the following grammar LR(k) parser performs reduction?
leads to? (i) The complete left context
S→A (ii) The reducible phrase itself
A → xA | yA | y (iii) The k-terminal symbols to its right
(A) Shift-reduce conflict (A) (i), (iii) only (B) (ii), (iii) only
(B) Reduce-reduce conflict (C) (i), (ii),(iii) (D) (i) only
(C) No conflict exist 13. After removing left-recursion from the following gram-
(D) Both (A) and (B) mar the resultant grammar will be:
7. Which of the following statement(s) is/are TRUE? P→P+Q|Q
(i) Synthesized attributes may be calculated only Q → int | (P)
from attributes of children. (A) P → Q + Q
(ii) Inherited attributes may be calculated only from Q → int | (P)
attributes of parents. (B) P → QP1
(A) (i) only (B) (ii) only P1 → +QP1 | e
(C) Both (i) and (ii) (D) Neither (i) nor (ii) Q → int | (P)
3.146 | Compiler Design Test 1

(C) P → QP1 (C) {T → T * • F F → • (E) F → • id}


P1 → +QP1 (D) {T → • T * F}
Q → int | (P) 20. Consider the following grammar:
(D) P → Q + P1 Stmts' → Stmts
P1 → Q + P1 | e Stmts → Stmt | Stmts; Stmt
Q → int | (P) Stmt → Id = E | Print E
14. Consider the following grammar: E → Id | E + E | (E)
S → PQRt What does the closure set of the following rule will in-
P → a| b|e clude?
Q→c|d|e Stmts' → • Stmts
R→e|f (A) Stmts' → • Stmts
For this grammar, FIRST(S) is (B) Stmt’s → • Stmts
(A) {a, b, c, d, e, f, e} (B) {a, b, c, d, e, f} Stmts → • Stmt
(C) {a, b, t} (D) {a, b, c, d} Stmts → • Stmts ; Stmt
15. Consider the following grammar, G: Stmt → • id = E
S → (L) | a Stmt → • Print E
L → L, S | S (C) Stmts' → • Stmts
Which of the following is TRUE? Stmts → • Stmt
(i) G is not suitable to be parsed using recursive de- Stmts → • Stmts ; Stmt
scent parsing. Stmt → • id = E
(ii) FIRST (S) = FIRST (L) = {(, a} Stmt → • Print E
(A) (i) only (B) (ii) only E → • id
(C) Both (i) and (ii) (D) Neither (i) nor (ii) E→•E+E
E → • (E)
16. Consider the following configuration:
(D) Stmts' → • Stmts
A → X1 X2 ... Xj, a
Stmts → • Stmt
Then which of the following is TRUE?
Stmts → • Stmts; Stmt
(i) With SLR(1) parsing we would reduce if the next
token was any of those in FOLLOW (A). 21. For the following grammar:
(ii) With LR(1) parsing, we reduce only if the next to- A → BCD
ken is exactly ‘a’. B → Bb | e
(A) (i) only (B) (ii) only C → cC| e
(C) Both (i) and (ii) (D) Neither (i) nor (ii) D→d
17. Which of the following is FALSE? What is FOLLOW (B)?
(A) An LALR(1) grammar is one which allows a (A) {b, $} (B) {b, c}
LALR(1) parse table to be constructed with no (C) {b, d} (D) {b, c, d}
more than one action per cell. 22. Consider the following grammar, G:
(B) LALR(1) grammar allow a wider set of structures S → AB
than SLR(1) grammar. A → aAa | e
(C) LALR(1) grammars do not handle some cases al- B → bBb | e
lowed in LR(1) grammars. Which of the following statements is TRUE?
(D) None of these (i) G is ambiguous.
18. Identify the correct statements from the following: (ii) G is not in LL(1).
(i) LALR(1) is a subset of LR(1). (A) (i) only (B) (ii) only
(ii) LALR(1) is a super set of SLR(1). (C) Both (i) and (ii) (D) Neither (i) nor (ii)
(iii) A grammar that is not LR(1) may LALR(1).
(A) (i), (ii) only (B) (ii), (iii) only 23. Consider the following grammar:
(C) (i), (iii) only (D) (i), (ii), (iii) S → a | AbC
19. Based on the following grammar, A→a
E → E + T| T C → A| c
T→T*F|F Which of the following is FALSE?
F → (E) | id (A) FOLLOW(S) = {$}
What is the closure ({T → T • * F}? (B) FOLLOW(A) = {b, $}
(A) {T → T • * F} (C) FOLLOW(C) = {$}
(B) {T → T • * F T → • F} (D) The grammar is in SLR(1).
Compiler Design Test 1 | 3.147

24. Which of the following is TRUE about LR-attributed 25. Which of the following conditions must hold to call a
grammar? grammar is in SLR(1)?
(i) Attributes can be evaluated in LR parsing (i) For any production A → x.yz in the set, with ter-
(ii) LR-Attribute grammars are a subset of L-attribut- minal y, there is no complete item B → W. In that
ed grammars set with ‘y’ in FOLLOW (B).
(iii) LR-attribute grammars are a subset of S-attributed (ii) For any two complete productions A → X and
grammars. B → Y. In the set, the follow sets must be disjoint.
(A) (i), (ii) only (B) (ii), (iii) only (A) (i) only (B) (ii) only
(C) (i), (iii) only (D) (i), (ii), (iii) only (C) Both (i) and (ii) (D) Neither (i) nor (ii)

Answer Keys
1. B 2. D 3. A 4. C 5. A 6. A 7. A 8. D 9. C 10. B
11. A 12. C 13. B 14. B 15. C 16. C 17. D 18. A 19. A 20. B
21. D 22. B 23. D 24. A 25. C

Hints and Explanations


1. Lexical-analyzer generator makes the code easier to A→aA|e
modify a lexical analyzer (Since we have to rewrite B → bBS| e
only the affected patterns). FOLLOW (B) = FIRST (S) U FOLLOW (S)
It also speeds up the process of implementing the ana- FIRST (S) = {FIRST (A) – e} ∪ {c} = {a, c}
lyzer (Since the programmer specifies the software at Follow (S) = {$}
the very high level of patterns and relies on generator ∴ Follow (B) = {a, c, $} Choice (C)
to produce code). Choice (B) 10. LR(0) ⊂ SLR (1) ⊂ CLR (1) Choice (B)
3. LL(k) languages are Deterministic CFLs but there are 11. Given language:
Deterministic CFL’s that are not LL(k). Choice (A) {an bn+k | n, k ∈ N}
4. S → aS/b is in LL(1) (i) S → PQ
( By looking single look-ahead, we can decide P → aPb| e
whether to shift or reduce). Q → bQ| e
S → aaS|ab|b is in LL(2). This is LL(1) (No ambiguity, no left recursion).
S → aaaS| aab| ab| b is in LL(3). Choice (C) To derive ‘ab’:
5. On errorneous input, LALR parser makes more moves S → PQ
(i.e., reductions) than the LR parser. Choice (A) → aPbQ
→ ab.
6. Given grammar, (ii) S → aSb | T
S→A T → bT | e
A → xA | yA | y To derive ‘ab’ :
This grammar has shift-reduce conflict. S → aSb
A → y . A || leads to shift → aTb
A → y. || leads of reduce → abTb (T → bT)
These productions leads to shift-reduce conflict. (Back track)
Choice (A) (or)
7. Synthesized attributes are calculated from attributes of S → aSb
children. → aTb
Inherited attributes are calculated from attributes of → ab(T → e)
parents or siblings. Choice (A) ∴ (ii) not in LL(1) (as which production to use in
some derivation is unknown). Choice (A)
8. An LL parser perform the following actions:
(i) Match (ii) Predict 12. All the three parameters are required during reduction
in LR(k) parser. Choice (C)
(iii) Accept (iv) Error Choice (D)
13. Given grammar,
9. Given grammar,
P→P+Q|Q
S → AcB
3.148 | Compiler Design Test 1

Q → int | (P) 22. Given grammar G:


To avoid left-recursion, replace recursive term with S → AB
other terms and introduce a new variable as shown be- A → aAa| e
low. B → bBb | e
P → P + Q | Q can be written as G is not ambiguous.
P → QP1 ( To derive any string only one possible method exists)
P1 → + QP1 | e Choice (B) G is not in LL(1).
14. FIRST (S) = {FIRST (P) – e} U {FIRST (Q) – e} U To derive aa.
{FIRST (R)} S → AB
= {a, b, c, d, e, f}. Choice (B) → aA aB
→ aa A aaB (or) S → AB
15. Given grammar is not parsed using recursive descent
(Back track) → aA aB
parsing as the grammar is left recursive.
→ aAa B → aaB
FIRST (S) = {(, a}
→ aa → aa
FIRST (L) = FIRST (S) = {(, a} Choice (C)
∴ G is not in LL(1).
16. For the configuration, [OR]
A → X1, X2, ... Xj, a To check whether G is in LL(1) or not:
If ‘a’ is in FOLLOW(A) then reduce in SLR(1). Check whether for all A ∈ N with A → a1 | a2| ... an
If the next token is exactly ‘a’ then reduce in LR(1). being all A-Productions in G, the following holds:
Choice (C) (i) FIRST(a1),………FIRST(an) are pairwise dis-
18. SLR(1) ⊂ LALR (1) ⊂ LR(1) joint.
If a grammar is not in LR(1) then it is not in LALR(1). (ii) If ∈ ∈ FIRST(aj) for some j ∈ [1, n] then FOL-
( Conflict exist in LALR also) Choice (A) LOW (A) h FIRST (ai) = f for all 1 ≤ i ≤ n, j ≠ i.
19. Given grammar (i) is satisfied.
E→E+T|T FIRST (aAa) = {a}
T→T*F|F FIRST (e) = {e}
F → (E) | id (ii) is failed.
FIRST (aA a) = {a}
Closure ({T → T • * F}) will be {T → T • * F} only.
FOLLOW (A) = {a, b, $}
To calculate closure we add new productions to the set
These are not disjoint.
if the • is before a non-terminal.
∴ G is not in LL(1). Choice (B)
(Here • is before ‘*’ which is a terminal)
Choice (A) 23. Given grammar,
S → a | AbC
20. Given grammar,
A→a
Stmts’ → Stmts C → A|c
Stmts → Stmt | Stmts; Stmt FOLLOW (S) = {$}
Stmt → id = E| Print E FOLLOW (A) = {b} ∪ follow (C) = {b, $}
E → id | E + E | (E) FOLLOW (C) = FOLLOW (S) = {$}
Closure ({Stmts’ → • Stmts}), The grammar is not in SLR(1) , since there is reduce/
will include the productions of Stmts ( • is before reduce conflict with the productions.
Stmts) S → a.
Stmts → • Stmt A → a. Choice (D)
Stmts → •Stmts; Stmt 24. LR-attribute grammar is a special type of attribute
Now production of ‘Stmt’ will be included ( • is be- grammar. It allows the attributes to be evaluated on LR
fore Stmt) parsing.
Stmt → •id = E LR-attribute grammar ⊂ L-attributed grammar.
Stmt → • Print E Choice (B) S-attributed grammar ⊂ LR-attributed grammar.
21. Follow (B) = {b} ∪ {FIRST (C) – ∈} ∪ {FIRST (D)} Choice (A)
= {b} ∪ {c} ∪ {d} 25. (i) avoids shift-reduce conflict.
= {b, c, d} Choice (D) (ii) avoids reduce-reduce conflict. Choice (C)

You might also like