簡體   English   中英

在Qt中使用Flex和Bison

[英]Using Flex and Bison with Qt

我正在嘗試將flex和bison與qt一起使用,關於Web的資源很多,它們都使用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"); }

%%

解析器

%{

#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();
}

yywrap是未定義的,因為您從未定義它。 您必須定義它,或告訴flex不需要它。 我認為您不需要它,因此您應該添加

%option noyywrap

到您的flex輸入文件( lexer.l )。 (它在第一個%%之前,但不在序言內部(被%{%}包圍的部分%} 。(有關更多信息,請參見flex手冊 。)

yyerror未定義(在lexer文件中),因為它是在解析器文件中定義的,並且您正在將解析器編譯為C ++程序,而將詞法分析器編譯為C程序。

您在這兩個文件中都不使用C ++,因此可以將兩者都編譯為C。也可以將兩者都編譯為C ++。 或者,您可以將yyerror聲明為extern "C" 如果將解析器編譯為C程序,則需要在main.cpp中將yyparse聲明為extern "C" ,它使用QT,因此必須為C ++。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM