Statement for Linked Answer Questions 83a & 83b:
Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.
The above grammar and the semantic rules are fed to a yaac tool (which is an LALR(1) parser generator) for parsing and evaluating arithmetic expressions. Which one of the following is true about the action of yaac for the given grammar?
View worked solution
Answer: C
The given grammar is ambiguous:
When an ambiguous grammar is fed to yacc (an LALR(1) parser generator), it generates conflicts in the parsing table:
- Shift-Reduce Conflicts: For an input like
id + id + id, after parsing the firstid + id, the parser can either reduceE + EtoE, or shift the next+operator.
By default, yacc resolves these conflicts using the following built-in disambiguation rules:
- Shift-Reduce Conflict: Resolved in favor of the shift action (which makes operators right-associative by default unless precedence declarations are specified).
- Reduce-Reduce Conflict: Resolved by choosing the production rule that appears first in the grammar specification.
Therefore, yacc detects a shift-reduce conflict and resolves it in favor of a shift over a reduce action.
Free practice · Your answers on this page are not saved.
