Search code examples
parsinggrammarcontext-free-grammarlr-grammar

Is it possible to transform this grammar to be LR(1)?


The following grammar generates the sentences a, a, a, b, b, b, ..., h, b. Unfortunately it is not LR(1) so cannot be used with tools such as "yacc".

S -> a comma a.
S -> C comma b.
C -> a | b | c | d | e | f | g | h.

Is it possible to transform this grammar to be LR(1) (or even LALR(1), LL(k) or LL(1)) without the need to expand the nonterminal C and thus significantly increase the number of productions?


Solution

  • Not as long as you have the nonterminal C unchanged preceding comma in some rule.

    In that case it is clear that a parser cannot decide, having seen an "a", and having lookahead "comma", whether to reduce or shift. So with C unchanged, this grammar is not LR(1), as you have said.

    But the solution lies in the two phrases, "having seen an 'a'" and "C unchanged". You asked if there's fix that doesn't expand C. There isn't, but you could expand C "a little bit" by removing "a" from C, since that's the source of the problem:

    S -> a comma a .
    S -> a comma b .
    S -> C comma b .
    C -> b | c | d | e | f | g | h .
    

    So, we did not "significantly" increase the number of productions.