An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if
View worked solution
Answer: B
An LALR(1) parser is constructed by merging the states of an LR(1) parser that share the same core (i.e., the same set of LR(0) items, ignoring the lookahead sets).
Why merging states cannot introduce new Shift-Reduce (S-R) conflicts:
Suppose the merged LALR(1) state has a shift-reduce conflict on a terminal symbol . This means the state contains:
- A shift item:
- A reduce item: with lookahead
Since the LALR(1) state is formed by taking the union of lookaheads of LR(1) states with the same core:
- The core of this state must contain both and .
- The lookahead for the reduction must have been present in at least one of the original LR(1) states, say state , before merging.
Since state has the same core, it also contains the item . Thus, in the original LR(1) parser, state already had a shift-reduce conflict on terminal (shift on due to , and reduce on due to ).
Therefore, merging states to construct an LALR(1) parser never introduces new shift-reduce conflicts. An LALR(1) parser has an S-R conflict if and only if the LR(1) parser for has an S-R conflict.
Analysis of other options:
- Option A & C: SLR(1) and LR(0) parsers are weaker than LALR(1). They can have S-R conflicts even when the LALR(1) parser does not.
- Option D: Merging states can introduce new reduce-reduce (R-R) conflicts, but this is independent of whether there is an S-R conflict.
Free practice · Your answers on this page are not saved.
