help-bison
[Top][All Lists]
Advanced

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

Re: shift/reduce conflicts


From: Hans Aberg
Subject: Re: shift/reduce conflicts
Date: Mon, 29 Jan 2001 12:01:21 +0100

At 10:04 +0300 1-01-29, —៊Պ" ڍÀ¦× wrote:
>     After I  added  this  sequence (below) of  operators Bison told me
>about half-dozen shift/reduce conflicts.
>
>     Still - the Converter works well.
>
>     I'd be grateful if somebody will give his advice what does it mean.

Shift means (roughly) to put a token onto the stack, and go to a new state,
whereas reduce (roughly) means to apply a full rule and its action in the
grammar definition (see the Bison docs or a book on compilers). Sometimes
the LALR(1) algorithm cannot decide which one to use, in which case Bison
issues a warning, but prefers a shift over a reduce. Use the --verbose
option to write a .output file the details.

Your grammar can be fixed by telling whether the operators should be left
or right associative:
  %left andor mathoper

Bison can handle precedence as well, so you could simplify your grammar
further. A simple calculator might have (also described in the Bison
manual) (leaving it to you to fix it up for your application):

%token NUMBER
%token ELEMENTARY_FUNCTION
%token IDENTIFIER
%token EXIT

%left '+' '-'
%right ELEMENTARY_FUNCTION
%left '*' '/'
%right '^'
%right UMINUS

%%
expr:
    expr '+' expr { $$.value = $1.value + $3.value }
  | expr '-' expr { $$.value = $1.value - $3.value }
  | expr '*' expr { $$.value = $1.value * $3.value }
  | expr '/' expr { $$.value = $1.value / $3.value }
  | expr '^' expr { $$.value = pow($1.value, $3.value) }
  | '(' expr ')' { $$.value = $2.value }
  | '-' expr  %prec UMINUS { $$.value = -$2.value }
  | ELEMENTARY_FUNCTION expr { $$.value = (*$1.f)($2.value) }
  | NUMBER

  Hans Aberg





reply via email to

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