1
我跟着Flex and Bison tutorial學習flex &野牛, 但我卡住了。當我編譯 「G ++ snazzle.tab.c的lex.yy.c -lfl -o snazzle」, 我得到這些錯誤消息:使用野牛作出可執行文件,但錯誤
snazzle.tab.c: In function ‘int yyparse()’:
snazzle.tab.c:1403: warning: deprecated conversion from string constant to ‘char*’
snazzle.tab.c:1546: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccFyBCBm.o: In function `yyparse':
snazzle.tab.c:(.text+0x1e0): undefined reference to `yylex'
collect2: ld returned 1 exit status
我ENV是Ubuntu的10.04 野牛2.4.1 彎曲2.5.35
我仍然無法找到問題。首先我用「bison -o snazzle.y」編譯,然後分別生成兩個snazzle.tab.c和snazzle.tab.h。最後我編譯「g ++ snazzle.tab.c lex.yy.c -lfl -o snazzle」。我的代碼follew:
snazzle.l
%{
#include <cstdio>
#include <iostream>
using namespace std;
#include "snazzle.tab.h"
%}
%%
[ \t] ;
[0-9]+\.[0-9]+ { yylval.fval = atof(yytext); return FLOAT; }
[0-9]+ { yylval.ival = atoi(yytext); return INT; }
[a-zA-Z0-9]+ {
// we have to copy because we can't rely on yytext not changing underneath us:
yylval.sval = strdup(yytext);
return STRING;
}
. ;
%%
snazzle.y
%{
#include <cstdio>
#include <iostream>
using namespace std;
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;
void yyerror(char *s);
%}
%union {
int ival;
float fval;
char *sval;
}
%token <ival> INT
%token <fval> FLOAT
%token <sval> STRING
%%
snazzle:
INT snazzle { cout << "bison found an int: " << $1 << endl; }
| FLOAT snazzle { cout << "bison found a float: " << $1 << endl; }
| STRING snazzle { cout << "bison found a string: " << $1 << endl; }
| INT { cout << "bison found an int: " << $1 << endl; }
| FLOAT { cout << "bison found a float: " << $1 << endl; }
| STRING { cout << "bison found a string: " << $1 << endl; }
;
%%
main() {
// open a file handle to a particular file:
FILE *myfile = fopen("a.snazzle.file", "r");
// make sure it is valid:
if (!myfile) {
cout << "I can't open a.snazzle.file!" << endl;
return -1;
}
// set flex to read from it instead of defaulting to STDIN:
yyin = myfile;
// parse through the input until there is no more:
do {
yyparse();
} while (!feof(yyin));
}
void yyerror(char *s) {
cout << "EEK, parse error! Message: " << s << endl;
// might as well halt now:
exit(-1);
}
除非你像定義YY_DECL那樣做了一些奇怪的事情,或者使用了一些額外的參數來flex,yylex應該在lex.yy.c中定義。最好的猜測是,你實際上並沒有運行flex,或者它失敗了,並且你有一個老的/空的lex.yy.c在你偶然發現的地方。沒有更多的細節(實際的命令行運行bison/flex和.y和.l文件的實際內容),我懷疑你會得到很多幫助。 –
我仍然無法找到問題。 – user997948