Re: please help on infix yylex.

2005-05-09 Thread Hans Aberg
At 04:09 + 2005/05/09, Laxman Bana wrote:
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.
I posted 2005/04/01 an example "Simple Flex/Bison example" in this list. 
See:
List-Archive: 
--
  Hans Aberg
___
Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison


Re: please help on infix yylex.

2005-05-09 Thread 魏政元
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: 
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 
> #include 
> 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 
> #include 
> %}
> %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