Questions: The Parsing Problem

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A grammar for a programming language produces two different parse trees for the input 'if (a) if (b) x = 1; else x = 2;' — one where 'else' binds to the outer 'if' and one where it binds to the inner 'if'. What is the fundamental problem and what must be done?

AThe grammar has too many production rules; reducing rule count will eliminate the conflict
BThe grammar is ambiguous — the same token stream has multiple parse trees, so the program's meaning is undefined; the grammar must be rewritten or explicit disambiguating rules added
CThe parser needs more lookahead tokens; switching from LL(1) to LL(3) will resolve the conflict
DThe lexer is incorrectly tokenizing 'else'; fixing the tokenization rules will eliminate the ambiguity
Question 2 Multiple Choice

A CFG for an arithmetic language is unambiguous, but a backtracking recursive-descent parser runs in exponential time on some valid inputs. A developer argues this is acceptable since the grammar is correct. Why is this reasoning flawed?

AIt is not flawed — unambiguous grammars always parse efficiently; the exponential behavior must be a bug in the implementation
BEven unambiguous grammars can require exponential time with naive algorithms; practical compilers restrict to grammar subclasses (LL, LR) that guarantee linear-time parsing regardless of input
CThe exponential behavior only occurs on invalid inputs, which can be filtered by the lexer before parsing begins
DAdding more production rules to the grammar will transform it into an LL grammar, fixing the performance problem
Question 3 True / False

A parse tree makes operator precedence explicit: in the expression '3 + 4 * 5', a correctly written grammar will produce a parse tree where the multiplication node is nested deeper than the addition node, encoding that multiplication binds more tightly.

TTrue
FFalse
Question 4 True / False

An ambiguous grammar can usually be converted into an equivalent unambiguous grammar — one that accepts exactly the same strings — by rewriting its production rules.

TTrue
FFalse
Question 5 Short Answer

Why is grammar ambiguity a serious problem for compilers, not just a theoretical concern?

Think about your answer, then reveal below.