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 16–20 of 58
Consider the augmented grammar given below:
Let . The number of items in the set is __________.
Let . The number of items in the set is __________.
Answer: 5
In , the item moves on to . Closure (dot before L, then before S):
1.
2.
3.
4.
5.
Answer: 5
1.
2.
3.
4.
5.
Answer: 5
C onsider the following parse tree for the expression a#b$c$d#e#f, involving two binary operators $ and #.
Which one of the following is correct for the given parse tree?
Which one of the following is correct for the given parse tree?
Answer: (A)
From the tree, the expression is grouped as .
- Each sits lower in the tree than the above it, so is applied first: has higher precedence.
- is grouped as , with the left pair done first: is left associative.
- The operators nest to the right: . So is right associative.
Options (B) and (D) are wrong because they give the higher precedence, and (C) is wrong because it makes left associative.
Answer: (A)
- Each sits lower in the tree than the above it, so is applied first: has higher precedence.
- is grouped as , with the left pair done first: is left associative.
- The operators nest to the right: . So is right associative.
Options (B) and (D) are wrong because they give the higher precedence, and (C) is wrong because it makes left associative.
Answer: (A)
Consider the following grammar:
stmt if expr then expr else expr; stmt|
exprterm relop term|term
term id|number
ida|b|c
number [0-9]
where relop is a relational operator (e.g ,...), refers to the empty statement, and if ,then, else are terminals.
Consider a program P following the above grammar containing ten if terminals. The number of control flow paths in P is ____________. For example the program
if then else
has 2 control flow paths and .
stmt if expr then expr else expr; stmt|
exprterm relop term|term
term id|number
ida|b|c
number [0-9]
where relop is a relational operator (e.g ,...), refers to the empty statement, and if ,then, else are terminals.
Consider a program P following the above grammar containing ten if terminals. The number of control flow paths in P is ____________. For example the program
if then else
has 2 control flow paths and .
Answer: 1024
In the grammar, is or the empty statement. The and parts derive only , never , so an cannot sit inside another . The only is the one after the semicolon. So the program is a sequence of if-then-else statements, one after another, ending with the empty statement.
Each if-then-else has 2 paths ( or ), and the choice in one statement does not depend on the others. With ten terminals the paths multiply: .
Answer: 1024
Each if-then-else has 2 paths ( or ), and the choice in one statement does not depend on the others. With ten terminals the paths multiply: .
Answer: 1024
A student wrote two context-free grammars G1 and G2 for generating a single C-like array declaration. The dimension of the array is at least one. For example,
int a[10][3];
The grammars use D as the start symbol,and use six terminal symbols int ;id[]num.
Which of the grammars correctly generate the declaration mentioned above?
int a[10][3];
The grammars use D as the start symbol,and use six terminal symbols int ;id[]num.
Grammar G1 Grammar G2
D → int L; D → int L;
L → id [E L → id E
E → num] E → E[num]
E → num] [E E → [num] Which of the grammars correctly generate the declaration mentioned above?
Answer: (A)
We derive int a[10][3]; using each grammar in turn.
- G1: D int L; int id [ E ; int id [ num ] [ E ; int id [num] [num] ; This is accepted.
- G2: D int L; int id E ; int id E [num] ; int id [num] [num] ; This is accepted.
In general, both grammars also generate arrays with one or more dimensions.
Answer: Both G1 and G2
- G1: D int L; int id [ E ; int id [ num ] [ E ; int id [num] [num] ; This is accepted.
- G2: D int L; int id E ; int id E [num] ; int id [num] [num] ; This is accepted.
In general, both grammars also generate arrays with one or more dimensions.
Answer: Both G1 and G2
Which one of the following is TRUE at any valid state in shift-reduce parsing?
Answer: (C)
In a valid shift-reduce parse, the stack always holds a viable prefix. A viable prefix is a prefix of some right sentential form that does not go past the right end of the handle. Every prefix of a viable prefix is also a viable prefix, so reading the stack from the bottom up to any point gives a viable prefix.
- (A) and (B) are wrong: viable prefixes are not limited to the bottom or the top. The whole stack is one, and so is every part of it from the bottom.
- (D) is wrong: the stack always holds a viable prefix.
Answer: (C)
- (A) and (B) are wrong: viable prefixes are not limited to the bottom or the top. The whole stack is one, and so is every part of it from the bottom.
- (D) is wrong: the stack always holds a viable prefix.
Answer: (C)
Keep practising