help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is this a shift/reduce conflict?


From: Valentin Tolmer
Subject: Re: Why is this a shift/reduce conflict?
Date: Wed, 05 Jun 2013 02:32:12 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6

On Wed 05 Jun 2013 12:06:30 AM CEST, Adam Smalin wrote:
During my test to get  << a difference precedence then < I suspect it is
impossible because after expr < expr [<] it has no idea if it will be a <<
expr or an < expression thus I can't tell it to have a different precedence
between < and < <.

Because I can't I wrote this test grammar. When I execute `a<b< <cd<e` I
get << < < as I wanted. The issue is 2 shift reduce conflicts. I can't
understand it. It does exactly what I want (<< before <). Its has
%glr-parser at the top. Why isn't it ok shifting and reducing when it can't
complete the longer rule (mainElement3). I'm just confused all over I don't
see the conflict. How would I rewrite this to not have the conflict?

%nonassoc DOLLAR
//%left '<'
%left '*' PREC
%left '.'


%%
program: mEOS main
main: | mainLoop mEOS
mainLoop:
       mainElement
     | mainLoop mainElement
mainElement:
       mainElement3
     | mainElement '<' mainElement3    { printf("<\n"); }
mainElement3:
       mainElement4
     | mainElement3 '<' '<' mainElement4     { printf("<<\n"); }
mainElement4:
       '$' VarName     %prec DOLLAR    { printf("varname\n"); }
     | mainElement2
     //| mainElement '*' mainElement    { printf("*\n"); }
mainElement2:
       VAR             { printf("var %s\n", yytext); }
     | Token         { printf("token\n"); }

Token: '.'
VarName: VAR
     | VarName  '.' VAR

EOS:   '\n' | ';'
mEOS:       | mEOS EOS

%%


state 12

     6 mainElement: mainElement3 .  [$end, VAR, '.', '<', '$', '\n', ';']
     9 mainElement3: mainElement3 . '<' '<' mainElement4

     '<'  shift, and go to state 22

     '<'       [reduce using rule 6 (mainElement)]
     $default  reduce using rule 6 (mainElement)

state 24

     7 mainElement: mainElement '<' mainElement3 .  [$end, VAR, '.', '<',
'$', '\n', ';']
     9 mainElement3: mainElement3 . '<' '<' mainElement4

     '<'  shift, and go to state 22

     '<'       [reduce using rule 7 (mainElement)]
     $default  reduce using rule 7 (mainElement)
_______________________________________________
address@hidden https://lists.gnu.org/mailman/listinfo/help-bison

If this is a possibility, you could detect a double << at the lexer level, and give the parser a special token corresponding to <<. That would simplify your grammar.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]