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 1–5 of 58
Consider the following C statements:
char *str1 = "Hello; /* Statement S1 */
char *str2 = "Hello;"; /* Statement S2 */
int *str3 = "Hello"; /* Statement S3 */
Which of the following options is/are correct?
char *str1 = "Hello; /* Statement S1 */
char *str2 = "Hello;"; /* Statement S2 */
int *str3 = "Hello"; /* Statement S3 */
Which of the following options is/are correct?
One or more options are correct.
Answer: (C)
— the closing double-quote of the string literal is missing. A string literal that is not closed is a lexical error, because the lexer/scanner cannot build a valid string token.
— this is a valid string literal (it has a semicolon character inside) given to a pointer. There is no error here.
— this is fine lexically and syntactically (a valid string literal and a valid statement form). But it gives a value to a pointer of the wrong type, . This is a type mismatch, so it is a semantic error (found in semantic/type analysis, not in parsing).
Answer: S1 has a lexical error, and S3 has a semantic error.
— this is a valid string literal (it has a semicolon character inside) given to a pointer. There is no error here.
— this is fine lexically and syntactically (a valid string literal and a valid statement form). But it gives a value to a pointer of the wrong type, . This is a type mismatch, so it is a semantic error (found in semantic/type analysis, not in parsing).
Answer: S1 has a lexical error, and S3 has a semantic error.
Which of the following statements is/are true?
One or more options are correct.
Answer: (C)
- (A) "LL(1) parser uses backtracking" — false. LL(1) is a predictive top-down parser and needs no backtracking. It picks the production by reading just one lookahead symbol.
- (B) "For LL(1), the grammar must be left-recursive" — false. It is the opposite. A left-recursive grammar can never be LL(1), because it would cause infinite recursion in the predictive parser. So left recursion must be removed first.
- (C) "For LL(1), the grammar must be left-factored" — true. Suppose two productions of a non-terminal begin with the same prefix. Then the parser cannot choose between them using one lookahead symbol. Left-factoring removes this problem, so a grammar that needs it is not LL(1) until it is done.
- (D) "LL(1) parsers are more powerful than SLR parsers" — false. The two classes of grammars are incomparable, and neither contains the other. Some SLR(1) grammars are not LL(1), for example any left-recursive one such as . Some LL(1) grammars are not SLR(1), for example . So LL(1) is not simply "more powerful".
Answer: (C)
- (B) "For LL(1), the grammar must be left-recursive" — false. It is the opposite. A left-recursive grammar can never be LL(1), because it would cause infinite recursion in the predictive parser. So left recursion must be removed first.
- (C) "For LL(1), the grammar must be left-factored" — true. Suppose two productions of a non-terminal begin with the same prefix. Then the parser cannot choose between them using one lookahead symbol. Left-factoring removes this problem, so a grammar that needs it is not LL(1) until it is done.
- (D) "LL(1) parsers are more powerful than SLR parsers" — false. The two classes of grammars are incomparable, and neither contains the other. Some SLR(1) grammars are not LL(1), for example any left-recursive one such as . Some LL(1) grammars are not SLR(1), for example . So LL(1) is not simply "more powerful".
Answer: (C)
Which of the following grammars is/are ambiguous?
One or more options are correct.
Answer: (B), (C)
A grammar is ambiguous if some string has two different parse trees.
- : it makes only . Each use of adds one a and one b around the middle, and ends it, so every string has one parse tree. Unambiguous.
- : the string has two parse trees. In one, the root is and the right is , which gives . In the other, the root is and the left is , which gives . Ambiguous.
- : the string has two parse trees. One uses (the root has children and ), and the other uses (the root has children and ), with the inner going to in both. Ambiguous.
- : it is only right recursive, and has exactly one derivation (use times, then ). Unambiguous.
Answer: (B), (C)
- : it makes only . Each use of adds one a and one b around the middle, and ends it, so every string has one parse tree. Unambiguous.
- : the string has two parse trees. In one, the root is and the right is , which gives . In the other, the root is and the left is , which gives . Ambiguous.
- : the string has two parse trees. One uses (the root has children and ), and the other uses (the root has children and ), with the inner going to in both. Ambiguous.
- : it is only right recursive, and has exactly one derivation (use times, then ). Unambiguous.
Answer: (B), (C)
Consider the canonical parsing of the grammar below using terminals and non-terminals with as the start symbol.
Which one of the following options gives the number of shift-reduce conflicts that will occur in the ACTION table?
Which one of the following options gives the number of shift-reduce conflicts that will occur in the ACTION table?
Answer: (D)
Build the LR(0) canonical collection for . A state has a shift-reduce conflict when it holds a complete item (reduce) and also a shift item on some terminal.
Going through the automaton, these states each have a reduce item together with a shift item:
- The initial state : reduce and shift on .
- The state reached after , : reduce and shift on .
- The state on that loops to itself, : reduce and shift on .
- The state on that loops to itself, : reduce and shift on .
- The state on that loops to itself (from ), : reduce and shift on .
So there are exactly states with a conflict. The other states, such as those after fully reducing or , have only one complete item and no shift item to compete with it.
Answer: 5
Going through the automaton, these states each have a reduce item together with a shift item:
- The initial state : reduce and shift on .
- The state reached after , : reduce and shift on .
- The state on that loops to itself, : reduce and shift on .
- The state on that loops to itself, : reduce and shift on .
- The state on that loops to itself (from ), : reduce and shift on .
So there are exactly states with a conflict. The other states, such as those after fully reducing or , have only one complete item and no shift item to compete with it.
Answer: 5
Which of the following statement(s) is/are TRUE while computing and during top-down parsing by a compiler?
One or more options are correct.
Answer: (A), (D)
(A) True. If is a production, can derive the empty string, so is put in .
(B) False. The end marker is not the first symbol of anything a nonterminal derives, so it is never put in a First set. It appears only in Follow sets.
(C) False. is not an input symbol that can come after , so it is never put in any Follow set. Follow sets hold only terminals and .
(D) True. The start symbol derives the whole input, and the end marker comes right after the whole input. So is put in . ( may also be followed by other terminals if it appears on some right-hand side.)
Answer: (A), (D)
(B) False. The end marker is not the first symbol of anything a nonterminal derives, so it is never put in a First set. It appears only in Follow sets.
(C) False. is not an input symbol that can come after , so it is never put in any Follow set. Follow sets hold only terminals and .
(D) True. The start symbol derives the whole input, and the end marker comes right after the whole input. So is put in . ( may also be followed by other terminals if it appears on some right-hand side.)
Answer: (A), (D)
Keep practising