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 31–35 of 58
Which of the following statements are TRUE?
I There exist parsing algorithms for some programming languages whose complexities are less than .
II A programming language which allows recursion can be implemented with static storage allocation.
III No L-attributed definition can be evaluated in the framework of bottom-up parsing.
IV Code improving transformations can be performed at both source language and intermediate code level.
I There exist parsing algorithms for some programming languages whose complexities are less than .
II A programming language which allows recursion can be implemented with static storage allocation.
III No L-attributed definition can be evaluated in the framework of bottom-up parsing.
IV Code improving transformations can be performed at both source language and intermediate code level.
Answer: (B)
I is true. Parsers for programming languages (LL(1), LR(1), LALR(1) parsers) read the input in one pass and take time, which is less than . Only a general CFG parser such as CYK needs .
II is false. Static allocation fixes one memory location for each variable at compile time, so there is no place for a new copy of the local variables at every recursive call. Recursion needs stack (dynamic) allocation.
III is false. Every S-attributed definition is also L-attributed, and S-attributed definitions are evaluated easily during bottom-up parsing. So some L-attributed definitions can be evaluated bottom-up.
IV is true. Code-improving transformations can be done on the source (for example, constant folding or loop changes on the syntax tree) and on the intermediate code (for example, common subexpression removal).
So I and IV are true.
Answer: (B)
II is false. Static allocation fixes one memory location for each variable at compile time, so there is no place for a new copy of the local variables at every recursive call. Recursion needs stack (dynamic) allocation.
III is false. Every S-attributed definition is also L-attributed, and S-attributed definitions are evaluated easily during bottom-up parsing. So some L-attributed definitions can be evaluated bottom-up.
IV is true. Code-improving transformations can be done on the source (for example, constant folding or loop changes on the syntax tree) and on the intermediate code (for example, common subexpression removal).
So I and IV are true.
Answer: (B)
An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if
Answer: (B)
LALR(1) states are made by merging LR(1) states that have the same core (the same items, ignoring look aheads). The look aheads of the merged items are united.
Say the LR(1) parser has two states with items and . Their cores are the same, so they merge into one state with the item .
A shift-reduce conflict on a symbol b needs a state with a shift item and a reduce item:
(shift on b) and (reduce on b).
- If an LR(1) state has this conflict, the merged LALR(1) state keeps both items (merging only adds look aheads), so the conflict stays.
- If a merged LALR(1) state has this conflict, the shift item is in every LR(1) state of the merge, because they all share the same core. The reduce item with look ahead b came from at least one of those LR(1) states, and that state also has the shift item. So it already had the conflict.
So LALR(1) has an S-R conflict if and only if LR(1) has one (merging can only add reduce-reduce conflicts). SLR(1) and LR(0) reduce on larger sets of symbols, so they can have S-R conflicts that LALR(1) does not. Option D is about a different kind of conflict.
Answer: (B)
Say the LR(1) parser has two states with items and . Their cores are the same, so they merge into one state with the item .
A shift-reduce conflict on a symbol b needs a state with a shift item and a reduce item:
(shift on b) and (reduce on b).
- If an LR(1) state has this conflict, the merged LALR(1) state keeps both items (merging only adds look aheads), so the conflict stays.
- If a merged LALR(1) state has this conflict, the shift item is in every LR(1) state of the merge, because they all share the same core. The reduce item with look ahead b came from at least one of those LR(1) states, and that state also has the shift item. So it already had the conflict.
So LALR(1) has an S-R conflict if and only if LR(1) has one (merging can only add reduce-reduce conflicts). SLR(1) and LR(0) reduce on larger sets of symbols, so they can have S-R conflicts that LALR(1) does not. Option D is about a different kind of conflict.
Answer: (B)
Match the following:
Answer: (C)
- E (identifiers declared before use): the name in the declaration must be the same as the name in the use, which is the "copy" language with . So E - R.
- F (formal and actual parameters agree in number): the a's and c's must be equal in number, and so must the b's and d's, as in . So F - P.
- G (arithmetic expressions with matched parentheses): in Q, d and f work as "(" and ")" around an expression, every d gets its f, b and c are operators and g is an operand. So G - Q.
- H (palindromes): puts the same symbol at both ends and grows from the middle, so it gives even length palindromes. So H - S.
Answer: (C)
- F (formal and actual parameters agree in number): the a's and c's must be equal in number, and so must the b's and d's, as in . So F - P.
- G (arithmetic expressions with matched parentheses): in Q, d and f work as "(" and ")" around an expression, every d gets its f, b and c are operators and g is an operand. So G - Q.
- H (palindromes): puts the same symbol at both ends and grows from the middle, so it gives even length palindromes. So H - S.
Answer: (C)
Which of the following describes a handle (as applicable to LR-parsing) appropriately?
Answer: (D)
In bottom-up (LR) parsing, the parser builds a rightmost derivation in reverse. At each step it reduces some part of the current sentential form to a non-terminal. The handle is that part, i.e. the right-hand side of the production that is reduced in the next step, together with its position in the sentential form.
Example with and the string . The reductions are . The handles, in order, are the first , then , then . The handle changes at every step.
Option (A) gives only a position, with no production. Option (B) gives only a non-terminal, but one non-terminal can have many productions. Option (C) talks about a reduction in a future step, but the handle is for the next reduction. Option (D) gives the production for the next reduction and where its right-hand side is found.
Answer: (D)
Example with and the string . The reductions are . The handles, in order, are the first , then , then . The handle changes at every step.
Option (A) gives only a position, with no production. Option (B) gives only a non-terminal, but one non-terminal can have many productions. Option (C) talks about a reduction in a future step, but the handle is for the next reduction. Option (D) gives the production for the next reduction and where its right-hand side is found.
Answer: (D)
Consider the following grammars. Names representing terminals have been specified in capital letters.
Which one of the following statements is true?
Which one of the following statements is true?
Answer: (D)
A regular grammar must be right linear (each production is or , with x a string of terminals) or left linear ( or ).
The languages are regular. For there is a right linear grammar (w = WHILE, o = OTHER):
For , add and to it.
But the grammars as given are not regular grammars. In , the production has non-terminals in the middle and at the end, so it is neither right nor left linear. In , has a non-terminal at both ends. Both grammars are context-free but not regular.
Answer: (D)
The languages are regular. For there is a right linear grammar (w = WHILE, o = OTHER):
For , add and to it.
But the grammars as given are not regular grammars. In , the production has non-terminals in the middle and at the end, so it is neither right nor left linear. In , has a non-terminal at both ends. Both grammars are context-free but not regular.
Answer: (D)
Keep practising