help-bison
[Top][All Lists]
Advanced

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

Re: Give me an example please!!!


From: Hans Aberg
Subject: Re: Give me an example please!!!
Date: Wed, 5 Jun 2002 12:34:16 +0200

Reply-to: address@hidden

At 11:33 -0500 2002/06/04, Javier Andres Mena Zapata wrote:
I have to the next grammar:
> <exp> ::= <prim> ( <list-of-exp> )
>       ::= <number>
>
> <prim> ::= + | - | *
>
> <list-of-exp> ::= <exp>
>               ::= <list-of-exp> <exp>
>
>How can I evaluate an exp using Bison.

In the Bison .y file your grammar might look like:

%token number
%%
 exp: prim '(' list_of_exp ')' { /* action */ }
    | number                   { /* action */ }

 prim:
    '+' { /* action */ }
  | '-' { /* action */ }
  | '*' { /* action */ }

 list_of_exp:
     exp             { /* action */ }
   | list_of_exp exp { /* action */ }
%%

Your YYSTYPE must be able to store both a number and the operator type. So
you might use the %union feature (see Bison manual), one field of number
type X, and then perhaps your operators will have type (C++ pseudo-code)
    X (*)(const &list<X>)

  Hans Aberg





reply via email to

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