help-bison
[Top][All Lists]
Advanced

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

RE: Why Bison prefer reduce to shift in dangling else?


From: Lunjin Lu
Subject: RE: Why Bison prefer reduce to shift in dangling else?
Date: Mon, 24 Feb 2003 21:50:07 -0500

Thank you. I added %nonassoc THEN %nonassoc ELSE to my grammar for the Tiger
langauge. Attached. The bison still chooses reduce rather than shift. The
first line of the tiger.output reads
"Conflict in state 104 between rule 25 and token ELSE resolved as shift."
but under state 104, i found

        state 104

                exp  ->  IF vexp THEN exp .   (rule 25)
                exp  ->  IF vexp THEN exp . ELSE exp   (rule 26)

                ELSE    shift, and go to state 123

                $default        reduce using rule 25 (exp)

It is frustrating. Any help will be appreciated.

Lunjin Lu


> -----Original Message-----
> From: Hans Aberg [mailto:address@hidden
> Sent: Monday, February 24, 2003 1:10 PM
> To: address@hidden
> Cc: address@hidden; address@hidden
> Subject: RE: Why Bison prefer reduce to shift in dangling else?
>
>
> At 10:15 -0500 2003/02/24, Lunjin Lu wrote:
> >Dear Hans,
> >
> >I tried to use both
> >%left THEN
> >%left ELSE
> >
> >and
> >%left ELSE
> >%left THEN
> >
> >but bison still does not make the right choice. Is there any way to force
> >bison to choose shift in a shift/reduce conflict?
>
> I simply wrote:
>
> %token IF THEN ELSE variable
>
> %nonassoc THEN
> %nonassoc ELSE
>
> %%
>
> stmt:
>     expr
>   | if_stmt
>
> if_stmt:
>     IF expr THEN stmt
>   | IF expr THEN stmt ELSE stmt
>
> expr: variable
>
> And as I remember, changing the order between the %nonassoc declarations
> should produce the two possibilities: The difference is in
>     IF ... THEN IF ... THEN ... ELSE ...      -- is statement
>     IF ... THEN (IF ... THEN ...) ELSE ...    -- reduce
>     IF ... THEN (IF ... THEN ... ELSE ...)    -- shift ELSE
> Here the %nonassoc precedences will tell whether the last ELSE should be
> shifted or not when compared to the last THEN. If ELSE has higher
> precedence, i.e. appears later in the %nonassoc declaration, it should be
> shifted. (Think of an expression like a + b * c, where * has a higher
> precedence that +.)
>
> It should not be different when using %left or %right (the difference
> between these concerns tokens of the same priority).
>
> Check that it is right by looking into the .output file that Bison writes
> if you have the --verbose startup option.
>
> Perhaps you have something else in your grammar causing your problem.
>
> BTW, the choice of shift that Bison does is what one usually encounter in
> computer languages. But it is better to make the grammar fully specified
> via precedences.
>
>   Hans Aberg
>
>

Attachment: tiger.y
Description: Binary data

Attachment: tiger.output
Description: Binary data


reply via email to

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