> 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