Questions: Grammar Design for Compilation

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A grammar includes the production: Expr → Expr + Term | Term. A student implements a recursive descent parser for this grammar. When parsing '3 + 4', the parser:

ASuccessfully parses the input and produces the correct left-associative parse tree
BEnters infinite recursion immediately, because expanding Expr calls Expr again without consuming any input
CGenerates a shift-reduce conflict and cannot decide whether to reduce or shift
DFails because recursive descent parsers cannot handle binary operators
Question 2 Multiple Choice

A grammar needs to parse arithmetic expressions with the standard precedence (multiplication before addition, both left-associative). The canonical technique to encode this in the grammar is:

AAdd explicit parenthesization to the language, requiring programmers to always write (3) + (4 * 5)
BUse a single nonterminal Expr with all operators at the same level, and add a post-processing step to reorder the parse tree by precedence
CIntroduce a hierarchy of nonterminals — one tier per precedence level — so that higher-precedence operators appear in productions that are deeper in the grammar
DResolve precedence through a separate disambiguation table provided as metadata to the parser generator, without changing the grammar rules
Question 3 True / False

The same context-free language can be described by grammars that differ significantly in their suitability for a given parsing algorithm, meaning theoretical correctness and practical usability are distinct properties of a grammar.

TTrue
FFalse
Question 4 True / False

Eliminating left-recursion from a grammar guarantees that the resulting grammar will have no conflicts and be suitable for any parsing algorithm.

TTrue
FFalse
Question 5 Short Answer

Why is writing a theoretically correct grammar — one that accepts exactly the right language — not sufficient for compiler design? What additional properties must the grammar satisfy?

Think about your answer, then reveal below.