GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 131–135 of 185
Which of the following are decidable?
I. Whether the intersection of two regular languages is infinite
II. Whether a given context-free language is regular
III. Whether two push-down automata accept the same language
IV. Whether a given grammar is context-free
I. Whether the intersection of two regular languages is infinite
II. Whether a given context-free language is regular
III. Whether two push-down automata accept the same language
IV. Whether a given grammar is context-free
Answer: (B)
I. Decidable. The intersection of two regular languages is regular, and a DFA for it comes from the product of the two DFAs. The language is infinite exactly when some state reachable from the start state lies on a cycle and can reach a final state. This can be checked on the DFA graph.
II. Undecidable. Whether a given context-free language is regular cannot be decided by any algorithm (a standard result for CFLs).
III. Undecidable. Equivalence of two PDAs, that is of two CFLs, cannot be decided. Even asking whether a CFL equals is undecidable.
IV. Decidable. A grammar is context-free when every production has the form with a single variable and . A Turing machine can check each production.
So only I and IV are decidable.
Answer: (B)
II. Undecidable. Whether a given context-free language is regular cannot be decided by any algorithm (a standard result for CFLs).
III. Undecidable. Equivalence of two PDAs, that is of two CFLs, cannot be decided. Even asking whether a CFL equals is undecidable.
IV. Decidable. A grammar is context-free when every production has the form with a single variable and . A Turing machine can check each production.
So only I and IV are decidable.
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)
Which of the following are true?
I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can be implemented with static storage allocation
II. Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has nesting of procedures/functions
III. Recursion in programming languages cannot be implemented with dynamic storage allocation
IV. Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records
V. Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records
I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can be implemented with static storage allocation
II. Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has nesting of procedures/functions
III. Recursion in programming languages cannot be implemented with dynamic storage allocation
IV. Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records
V. Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records
Answer: (A)
I. False. Recursion needs a fresh activation record for every call, and the number of calls is not known at compile time. So static allocation cannot work, even without globals and nesting.
II. True. Access links (or a display) are used to reach the variables of enclosing procedures. They are needed only when procedures can be nested. Without nesting, a non-local name is a global and is reached directly.
III. False. Recursion is implemented with dynamic allocation: each call pushes a new activation record.
IV. False. Nesting and recursion work with a stack of activation records. Access links connect a record to the records of its enclosing procedures, so no heap is needed.
V. True. A function returned as a result may still use the local variables of the function that returned it. Those locals must live after the call ends, but a stack pops the activation record on return. So a stack alone is not enough.
Only II and V are true.
Answer: (A)
II. True. Access links (or a display) are used to reach the variables of enclosing procedures. They are needed only when procedures can be nested. Without nesting, a non-local name is a global and is reached directly.
III. False. Recursion is implemented with dynamic allocation: each call pushes a new activation record.
IV. False. Nesting and recursion work with a stack of activation records. Access links connect a record to the records of its enclosing procedures, so no heap is needed.
V. True. A function returned as a result may still use the local variables of the function that returned it. Those locals must live after the call ends, but a stack pops the activation record on return. So a stack alone is not enough.
Only II and V are true.
Answer: (A)