help-bison
[Top][All Lists]
Advanced

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

Why is this a shift/reduce conflict?


From: Adam Smalin
Subject: Why is this a shift/reduce conflict?
Date: Tue, 4 Jun 2013 18:06:30 -0400

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)


reply via email to

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