Problem with Lex

Problem with Lex

Post by Zomb » Thu, 06 Jun 2002 16:07:57



hi!

i am new to lex nd yacc. I am finding a strange prob wit it. if u see
the below code, i am trying to match a pattern of only umbers while, i
see that yytext contains even characters.ie it recognizes..."sr34" and
prints it while it should nt do it, as far as my knowledge
goes........please help.

thanx

natkhat

%{
        int k;
%}
num     -?(([0-9]+)|(0-9]*\.[0-9]+))
%%
{num}   { printf("%s", yytext); k=atoi(yytext);
          if(k%7==0) printf("\nIt is divisible by 7");
        }
%%
int main()
{
        printf(" \n This is my program....... Welcome ");
        yylex();

Quote:}

 
 
 

Problem with Lex

Post by Andrey Simonenk » Thu, 06 Jun 2002 18:13:25


yylex() just outputs unrecognized tokens to stdout (unmatched scanner
input is echoed to stdout). You should add Lex rule to eat
other tokens.



Quote:> hi!

> i am new to lex nd yacc. I am finding a strange prob wit it. if u see
> the below code, i am trying to match a pattern of only umbers while, i
> see that yytext contains even characters.ie it recognizes..."sr34" and
> prints it while it should nt do it, as far as my knowledge
> goes........please help.

> thanx

> natkhat

> %{
>         int k;
> %}
> num     -?(([0-9]+)|(0-9]*\.[0-9]+))
> %%
> {num}   { printf("%s", yytext); k=atoi(yytext);
>           if(k%7==0) printf("\nIt is divisible by 7");
>         }
> %%
> int main()
> {
>         printf(" \n This is my program....... Welcome ");
>         yylex();
> }


 
 
 

Problem with Lex

Post by Jens.Toerr.. » Thu, 06 Jun 2002 18:31:59



> hi!
> i am new to lex nd yacc. I am finding a strange prob wit it. if u see
> the below code, i am trying to match a pattern of only umbers while, i
> see that yytext contains even characters.ie it recognizes..."sr34" and
> prints it while it should nt do it, as far as my knowledge
> goes........please help.
> thanx
> natkhat
> %{

#include <stdio.h>
#include <stdlib.h>

Quote:>         int k;
> %}
> num     -?(([0-9]+)|(0-9]*\.[0-9]+))
> %%
> {num}   { printf("%s", yytext); k=atoi(yytext);
>           if(k%7==0) printf("\nIt is divisible by 7");
>         }
> %%
> int main()
> {
>         printf(" \n This is my program....... Welcome ");
>         yylex();
> }

You know that per default lex prints everything out it has *no* rule
for? In order not to print out input that didn't match you need an
additional rule like

%%
{num}   { printf("%s", yytext); k=atoi(yytext);
          if(k%7==0) printf("\nIt is divisible by 7");
        }
.*      /* dump lines not consisting of numbers only */
%%

If, on the other hand, you want to get all number in a line (i.e. get at
the 34 in "sr34") you need

%%
{num}   { printf("%s", yytext); k=atoi(yytext);
          if(k%7==0) printf("\nIt is divisible by 7");
        }
.       /* dump all non-number characters */
%%

                                        Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046