-->

In function ‘yylex’: 'Variable’ undeclared

2019-02-18 22:23发布

问题:

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:

  1. Is necessary use 1 tab before this line int cnt = 0,num_lines=0,num_chars=0;? why? explain logically.

  2. Is another solution to solve this error ?

回答1:

I'm not very sure about the tab problem but one explanation is that if you don't place tab and write in the first section something like:

int cnt = 0;

Then note that in the first section you may also write "shortcuts" like:

Digit  [0-9] 

which is a regular expression that defines what digit is instead of writing all the time [0-9] to denote digit.

So when writing int cnt = 0; in the first column without using tab is like defining the keyword int (that's probably why the error tells you did you mean int’?). So tab is a way to distinguish the two above cases.

According to flex in order to write c/c++ code it needs to be inside: %{ ... c/c++ code... %} so for your example:

%{
   int cnt = 0,num_lines=0,num_chars=0;
%}

So I thing the best way is to write your c-code inside %{ %}.