GATE 2005
203 questions
MCQ 2031-mark 203
Questions 141–145 of 203
Consider the context-free grammar
where E is the starting symbol, the set of terminals is , and the set of nonterminals is {E}. Which of the following terminal strings has more than one parse tree when parsed according to the above grammar?
where E is the starting symbol, the set of terminals is , and the set of nonterminals is {E}. Which of the following terminal strings has more than one parse tree when parsed according to the above grammar?
Answer: (A)
The rule has no precedence or associativity, so a chain of signs can be grouped in more than one way.
(A) has three with no brackets. It can be grouped as , , and others, so it has 5 parse trees.
(B), (C), (D): every use of must be written with its own brackets, so the shape is forced.
(B) has one , with id on the left and a bracketed product on the right. Each product is inside brackets, so there is one tree.
(C) is the same, with the bracketed product on the left.
(D) is where E is . That inner part is with E = id and E = id+id, and id+id has only one tree. So there is one tree.
So only (A) has more than one parse tree.
Answer: (A)
(A) has three with no brackets. It can be grouped as , , and others, so it has 5 parse trees.
(B), (C), (D): every use of must be written with its own brackets, so the shape is forced.
(B) has one , with id on the left and a bracketed product on the right. Each product is inside brackets, so there is one tree.
(C) is the same, with the bracketed product on the left.
(D) is where E is . That inner part is with E = id and E = id+id, and id+id has only one tree. So there is one tree.
So only (A) has more than one parse tree.
Answer: (A)
Consider the following expression grammar. The semantic rules for expression calculation are stated next to each grammar production.
Assume the conflicts of this question are resolved usinf yacc tool and an LALR(1) parser is generated for parsing arithmetic expressions as per the given grammar. Consider an expression 3 x 2 + 1. What precedence and associativity properties does the generated parser realize?
E → number E.val = number.val
| E ‘+’ E E(1).val = E(2).val + E(3).val
| E ‘×’ E E(1).val = E(2).val × E(3).val Assume the conflicts of this question are resolved usinf yacc tool and an LALR(1) parser is generated for parsing arithmetic expressions as per the given grammar. Consider an expression 3 x 2 + 1. What precedence and associativity properties does the generated parser realize?
Answer: (B)
The grammar is ambiguous, and no precedence or associativity is declared. In yacc, a shift/reduce conflict is then resolved in favour of shift.
Parse :
- Shift 3, reduce to E. Shift . Shift 2, reduce to E. The stack is and the next input is .
- Conflict: reduce or shift . Yacc shifts.
- Shift , shift 1, reduce to E. Reduce to E, which is . Then reduce to E, which is .
The same shift choice is made for every pair of operators, so both operators have the same precedence and the grouping goes from the right: .
Option (A) (equal precedence, left associative, value 7) would need the parser to reduce first, but yacc shifts here. Option (C) needs declared with higher precedence than , and option (D) needs the reverse. No such declaration is made, so the default shift rule decides.
Answer: (B)
Parse :
- Shift 3, reduce to E. Shift . Shift 2, reduce to E. The stack is and the next input is .
- Conflict: reduce or shift . Yacc shifts.
- Shift , shift 1, reduce to E. Reduce to E, which is . Then reduce to E, which is .
The same shift choice is made for every pair of operators, so both operators have the same precedence and the grouping goes from the right: .
Option (A) (equal precedence, left associative, value 7) would need the parser to reduce first, but yacc shifts here. Option (C) needs declared with higher precedence than , and option (D) needs the reverse. No such declaration is made, so the default shift rule decides.
Answer: (B)
Consider the following expression grammar. The semantic rules for expression calculation are stated next to each grammar production.
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?
| E→ number | E.val = number.val |
| | E ‘+’ E | E(1).val = E(2).val + E(3).val |
| | E ‘×’ E | E(1).val = E(2).val × E(3).val |
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?
Answer: (C)
The grammar is . It is ambiguous, since it has no precedence or associativity rules.
Build the LR(1) items starting from .
The two states that contain or also contain and . So on + and * they can both reduce and shift. That makes a shift-reduce (SR) conflict on + and on * in each of the two states, 4 conflicts in all. The rule causes none.
No state has two different completed items, so there is no reduce-reduce conflict (this rules out B). The LALR(1) tables are the same as the LR(1) tables here, since no states have the same core with different look aheads.
Yacc detects a shift-reduce conflict and by default resolves it in favour of the shift. (For a reduce-reduce conflict it picks the production that comes first in the grammar.) Yacc does not eliminate recursion (A is wrong), and it does not prefer reduce (D is wrong).
For example, in , when the handle is on the stack and + is next, yacc shifts and does not reduce. So the whole input is pushed, then is reduced first and last. All operators then have equal precedence and group to the right.
Answer: (C)
Build the LR(1) items starting from .
The two states that contain or also contain and . So on + and * they can both reduce and shift. That makes a shift-reduce (SR) conflict on + and on * in each of the two states, 4 conflicts in all. The rule causes none.
No state has two different completed items, so there is no reduce-reduce conflict (this rules out B). The LALR(1) tables are the same as the LR(1) tables here, since no states have the same core with different look aheads.
Yacc detects a shift-reduce conflict and by default resolves it in favour of the shift. (For a reduce-reduce conflict it picks the production that comes first in the grammar.) Yacc does not eliminate recursion (A is wrong), and it does not prefer reduce (D is wrong).
For example, in , when the handle is on the stack and + is next, yacc shifts and does not reduce. So the whole input is pushed, then is reduced first and last. All operators then have equal precedence and group to the right.
Answer: (C)
Consider the grammar:
S (S) | a
Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be n1, n2 and n3 respectively. The following relationship holds good:
S (S) | a
Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be n1, n2 and n3 respectively. The following relationship holds good:
Answer: (B)
Add . The LR(0) states are:
I0:
I1 = goto(I0, S):
I2 = goto(I0, '('):
I3 = goto(I0, a):
I4 = goto(I2, S):
I5 = goto(I4, ')'):
From I2, '(' goes back to I2 and a goes to I3. So the SLR(1) parser has states, and the LALR(1) parser has the same 6 states with lookaheads added: .
In LR(1), the state after the first '(' has lookahead for S, but the state after a later '(' has lookahead ')'. The lookaheads are different, so the states I2, I3, I4 and I5 each split in two (one with , one with ')'). That gives 6 + 4 = 10 states: .
LALR merges the states with the same core, so they come back to 6.
So .
Answer: (B)
I0:
I1 = goto(I0, S):
I2 = goto(I0, '('):
I3 = goto(I0, a):
I4 = goto(I2, S):
I5 = goto(I4, ')'):
From I2, '(' goes back to I2 and a goes to I3. So the SLR(1) parser has states, and the LALR(1) parser has the same 6 states with lookaheads added: .
In LR(1), the state after the first '(' has lookahead for S, but the state after a later '(' has lookahead ')'. The lookaheads are different, so the states I2, I3, I4 and I5 each split in two (one with , one with ')'). That gives 6 + 4 = 10 states: .
LALR merges the states with the same core, so they come back to 6.
So .
Answer: (B)
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)