我正在尝试使用flex和bison与qt。有很多关于它的网络资源。他们都使用QMAKE_EXTRA_COMPILERS来添加预编译。但是当我编译代码时,我总是得到yyerror和yywrap的LNK2019。我的代码是遵循:
专业档案
CONFIG += console
CONFIG -= app_bundle
LIBS += -lfl
FLEXSOURCES = lexer.l
BISONSOURCES = parser.y
OTHER_FILES += \
$$FLEXSOURCES \
$$BISONSOURCES
QT += core gui script
SOURCES += main.cpp
TEMPLATE = app
bisonsource.input = BISONSOURCES
bisonsource.output = ${QMAKE_FILE_BASE}.cpp
bisonsource.commands = bison -d --defines=${QMAKE_FILE_BASE}.h -o ${QMAKE_FILE_BASE}.cpp ${QMAKE_FILE_IN}
bisonsource.variable_out = SOURCES
bisonsource.name = Bison Sources ${QMAKE_FILE_IN}
bisonsource.CONFIG += target_predeps
QMAKE_EXTRA_COMPILERS += bisonsource
bisonheader.input = BISONSOURCES
bisonheader.output = ${QMAKE_FILE_BASE}.h
bisonheader.commands = @true
bisonheader.variable_out = HEADERS
bisonheader.name = Bison Headers ${QMAKE_FILE_IN}
bisonheader.CONFIG += target_predeps no_link
QMAKE_EXTRA_COMPILERS += bisonheader
flexsource.input = FLEXSOURCES
flexsource.output = ${QMAKE_FILE_BASE}.cpp
flexsource.commands = flex --header-file=${QMAKE_FILE_BASE}.h -o ${QMAKE_FILE_BASE}.cpp ${QMAKE_FILE_IN}
flexsource.variable_out = SOURCES
flexsource.name = Flex Sources ${QMAKE_FILE_IN}
flexsource.CONFIG += target_predeps
QMAKE_EXTRA_COMPILERS += flexsource
flexheader.input = FLEXSOURCES
flexheader.output = ${QMAKE_FILE_BASE}.h
flexheader.commands = @true
flexheader.variable_out = HEADERS
flexheader.name = Flex Headers ${QMAKE_FILE_IN}
flexheader.CONFIG += target_predeps no_link
QMAKE_EXTRA_COMPILERS += flexheader
lexer.l
%{
#include "parser.h"
extern void yyerror(const char *s);
%}
%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }
"(" { return OP; }
")" { return CP; }
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
\n { return EOL; }
"//".*
[ \t] { /* ignore white space */ }
. { yyerror("Mystery character\n"); }
%%
parser.y
%{
#include <stdio.h>
// yylex is a function generated by Flex and we must tell to Bison that it is
// defined in other place.
extern int yylex(void);
// Bison uses the yyerror function for informing us when a parsing error has
// occurred.
void yyerror(const char *s);
%}
/* declare tokens*/
%token NUMBER
%token ADD SUB MUL DIV ABS
%token OP CP
%token EOL
%%
calclist: /*nothing */
| calclist exp EOL { printf("= %d\n>", $2); }
| calclist EOL { printf("> "); }/* blank line or a comment */
;
exp: factor
| exp ADD exp { $$ = $1 + $3; }
| exp SUB factor { $$ = $1 - $3; }
| exp ABS factor { $$ = $1 | $3; }
;
factor: term
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1 / $3; }
;
term: NUMBER
| ABS term { $$ = $2 >= 0? $2 : - $2; }
| OP exp CP { $$ = $2; }
;
%%
void yyerror(char *s)
{
fprintf(stderr, "error: %s\n", s);
}
的main.cpp
#include <QtCore>
// This header is generated by Flex.
#include "lexer.h"
// This header is generated by bison.
#include "parser.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QString str("1+1");
// Insert the string into the input stream.
YY_BUFFER_STATE bufferState = yy_scan_string(str.toUtf8().constData());
// Parse the string.
yyparse();
// flush the input stream.
yy_delete_buffer(bufferState);
return app.exec();
}
答案 0 :(得分:2)
yywrap
未定义,因为您从未定义它。您必须定义它,或告诉flex它不需要它。我假设你不需要它,所以你应该添加
%option noyywrap
到你的flex输入文件(lexer.l
)。 (它位于第一个%%
之前,但不在prolog内部(由%{
和%}
包围的部分。)(有关详细信息,请参阅flex manual。)
yyerror
未定义(在词法分析器文件中),因为它是在解析器文件中定义的,并且您正在将解析器编译为C ++程序,而您将词法分析器编译为C程序。
你不能在任何一个文件中使用C ++,所以你可以将它们编译为C.或者你可以将它们编译为C ++。或者您可以将yyerror声明为extern "C"
。如果您将解析器编译为C程序,那么您需要在yyparse
中将extern "C"
声明为main.cpp
,Accept requests from these HTTP referrers (web sites)
使用QT,因此必须是C ++。