Consider a simple grammar for arithmetic expressions with rules:
E → E + T | T
T → T * F | F
F → (E) | id
Which of the following statements about the parse tree for the expression id + id * id is correct?
Recall that multiplication has higher precedence than addition in arithmetic expressions.
The grammar is left-recursive but respects operator precedence by the order of rules. Multiplication binds tighter, so the parse tree groups id * id first, then adds the first id.
Which of the following best describes the difference between a leftmost derivation and a rightmost derivation in context-free grammars?
Think about the order in which non-terminals are expanded during derivation.
Leftmost derivation always expands the leftmost non-terminal symbol first in each step, while rightmost derivation expands the rightmost non-terminal first. Both produce the same language but differ in derivation order.
Given the grammar rules:
S → S + S | S * S | (S) | id
Which of the following statements about the parse trees for the string id + id * id is true?
Consider if the grammar enforces operator precedence or allows multiple interpretations.
The grammar allows both (id + id) * id and id + (id * id) parse trees for the same string, showing ambiguity because it does not enforce operator precedence.
Which of the following best explains the relationship between a parse tree and a derivation sequence for a given string in a context-free grammar?
Think about how derivations and parse trees represent the generation of strings.
A derivation sequence lists the steps of replacing non-terminals to produce a string. The parse tree shows these steps as a tree structure, making the hierarchy clear.
Consider a parse tree generated from a context-free grammar for the string id + id * id. If each id corresponds to a leaf node, how many leaf nodes will the parse tree have?
Count the number of terminal symbols in the string.
The string id + id * id has five terminal symbols: three id tokens and two operators (+ and *). All terminals are leaves in the parse tree, so there are 5 leaf nodes.