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

Syntax Analysis and Parsing Questions and Answers

Answer: C

Either the grammar is rewritten to distinguish matched from unmatched statements, or the parser generator is told to prefer shifting the else. Both realise the same nearest if rule. Languages requiring an explicit end keyword avoid the ambiguity altogether.

Please log in or register to report this question.

12.

State whether the following is true or false.
An ambiguous grammar can sometimes be rewritten as an unambiguous grammar for the same language.

Answer: D

Correct answer: True

The dangling else is the standard demonstration: splitting the statement nonterminal into matched and unmatched forms gives an unambiguous grammar generating exactly the same language. It cannot always be done - some languages are inherently ambiguous - but for programming language constructs it usually can, and precedence and associativity are handled the same way.

Please log in or register to report this question.

Answer: A

Correct answer: introducing one nonterminal level per precedence level

Each level of the hierarchy - expression, term, factor - corresponds to one precedence level, and because a lower level can only be reached through a higher one, the tighter-binding operators end up deeper in the tree. Associativity follows from whether the recursion is on the left or the right of the production.

Please log in or register to report this question.

Answer: B

Recursion on the left produces left leaning trees, so a plus b plus c groups as (a plus b) plus c. Recursion on the right would make the operator right associative instead. Associativity is therefore a property of how the production is written.

Please log in or register to report this question.

Answer: C

Right recursion produces right leaning trees, so a equals b equals c groups as a equals (b equals c). Assignment is conventionally right associative for exactly this reason. Comparison operators by contrast are usually left associative or non associative.

Please log in or register to report this question.

Answer: D

Correct answer: top-down parsers, which would recurse without consuming input

A top-down parser expanding A by the production A → Aα immediately faces A again with no token consumed, so it recurses forever. A bottom-up parser has no such difficulty and in fact prefers left recursion, since it keeps the stack shallow.

Please log in or register to report this question.

Answer: A

Correct answer: A goes to beta A prime, with A prime going to alpha A prime or epsilon

The original generates β followed by any number of αs, and so does the replacement: A produces the β once, and A′ produces the repetitions and then stops with ε. The recursion has been turned from left to right, which is what a top-down parser needs, and the language is unchanged.

Please log in or register to report this question.

18.

Assertion (A): Removing left recursion changes the shape of the parse tree.
Reason (R): The transformed grammar is right recursive, so the tree leans the other way.

Answer: B

Both are true and the reason gives the mechanism, and this is why the semantic actions must be rewritten too. Associativity is recovered by carrying an inherited attribute through the new nonterminal rather than by the tree shape.

Please log in or register to report this question.

Answer: C

Correct answer: two productions for a nonterminal share a common prefix

If two alternatives begin with the same symbols, one token of lookahead cannot tell them apart. Left factoring pulls the common prefix out into a single production and defers the choice to a new nonterminal, so the decision is postponed until the input has distinguished the alternatives.

Please log in or register to report this question.

Answer: D

The common prefix is consumed first and the choice is deferred to a new nonterminal. The language is unchanged, but the parser can now decide after seeing what follows the prefix. Repeated factoring may be needed when several alternatives share prefixes.

Please log in or register to report this question.

Loading…

Other Subjects

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