我试图描述玩具语言的语法。以下是 的内容tokens.lex
:
/* Simple scanner for a toy language */
%{
/* need this for the call to atof() below */
#include <string.h>
#include <math.h>
%}
IDENTIFIER [a-z][a-z0-9]*
DIGIT [0-9]
%%
{DIGIT}+ {
printf("int: %s (%d)\n", yytext, atoi(yytext));
yylval.string = new std::string(yytext, yyleng);
} /* define int type */
{DIGIT}+"."{DIGIT}+ {
printf("float: %s (%d)\n", yytext, atof(yytext));
yylval.string = new std::string(yytext, yyleng);
} /* define float type */
b[\'\"]{IDENTIFIER}[\'\"] {
printf("bstream: %s\n", yytext);
yylval.string = new std::string(yytext, yyleng);
} /* define bstream type */
u[\'\"]{IDENTIFIER}[\'\"] {
printf("ustream: %s\n", yytext);
yylval.string = new std::string(yytext, yyleng);
} /* define ustream type */
if|then|begin|end|procedure|function {
printf( "A keyword: %s\n", yytext );
}
{IDENTIFIER} printf( "identifier: %s\n", yytext );
"+"|"-"|"*"|"/" printf( "operator: %s\n", yytext );
"{"[^}\n]*"}" /* Remove one-line comments */
[ \t\n]+ /* Remove whitespace */
. printf( "Unrecognized character: %s\n", yytext );
%%
int argc;
char **argv;
int main(argc, argv);
{
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}
然后我尝试编译它:
lex tokens.lex && g++ -lfl lex.yy.c
编译器返回几个错误:
tokens.lex:51:20: error: expression list treated as compound expression in initializer [-fpermissive]
tokens.lex:51:20: error: invalid conversion from ‘char**’ to ‘int’ [-fpermissive]
tokens.lex:52:3: error: expected unqualified-id before ‘{’ token
这里有什么问题?我在 C/C++ 方面不是很强,所以我不知道这里发生了什么。请问你能帮帮我吗?谢谢!