词法分析器flex下如何输出?
我使用flex作了一个词法分析程序,输出了以下程序的词法结果:
#include <stdio.h>
int main()
{
int i=0, j=0, k=0;
for(i = 0; i<10; i++)
{
printf("this is only a test!\n");
k = j++ + ++i;
}
}
词法分析的结果:可以看到结果的格式很不好,最前面老是有空格,
不知道怎么能够去掉。
include:257
<:60
stdio:257
.:46
h:257
>:62
int:290
main:257
(:40
):41
{:123
int:290
i:257
=:61
0:258
,:44
j:257
=:61
0:258
,:44
k:257
=:61
0:258
;:59
for:311
(:40
i:257
=:61
0:258
;:59
i:257
<:60
10:258
;:59
i:257
++:262
):41
{:123
printf:257
(:40
"this is only a test!\n":259
):41
;:59
k:257
=:61
j:257
++:262
+:43
++:262
i:257
;:59
}:125
}:125
=============================================================
词法分析的scan.l的脚本大体如下:
"<<" { count(); return(LEFT_OP); }
"++" { count(); return(INC_OP); }
"--" { count(); return(DEC_OP); }
"->" { count(); return(PTR_OP); }
"&&" { count(); return(AND_OP); }
"||" { count(); return(OR_OP); }
"<=" { count(); return(LE_OP); }
">=" { count(); return(GE_OP); }
"==" { count(); return(EQ_OP); }
"!=" { count(); return(NE_OP); }
";" { count(); return(';'); }
int main()
{
int x=0;
while(x = yylex())
{
printf(":%d\n", x);
}
return 0;
}
我的问题是如何去掉,词法分析结果中每个行最前面的空格。