List Info

Thread: Problem with precedence and symbolic tokens




Problem with precedence and symbolic tokens
user name
2008-03-17 13:30:03
I am trying to use precedences in Bison. After several
trials, a
simplified version of the relevant part of my input file is
(it is
supposed to produce reversed Polish notation):

------------------------------------------------------------
------------------------
%{ ... %}

%left ADD
%left MULT
%right EXP
%left UNARY

%%
...
Expr
        : Expr AddOp  Expr   %prec ADD    {
printf("+"); }
        | Expr MultOp Expr   %prec MULT   {
printf("*"); }
        | Expr ExpOp  Expr   %prec EXP     {
printf("^"); }
        | 'a'                                              
{ printf("a"); }
        | '(' Expr ')'
        | '-' Expr                  %prec UNARY  {
printf("~"); }
        ;
AddOp
        :  '+'
        |  '-'
        ;
MultOp
        :  '*'
        |  '/'
ExpOp
        :  '^'
        ;
%%
...
------------------------------------------------------------
------------------

Unfortunately precedences do not seem to work. On the other
hand, if I
replace operator nonterminals by (like Addop) their symbols
('+') and
put specs like %left '+', it works (without any %prec).

Any hints?

-- tsf


_______________________________________________
help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison

Re: Problem with precedence and symbolic tokens
country flaguser name
Sweden
2008-03-17 13:51:21
It just looks at the tokens in the rules, so you can't throw
in extra  
rules like you have done below. That is, AddOp must be
expanded into  
two rules "Expr: ...".

Look in the .outout file for the S/R conflicts, and the
tokens  
immediately before and after the parsing position dot
".".

   Hans Aberg


On 17 Mar 2008, at 19:30, T. S. Ferreira wrote:

> I am trying to use precedences in Bison. After several
trials, a
> simplified version of the relevant part of my input
file is (it is
> supposed to produce reversed Polish notation):
>
>
------------------------------------------------------------
---------- 
> --------------
> %{ ... %}
>
> %left ADD
> %left MULT
> %right EXP
> %left UNARY
>
> %%
> ...
> Expr
>         : Expr AddOp  Expr   %prec ADD    {
printf("+"); }
>         | Expr MultOp Expr   %prec MULT   {
printf("*"); }
>         | Expr ExpOp  Expr   %prec EXP     {
printf("^"); }
>         | 'a'                                          
    { printf 
> ("a"); }
>         | '(' Expr ')'
>         | '-' Expr                  %prec UNARY  {
printf("~"); }
>         ;
> AddOp
>         :  '+'
>         |  '-'
>         ;
> MultOp
>         :  '*'
>         |  '/'
> ExpOp
>         :  '^'
>         ;
> %%
> ...
>
------------------------------------------------------------
---------- 
> --------
>
> Unfortunately precedences do not seem to work. On the
other hand, if I
> replace operator nonterminals by (like Addop) their
symbols ('+') and
> put specs like %left '+', it works (without any
%prec).
>
> Any hints?
>
> -- tsf
>
>
> _______________________________________________
> help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison



_______________________________________________
help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison

Re: Problem with precedence and symbolic tokens
user name
2008-03-17 20:27:45
I got it. My application has lots of operators and I was
looking for
some way of simplifying my grammar but there seems to be no
other way.

Thanks a lot.

-- tsf


On Mon, Mar 17, 2008 at 3:51 PM, Hans Aberg <habergmath.su.se> wrote:
> It just looks at the tokens in the rules, so you can't
throw in extra
>  rules like you have done below. That is, AddOp must be
expanded into
>  two rules "Expr: ...".
>
>  Look in the .outout file for the S/R conflicts, and
the tokens
>  immediately before and after the parsing position dot
".".
>
>    Hans Aberg
>
>
>
>
>  On 17 Mar 2008, at 19:30, T. S. Ferreira wrote:
>
>  > I am trying to use precedences in Bison. After
several trials, a
>  > simplified version of the relevant part of my
input file is (it is
>  > supposed to produce reversed Polish notation):
>  >
>  >
------------------------------------------------------------
----------
>  > --------------
>  > %{ ... %}
>  >
>  > %left ADD
>  > %left MULT
>  > %right EXP
>  > %left UNARY
>  >
>  > %%
>  > ...
>  > Expr
>  >         : Expr AddOp  Expr   %prec ADD    {
printf("+"); }
>  >         | Expr MultOp Expr   %prec MULT   {
printf("*"); }
>  >         | Expr ExpOp  Expr   %prec EXP     {
printf("^"); }
>  >         | 'a'                                    
          { printf
>  > ("a"); }
>  >         | '(' Expr ')'
>  >         | '-' Expr                  %prec UNARY 
{ printf("~"); }
>  >         ;
>  > AddOp
>  >         :  '+'
>  >         |  '-'
>  >         ;
>  > MultOp
>  >         :  '*'
>  >         |  '/'
>  > ExpOp
>  >         :  '^'
>  >         ;
>  > %%
>  > ...
>  >
------------------------------------------------------------
----------
>  > --------
>  >
>  > Unfortunately precedences do not seem to work. On
the other hand, if I
>  > replace operator nonterminals by (like Addop)
their symbols ('+') and
>  > put specs like %left '+', it works (without any
%prec).
>  >
>  > Any hints?
>  >
>  > -- tsf
>  >
>  >
>  > _______________________________________________
>  > help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison
>
>



-- 
T. S. Ferreira


_______________________________________________
help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )