digit [0-9]
letter [A-Za-z]
%{
int count;
%}
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
%%
int main(void) {
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
printf语句不起作用。你能解释我应该在这段代码中包含什么。
如果yywrap
有错误 - 只需添加%option noyywrap
:
digit [0-9]
letter [A-Za-z]
%{
int count;
%}
%option noyywrap
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
%%
int main(void) {
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
然后编译:
flex f.l
gcc lex.yy.c
运行并且不要忘记最后发送EOF(使用Ctrl-D):
./a.out
a a a
number of identifiers = 3