Parsing · Compiler Design
58 questions · asked in 25 of 27 years (2000–2026)
MCQ 46 · MSQ 5 · NAT 71-mark 39 · 2-mark 19
2000Questions per year2026
Questions 51–55 of 58
Consider the grammar:
E E + n | E * n | n
For a sentence n + n * n, the handles in the right-sentential form of the reduction are:
E E + n | E * n | n
For a sentence n + n * n, the handles in the right-sentential form of the reduction are:
Answer: (D)
A handle is the right side that is reduced at each step of a bottom up parse. The reductions are a rightmost derivation traced in reverse, so each step must give a right-sentential form.
The rightmost derivation is . Reversing it:
- : the handle is the first , reduced by . This gives .
- : the handle is , reduced by . This gives .
- : the handle is , reduced by . This gives .
In , reducing the second alone would give , which cannot be derived (no production has E+E), so it is not a handle. The handles are n, E + n and E * n.
Answer: (D)
The rightmost derivation is . Reversing it:
- : the handle is the first , reduced by . This gives .
- : the handle is , reduced by . This gives .
- : the handle is , reduced by . This gives .
In , reducing the second alone would give , which cannot be derived (no production has E+E), so it is not a handle. The handles are n, E + n and E * n.
Answer: (D)
The grammar is not suitable for predictive-parsing because the grammar is:
Answer: (A)
The grammar is ambiguous. The empty string has many parse trees: , and also , and so on. The string also has several trees, for example and (with the second A empty).
A predictive (LL(1)) parser picks the production from one lookahead symbol using a table. For an ambiguous grammar some table cell must hold more than one production. Here , and would each hold both and (and for '('). So the parser cannot choose.
The grammar is also left-recursive because of , but left recursion can be removed by a standard rewriting. Ambiguity cannot be removed in the same way, and a grammar that is still ambiguous after the rewrite cannot be LL(1). So ambiguity is the basic reason. Right-recursion is not a problem for predictive parsing, and has two adjacent non-terminals, so it is not an operator grammar either.
Answer: (A)
A predictive (LL(1)) parser picks the production from one lookahead symbol using a table. For an ambiguous grammar some table cell must hold more than one production. Here , and would each hold both and (and for '('). So the parser cannot choose.
The grammar is also left-recursive because of , but left recursion can be removed by a standard rewriting. Ambiguity cannot be removed in the same way, and a grammar that is still ambiguous after the rewrite cannot be LL(1). So ambiguity is the basic reason. Right-recursion is not a problem for predictive parsing, and has two adjacent non-terminals, so it is not an operator grammar either.
Answer: (A)
Consider the grammar with the following translation rules and E as the start symbol.
E → E1 # T { E. value = E1 .value * T. value }
| T {E. value = T. value}
T → T1 & F { T. value = T1. value + F. value}
| F { T. value = F. value }
F → num {F. value = num. value }
Compute E.value for the root of the parse tree for the expression:2 # 3 & 5 # 6 & 4.
E → E1 # T { E. value = E1 .value * T. value }
| T {E. value = T. value}
T → T1 & F { T. value = T1. value + F. value}
| F { T. value = F. value }
F → num {F. value = num. value }
Compute E.value for the root of the parse tree for the expression:2 # 3 & 5 # 6 & 4.
Answer: (C)
By the semantic rules in the question, # means multiplication and & means addition.
Looking at the productions:
- & (+) has higher precedence than # (*), because & is farther from the start symbol.
- Both & and # are left associative.
So the expression is evaluated as
Answer: (C)
Consider the grammar shown below
In the predictive parse table. M, of this grammar, the entries M[S',e] and M[S',$] respectively are
In the predictive parse table. M, of this grammar, the entries M[S',e] and M[S',$] respectively are
Answer: (D)
FIRST(S') = {e, }.
FOLLOW(S): in , S is followed by S', so FIRST(S') without gives e. Also is in it, as S is the start symbol.
FOLLOW(S'): S' is the last symbol of , so FOLLOW(S) FOLLOW(S'). S is the last symbol of , so FOLLOW(S') FOLLOW(S). So FOLLOW(S') = FOLLOW(S) = {e, }.
Table rule: put in M[A, t] for each t in FIRST(), and, if can be empty, for each t in FOLLOW(A).
M[S', e]: e is in FIRST(), so goes in. S' can derive and e is in FOLLOW(S'), so goes in too. This cell has two productions.
M[S', ]: only (as is in FOLLOW(S'), and eS does not start with it).
So the entries are {} and {}.
Answer: (D)
FOLLOW(S): in , S is followed by S', so FIRST(S') without gives e. Also is in it, as S is the start symbol.
FOLLOW(S'): S' is the last symbol of , so FOLLOW(S) FOLLOW(S'). S is the last symbol of , so FOLLOW(S') FOLLOW(S). So FOLLOW(S') = FOLLOW(S) = {e, }.
Table rule: put in M[A, t] for each t in FIRST(), and, if can be empty, for each t in FOLLOW(A).
M[S', e]: e is in FIRST(), so goes in. S' can derive and e is in FOLLOW(S'), so goes in too. This cell has two productions.
M[S', ]: only (as is in FOLLOW(S'), and eS does not start with it).
So the entries are {} and {}.
Answer: (D)
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
Answer: (B)
The states of an SLR parser are the sets of LR(0) items (the canonical LR(0) collection).
An LALR(1) parser is built by taking the LR(1) states and merging those that have the same core, that is the same LR(0) items. So its states are exactly the canonical LR(0) states, one for each core. LALR(1) has more precise look aheads than SLR, but the number of states is the same.
So .
Answer: (B)
An LALR(1) parser is built by taking the LR(1) states and merging those that have the same core, that is the same LR(0) items. So its states are exactly the canonical LR(0) states, one for each core. LALR(1) has more precise look aheads than SLR, but the number of states is the same.
So .
Answer: (B)
Keep practising