Search code examples
c++qtbisonflex-lexer

Using Flex and Bison with Qt


I am trying to use flex and bison with qt.There many web resource about it.They all use the QMAKE_EXTRA_COMPILERS to add precompile.But I always get the LNK2019 of the yyerror and yywrap when I compile the code.My code was follow:

pro file

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

Solution

  • yywrap is undefined because you never defined it. You must define it, or tell flex that it is not needed. I assume you don't need it, so you should add

    %option noyywrap
    

    to your flex input file (lexer.l). (It goes before the first %% but not inside the prolog (the part surrounded by %{ and %}.) (See the flex manual for more information.)

    yyerror is not defined (in the lexer file) because it is defined in the parser file, and you are compiling your parser as a C++ program, while you compile your lexer as a C program.

    You don't use C++ in either file, so you could compile both as C. Or you could compile both as C++. Or you could declare yyerror as extern "C". If you compile the parser as a C program, then you'll need to declare yyparse as extern "C" in main.cpp, which uses QT and thus must be C++.