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 26–30 of 58
What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon- and unit-production (i.e., of type and ) to parse a string with n tokens?
Answer: (B)
If the grammar has no productions and no unit productions, each reduce step swaps at least 2 symbols for 1 symbol. So every reduce cuts the symbol count on the stack by at least 1.
To go from n tokens to the one start symbol, the parser can make at most n 1 reduce moves.
Note: the paper's gloss lists A a, so it treats every rule as having at least 2 symbols on its right side. Strictly, a unit production is A B, a single variable.
Answer: n 1
To go from n tokens to the one start symbol, the parser can make at most n 1 reduce moves.
Note: the paper's gloss lists A a, so it treats every rule as having at least 2 symbols on its right side. Strictly, a unit production is A B, a single variable.
Answer: n 1
For the grammar below, a partial LL(1) parsing table is also presented along with the grammar. Entries that need to be filled are indicated as E1, E2, and E3. is the empty string, $ indicates end of input, and, | separates alternate right hand sides of productions.
The appropriate entries for E1, E2, and E3 are
The appropriate entries for E1, E2, and E3 are
Answer: (C)
Find FIRST and FOLLOW first. FIRST(S) = {a, b, ε}.
FOLLOW(S): S is the start symbol, so . From , b follows A; from , a follows A; so FOLLOW(A) = {a, b}. Since , FOLLOW(S) contains FOLLOW(A). B ends the S productions, so FOLLOW(B) = FOLLOW(S). Hence .
Rule: put in M[A, t] for every t in FIRST(α); if α can give ε, also for every t in FOLLOW(A).
- E1 = M[S, a]: because a is in its FIRST; because a is in FOLLOW(S).
- E2 = M[S, b]: because b is in its FIRST; because b is in FOLLOW(S).
- E3 = M[B, ]: , because S can give ε and is in FOLLOW(B).
Option A leaves out in E1 and E2. Option B writes "", which is not a production. Option D puts A and B productions in row S.
Answer: (C)
FOLLOW(S): S is the start symbol, so . From , b follows A; from , a follows A; so FOLLOW(A) = {a, b}. Since , FOLLOW(S) contains FOLLOW(A). B ends the S productions, so FOLLOW(B) = FOLLOW(S). Hence .
Rule: put in M[A, t] for every t in FIRST(α); if α can give ε, also for every t in FOLLOW(A).
- E1 = M[S, a]: because a is in its FIRST; because a is in FOLLOW(S).
- E2 = M[S, b]: because b is in its FIRST; because b is in FOLLOW(S).
- E3 = M[B, ]: , because S can give ε and is in FOLLOW(B).
Option A leaves out in E1 and E2. Option B writes "", which is not a production. Option D puts A and B productions in row S.
Answer: (C)
For the grammar below, a partial LL(1) parsing table is also presented along with the grammar. Entries that need to be filled are indicated as E1, E2, and E3. is the empty string, $ indicates end of input, and, | separates alternate right hand sides of productions.
The FIRST and FOLLOW sets for the non-terminals A and B are
The FIRST and FOLLOW sets for the non-terminals A and B are
Answer: (A)
FIRST sets: gives FIRST(S) = {a, b, }. Since and , FIRST(A) = FIRST(B) = {a, b, }.
FOLLOW(A): in A is followed by b, and in A is followed by a. So FOLLOW(A) = {a, b}.
FOLLOW(B): B is the last symbol of the productions of S, so FOLLOW(S) is added to FOLLOW(B).
FOLLOW(S): is in it because S is the start symbol. Also gives FOLLOW(A) FOLLOW(S), and gives FOLLOW(B) FOLLOW(S). So FOLLOW(S) = {} {a, b} FOLLOW(B), and FOLLOW(S) = FOLLOW(B) = {a, b, }.
This also fills the table: E3 = M[B, ] is , because is in FIRST(S) and is in FOLLOW(B).
Option (B) is wrong because cannot be in FIRST(A) (FIRST has only terminals and ), and FOLLOW(B) is not just {}.
Answer: (A)
FOLLOW(A): in A is followed by b, and in A is followed by a. So FOLLOW(A) = {a, b}.
FOLLOW(B): B is the last symbol of the productions of S, so FOLLOW(S) is added to FOLLOW(B).
FOLLOW(S): is in it because S is the start symbol. Also gives FOLLOW(A) FOLLOW(S), and gives FOLLOW(B) FOLLOW(S). So FOLLOW(S) = {} {a, b} FOLLOW(B), and FOLLOW(S) = FOLLOW(B) = {a, b, }.
This also fills the table: E3 = M[B, ] is , because is in FIRST(S) and is in FOLLOW(B).
Option (B) is wrong because cannot be in FIRST(A) (FIRST has only terminals and ), and FOLLOW(B) is not just {}.
Answer: (A)
Consider two binary operators ' 'and '' with the precedence of operator being lower than that of the operator . Operator is right associative while operator is left associative. Which one of the following represents the parse tree for expression (7 3 4 3 2)?
Answer: (B)
The operator has higher precedence and is right associative, so we group it as .
The operator is left associative, so we group it as .
Tree: the root is . Its right child is 2 and its left child is . This matches figure B.
Answer: B
The operator is left associative, so we group it as .
Tree: the root is . Its right child is 2 and its left child is . This matches figure B.
Answer: B
The grammar SaSa|bS|c is
Answer: (C)
The three productions begin with different terminals a, b and c. So their FIRST sets do not overlap the grammar is LL(1).
Any LL(1) grammar is also LR(1).
Answer: Both LL(1) and LR(1)
Any LL(1) grammar is also LR(1).
Answer: Both LL(1) and LR(1)
Keep practising