Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Flex + bison + Qt



  • I'm trying to generate a code for parsing a language in my Qt plain C++ project. Lexer and Parser code are working properly. I tried them separately (not in a qt project):
    @
    bison -d -o parser.cpp ./parser.y
    flex -o lexer.cpp ./lexer.l
    g++ -o ./test ./main.cpp ./lexer.cpp ./parser.cpp ./parser.hpp -lfl -m
    @
    Trying to add flex and bison commands to .pro by QMAKE_EXTRA_COMPILERS I'm getting 37 errors.
    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= qt

    SOURCES += main.cpp
    LIBS += -lfl

    OTHER_FILES +=
    parser.y
    lexer.l

    FLEXSOURCES = lexer.l
    BISONSOURCES = parser.y

    bison.commands = bison -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_IN}
    bison.input = BISONSOURCES
    bison.output = parser.cpp
    bison.variable_out = SOURCES
    bison.name = bisonsource ${QMAKE_FILE_IN}
    QMAKE_EXTRA_COMPILERS += bison

    bisonheader.commands = @true
    bisonheader.input = BISONSOURCES
    bisonheader.output = parser.hpp
    bisonheader.variable_out = HEADERS
    bisonheader.name = bisonheader ${QMAKE_FILE_IN}
    bisonheader.depends = parser.cpp
    QMAKE_EXTRA_COMPILERS += bisonheader

    flex.commands = flex -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_IN}
    flex.input = FLEXSOURCES
    flex.output = lexer.cpp
    flex.variable_out = SOURCES
    flex.depends = parser.hpp
    flex.name = flex ${QMAKE_FILE_IN}
    QMAKE_EXTRA_COMPILERS += flex
    @

    errors:
    @
    lexer.cpp: In function 'int yylex()':
    lexer.cpp:562:9: error: 'yy_init' was not declared in this scope
    lexer.cpp:570:11: error: 'yy_start' was not declared in this scope
    .... // and a lot of undeclared errors...
    @



  • have look at qjson project, it uses bison and works for me.


Log in to reply