Pergunta

I am encountering a shift/reduce conflict inside this trivial Regular Expression parser. I am a beginner in yacc and I seem to be a little confused. Here is what I have written so far:

%token ID
%%
exp:    ID  { $$ = new YYRegExParserVal(this._createObjectForID($1.ival)); }
        | exp exp  { $$ = new YYRegExParserVal(this._createObjectForConcat($1.obj, $2.obj)); }
        ;
%%

The name of my parser class is YYRegExParser and right now it should be recognizing only simple ID's (alphanumeric symbols) and concatenation between two regular expressions. However, the second rule never gets matched even though my input is correct

Foi útil?

Solução

The grammar

exp → id | exp exp

is ambiguous because there are two different parse trees for id id id. In general, CFGs with productions of the form S → SS will be ambiguous as long as the S nonterminal is reachable from the start symbol. Since no ambiguous grammar is LALR(1), the parser will find either a shift/reduce or reduce/reduce conflict.

To fix this, try changing your grammar to

exp → id | id exp

This grammar is unambiguous, which should resolve the issue.

Hope this helps!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top