help-bison
[Top][All Lists]
Advanced

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

can i change a non-terminal type without using %union?


From: bookman bookman
Subject: can i change a non-terminal type without using %union?
Date: Tue, 21 Jul 2009 16:54:52 +0800

I tried a caculator using flex&bison, but i can not using it to caculate
real number. The flex and bison are as follows:
--flex cal.l--
%{
#define YYSTYPE double
#include "cal.y.h"
%}

floatNumber [0-9]+|([0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?
%%
{floatNumber} {
                        yylval=atof(yytext);
                        printf("%g\n",yylval);//(1)print the right number
                        return NUMBER;
                        }
[ \t]    ;        /* ignore white space */
\n    return 0;    /* logical EOF */
.    return yytext[0];
%%

--cal.y--
%{
#include <stdio.h>
%}
%token NUMBER

%%
statement:  expression   {printf("=%g\n",$1);}
          ;
expression: expression '+' tern {$$=$1+$3;}
           |expression '-' tern {$$=$1-$3;}
           |tern                {$$=$1;}
           ;
tern:       tern '*' factor {$$=$1*$3;}
           |tern '/' factor {if($3==0)yyerror("divided by 0;");else
$$=$1/$3;}
           |factor          {$$=$1;}
           ;
factor:    '(' expression ')' {$$=$2;}
           |NUMBER             {$$=$1;

printf("factor=%g,NUMBER=%g\n",$$,$1);
                                         /*(2)print wrong number,NUMBER
always be 0*/
                                         }
           ;
%%
int yyerror(char *s)
{
    fprintf(stderr, "%s\n", s);
}
int main(void)
{
  return yyparse();
}

/****************************************************************************/

As the resons i showed in (1) and (2), I indicate that bison  treats
non-termimal as integers and it change NUMBER from double to int when it get
yylval from flex.
  Is that right?
  I know I can using %union to specify the type of non-terminals,can i fix
the problem without using the directive of %union?
  Thank you very much.


reply via email to

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