Search code examples
parsingcontext-free-grammarlr-grammar

Can an LR(1) parser parse this grammar?


I have the following CFG that I'd like to parse with an LR(1) parser:

S → A | B

A → ε | A

B → ε |B

Can an LR(1) parser parse this grammar? If so, can you show me the parse table? If not, why not and how can you tell?


Solution

  • No, an LR(1) parser cannot parse this grammar. LR(k) parsers can only parse unambiguous grammars, and this grammar is ambiguous (you can derive ε in infinitely many ways).

    You can check this by building the configurating sets for the grammar, though that's going to be pretty boring. :-)

    Hope this helps!