代码之家  ›  专栏  ›  技术社区  ›  Yimin Rong

bison/yacc使用union%中的defines

  •  1
  • Yimin Rong  · 技术社区  · 6 年前

    我有 TOKEN_LEN 在头文件中定义 stuff.h . 我想把它用在 %union 章节:

    %union {
        int ival;
        char sval[TOKEN_LEN];
    }
    

    但是,包括在 %{ ... %} 部分只是逐字复制块, bison 将出错,因为 托肯 未定义。

    我可以用 flex -DTOKEN_LEN=100 ... 在makefile中,但这意味着 托肯 在两个地方定义。是的,在 flex 而不是 野牛 !

    我试图在makefile中单独定义它,如下所示:

    DEFS=TOKEN_LEN=100
    

    然后使用:

    flex -D${DEFS} ...
    bison -D${DEFS} ...
    gcc -D${DEFS} ...
    

    但是 make 不喜欢这样,因为这个原因:

    <command line>:1: error: %define variable 'TOKEN_LEN' is not used
    Makefile:4: recipe for target 'stuff' failed
    make: *** [stuff] Error 1
    

    有解决办法吗?最好是 托肯 仅定义在 东西。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Chris Dodd    6 年前

    %code

    %code requires {
    #include "stuff.h"
    }
    

    YYSTYPE %union

    %{ %} #include "stuff.h" #include "y.tab.h"