I made some change for you, it works
> WS [ \t\n] change to WS [ \t], you don't want to eat '\n'
> DIGIT [0-9]
> %%
> {DIGIT}+ { yylval = atoi(yytext); return NUM; }
> {WS} ; /* eat empty spaces. */
> . return yytext[0];
add 1 rule, '.' means any char except newline
[\n]    return yytext[0];


------------------------------------------------------
               致
礼!

               魏政元
               [EMAIL PROTECTED]
----- Original Message ----- 
From: "Laxman Bana" <[EMAIL PROTECTED]>
To: <help-bison@gnu.org>
Cc: <[EMAIL PROTECTED]>
Sent: Monday, May 09, 2005 12:09 PM
Subject: please help on infix yylex.


> Lex Gurus,
>    I am trying to learn flex and bison and I started with infix calc example 
> specified in the manual.
> Everything is nomal when I use yylex() function specified in the manual. But 
> I wanted to generate tokens using flex and I created following rules and it 
> never worked....:(
> I tried to fix this for long time but I am not able to see where I am making 
> a mistake? Please help.
> Thanks,
> Laxman
> 
> /* infix.l */
> %{
> #include <math.h>
> #include <infix.tab.h>
> extern int yylval;
> %}
> WS [ \t\n]
> DIGIT [0-9]
> %%
> {DIGIT}+ { yylval = atoi(yytext); return NUM; }
> {WS} ; /* eat empty spaces. */
> . return yytext[0];
> %%
> 
> yywrap()
> {
>    return 1;
> }
> 
> /* infix.y */
> %{
> #define YYSTYPE int
> #include <math.h>
> #include <stdio.h>
> %}
> %token NUM
> %%
> input:
> | input line
> ;
> line:   '\n'
> | exp '\n' { printf ("\t= %d\n", $1); }
> ;
> exp: NUM { $$ = $1; }
> | exp '+' exp { $$ = $1 + $3; }
> | exp '-' exp { $$ = $1 - $3; }
> | exp '*' exp { $$ = $1 * $3; }
> | exp '/' exp { $$ = $1 / $3; }
> ;
> %%
> 
> main()
> {
>     yyparse();
> }
> yyerror(s)
>     char *s;
> {
>     printf("%s \n", s);
> }
> 
> commands:
> $ flex -oinfix.l.c infix.l ; bison -d -oinfix.tab.c infix.y
> $ gcc -I.  -o infix  infix.l.c infix.tab.c
> flex version : 2.5.4
> Bison version: 1.875b
> 
> 
> 
> 
> _______________________________________________
> Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
_______________________________________________
Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison

Reply via email to