Hi Pranay,
the declarations of x and y are fine. Give the following a shot (I added
yywrap as it wouldn't compile on my system otherwise -- I did not track
down why)
%{
#include <stdio.h>
int x=0,y=0;
%}
%%
\n { ++x; ++y; printf("Found newline\n"); }
. { ++y; printf("Found something else\n"); }
%%
int main(void)
{
yyin = fopen("input.txt", "r");
yylex();
printf("No. of lines is %d and no. of characters are %d\n", x, y);
exit(EXIT_SUCCESS);
}
int yywrap (void){
return 1;
}
Cheers, Martin
pranay agarwal wrote:
> hi, i am using lex for the first time , i am trying to take input from a
> file and not from the terminal , however i am facing problem please help
> here is my lex file :
>
>
> %{
> #include <stdio.h>
> int x=0,y=0;
> %}
>
> %%
> '\n' ++x,++y;
> '.' ++y;
> %%
> main()
> {
> extern FILE * yyin;
> yyin= fopen("/home/username/example_test","r");
> yyrestart(yyin);
> printf("No. of lines is %d and no. of characters are %d\n",x,y);
> }
>
> the name of this file is test.l and the name of the input file is
> example_test
> i tried the following in command line in linux:
> lex test.c
> cc lex.yy.c -o test -ll
> ./test
>
> and the output is 0 for both x and y when it should be 2 and 20 respectively
> .
>
> Is there a mistake in declaring the global variables x ,y?
>
>
>
--
Martin Alexander Neumann
Mobile: +49 (0) 175 18 22 121
Phone: +49 (0) 5373 506 112
eMail: mar...@gm...
GPG key: 4096R/07F05842
3F12 9D7D DD95 062F 52AF 3848 5691 CDAA 07F0 5842
How to trust CAcert? - http://wiki.cacert.org/ImportRootCert
|