help-bison
[Top][All Lists]
Advanced

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

Re: please help on infix yylex.


From: 魏政元
Subject: Re: please help on infix yylex.
Date: Tue, 10 May 2005 11:26:38 +0800

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];


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

               魏政元
               address@hidden
----- Original Message ----- 
From: "Laxman Bana" <address@hidden>
To: <address@hidden>
Cc: <address@hidden>
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
> 
> 
> 
> 
> _______________________________________________
> address@hidden http://lists.gnu.org/mailman/listinfo/help-bison

reply via email to

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