Questions: Context-Free Grammar Properties and Ambiguity

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Consider the expression grammar E -> E + E | E * E | id. A compiler developer discovers that the string 'id + id * id' has two valid parse trees under this grammar. What is the fundamental problem this creates for the compiler?

AThe grammar is incomplete — it fails to generate some arithmetic expressions
BThe grammar generates too many strings — it accepts some expressions that should be invalid
CThe grammar cannot be used to guide parsing at all, because ambiguous grammars have no valid derivations
DThe compiler cannot assign a unique structural interpretation to the expression, leaving evaluation order undefined
Question 2 Multiple Choice

A grammar contains the production A -> Aalpha | beta, where A is the first symbol on the right-hand side. Which type of parser can handle this production directly without transformation?

ALL (top-down) parsers, because they process input from left to right
BNeither LL nor LR parsers — left recursion is illegal in context-free grammars
CLR (bottom-up) parsers, which build parse trees from leaves to root and handle left recursion naturally
DBoth LL and LR parsers, provided the grammar is otherwise unambiguous
Question 3 True / False

Two context-free grammars can generate exactly the same set of strings yet differ in whether they are ambiguous.

TTrue
FFalse
Question 4 True / False

Fixing an ambiguous grammar generally requires removing some strings from the language — that is, accepting a more restrictive set of programs.

TTrue
FFalse
Question 5 Short Answer

What exactly is wrong with using an ambiguous grammar in a compiler, and how does encoding operator precedence directly into the grammar's nonterminal hierarchy solve this problem?

Think about your answer, then reveal below.