I am working with Lexical Analysis. For this I am using Flex
and I fetch following Problems.
work.l
int cnt = 0,num_lines=0,num_chars=0; // Problem here.
%%
[" "]+[a-zA-Z0-9]+ {++cnt;}
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int yywrap()
{
return 1;
}
int main()
{ yyin = freopen("in.txt", "r", stdin);
yylex();
printf("%d %d %d\n", cnt, num_lines,num_chars);
return 0;
}
then, I use following command and it work properly and create lex.yy.c
.
Rezwans-iMac:laqb-2 rezwan$ flex work.l
then, I use following command.
Rezwans-iMac:laqb-2 rezwan$ gcc lex.yy.c -o b
and getting following error
:
work.l: In function ‘yylex’:
work.l:3:4: error: ‘cnt’ undeclared (first use in this function); did you mean int’?
[" "]+[a-zA-Z0-9]+ {++cnt;}
^~~
int
work.l:3:4: note: each undeclared identifier is reported only once for each function it appears in
work.l:4:4: error: ‘num_lines’ undeclared (first use in this function)
\n {++num_lines; ++num_chars;}
^~~~~~~~~
work.l:4:17: error: ‘num_chars’ undeclared (first use in this function); did you mean ‘num_lines’?
\n {++num_lines; ++num_chars;}
^~~~~~~~~
num_lines
work.l: In function ‘main’:
work.l:15:23: error: ‘cnt’ undeclared (first use in this function); did you mean ‘int’?
return 0;
^
int
work.l:15:28: error: ‘num_lines’ undeclared (first use in this function)
return 0;
^
work.l:15:38: error: ‘num_chars’ undeclared (first use in this function); did you mean ‘num_lines’?
return 0;
^
num_lines
I am not getting above error
, if I modify work.l
file like this .
int cnt = 0,num_lines=0,num_chars=0; // then work properly above command.
%%
[" "]+[a-zA-Z0-9]+ {++cnt;}
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int yywrap()
{
return 1;
}
int main()
{ yyin = freopen("in.txt", "r", stdin);
yylex();
printf("%d %d %d\n", cnt, num_lines,num_chars);
return 0;
}
That is to say, If I use 1 tab
before this line int cnt = 0,num_lines=0,num_chars=0;
, it works properly.
Now I have two question:
Is necessary use
1 tab
before this lineint cnt = 0,num_lines=0,num_chars=0;
? why? explain logically.Is another solution to solve this error ?