我正在與詞法分析。爲此,我使用Flex
並提取以下問題。函數'yylex':'變量'未聲明
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;
}
然後,我用下面的命令,它正常工作,創造lex.yy.c
。
Rezwans的iMac:laqb-2 rezwan $彎曲work.l
然後,我用下面的命令。
Rezwans的iMac:laqb-2 rezwan $ GCC的lex.yy.c -Ob
並獲得以下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
我沒有得到上述error
,如果我像這樣修改work.l
文件。
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;
}
也就是說,如果我這一行int cnt = 0,num_lines=0,num_chars=0;
之前使用1 tab
,它工作正常。
現在我有兩個問題:
這是行前
int cnt = 0,num_lines=0,num_chars=0;
配套使用1 tab
?爲什麼?邏輯解釋。是解決這個錯誤的另一種解決方案嗎?
您是否閱讀過有關定義部分格式的文檔? – molbdnilo
是啊@molbdnilo。但我找不到這樣的人。 –