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: Hans Aberg
Subject: RE: Why Bison prefer reduce to shift in dangling else?
Date: Mon, 24 Feb 2003 19:09:53 +0100

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






reply via email to

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