Machine-translated by Google. Questions are authored in English.
Top

Syntax Analysis and Parsing Questions and Answers

21.

Arrange these grammar transformations in the order they are normally applied when preparing a grammar for predictive parsing.
1. perform left factoring
2. remove ambiguity from the grammar
3. compute the FIRST and FOLLOW sets
4. eliminate left recursion

Answer: A

Correct answer: 2 - 4 - 1 - 3

Remove ambiguity first, since neither of the other transformations helps with it and the grammar must be unambiguous before either is worth doing. Then eliminate left recursion, which a top-down parser cannot tolerate at all. Then left factor, which removes the remaining need for lookahead beyond one token. Only then compute FIRST and FOLLOW, on the grammar that will actually be used.

Please log in or register to report this question.

Answer: B

Correct answer: the set of terminals that can begin a string derived from it

FIRST tells the parser what it may expect to see if it chooses a given production, so comparing the FIRST sets of the alternatives is how the choice is made. Epsilon is included when the string can derive nothing at all, and it is that case which brings FOLLOW into the calculation.

Please log in or register to report this question.

Answer: C

FOLLOW is needed to decide when to apply an epsilon production, since the parser must then look at what comes after A. The end marker is in FOLLOW of the start symbol by definition. Computing the sets is an iterative process repeated until nothing more changes.

Please log in or register to report this question.

24.

Consider the following statements about FIRST and FOLLOW.
1. Epsilon may belong to a FIRST set.
2. The end of input marker may belong to a FOLLOW set.
3. Epsilon may belong to a FOLLOW set.

Answer: D

Correct answer: 1 and 2 only

Statements 1 and 2 are correct: epsilon belongs to the FIRST set of anything that can derive the empty string, and the end of input marker belongs to the FOLLOW set of the start symbol. Statement 3 is wrong - a FOLLOW set contains terminals and the end marker, and epsilon is not something that can follow a symbol.

Please log in or register to report this question.

Answer: A

Correct answer: one token of lookahead suffices to choose the correct production

The two Ls mean scanning left to right and producing a leftmost derivation, and the 1 is the tokens of lookahead. The condition is that for each nonterminal the FIRST sets of its alternatives are disjoint, and where an alternative can be empty, that its FIRST set is disjoint from the nonterminal’s FOLLOW set.

Please log in or register to report this question.

Answer: B

Correct answer: LL(1) or LR(1), since both require a unique parsing action

Both methods build a table with one entry per state and lookahead, and ambiguity means two different actions are needed in one place, which appears as a conflict. The grammar may still be written down and may describe a real construct perfectly well - it simply cannot be parsed deterministically without being rewritten or a conflict resolution rule supplied.

Please log in or register to report this question.

Answer: C

Correct answer: LL(1), since the predictive parser would not terminate

A predictive parser expanding a left recursive nonterminal would call itself without consuming a token and never return. Left recursion is therefore fatal to LL parsing and must be eliminated first; LR parsing handles it without difficulty, and in fact prefers it. Nothing about left recursion makes a grammar ambiguous.

Please log in or register to report this question.

28.

Match each grammar defect with the transformation that addresses it.
List I
A. left recursion
B. common prefix in alternatives
C. two parse trees for one sentence
D. nonterminal deriving no terminal string
List II
1. rewriting to remove ambiguity
2. recursion elimination
3. removal of useless symbols
4. left factoring

Answer: D

Each defect blocks a different aspect of parsing. Recognising which transformation applies is the routine first step in preparing any grammar for a parser generator. Most generators report the defect precisely enough to identify the offending rule.

Please log in or register to report this question.

Answer: A

Correct answer: derives no terminal string or is unreachable from the start symbol

Either fault makes the nonterminal contribute nothing to the language: one that derives no terminal string can never complete a derivation, and one unreachable from the start symbol can never begin one. Both are removed before parsing, since they only complicate the tables and can mask genuine errors in the grammar.

Please log in or register to report this question.

Answer: B

Top-down builds the tree from the root toward the leaves, bottom-up from the leaves toward the root. Every practical parsing algorithm belongs to one family or the other. The choice determines both the grammars accepted and the shape of the code written.

Please log in or register to report this question.

Loading…
Tags: Syntax Analysis and Parsing Questions and Answers || Syntax Analysis and Parsing MCQ Questions and Answers || Syntax Analysis and Parsing GK Questions and Answers || Syntax Analysis and Parsing GK MCQ Questions || Syntax Analysis and Parsing Multiple Choice Questions and Answers || Syntax Analysis and Parsing GK || GK on Syntax Analysis and Parsing || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design