Questions: Context-Free Grammars in Compiler Design

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A grammar has two rules: `Expr → Expr + Term | Term` and `Term → Term * Factor | Factor`. A student asks why multiplication binds more tightly than addition without any explicit precedence declaration. What is the correct explanation?

AThe parser is programmed to always evaluate * before +, so the grammar order doesn't matter
BBecause * appears alphabetically before + in ASCII, parsers process it first
CMultiplication is handled at the Term level, which must be fully resolved before it can become part of an Expr; this nesting means * binds more tightly than + by the grammar's structure alone
DPrecedence is specified separately in a precedence table; the grammar rules are just for syntax
Question 2 Multiple Choice

Why are context-free grammars necessary for describing programming language syntax, rather than simpler regular expressions?

ACFGs are not necessary — regular expressions are equally expressive but slower to evaluate
BRegular expressions can describe nested structures like matching parentheses or arbitrarily deep if-else blocks, but CFGs are preferred for efficiency
CRegular expressions cannot describe recursive nesting — matching parentheses, nested blocks, or arbitrarily deep expression trees require CFG productions that refer to themselves
DCFGs are required only for semantics (type checking); regular expressions suffice for syntax
Question 3 True / False

A context-free grammar rule like `Expr → Expr + Term` simultaneously defines which token sequences are syntactically valid AND encodes that addition is left-associative through its recursive structure.

TTrue
FFalse
Question 4 True / False

A context-free grammar for a programming language also specifies the semantic rules, such as type checking and variable scope resolution, since these follow directly from the parse tree structure.

TTrue
FFalse
Question 5 Short Answer

Explain why operator precedence and associativity fall out naturally from the grammar's structure, rather than needing to be declared as separate rules.

Think about your answer, then reveal below.