Bison%代码顶部错误

时间:2010-02-21 12:34:02

标签: yacc bison

%code top命令在parser.tab.h文件中不包含其内容(应该这样做,对吧?)。野牛版是2.4.1。这个(简化的)代码有什么问题?

%{
  #include <stdlib.h>
  #include <string.h>
  #include <stdio.h>
  #include <io.h>
  #define  YYDEBUG 0
  int  errors;
%}

%code top {

  struct DICT
  {
    char *Name;
    int  Offs;
    int  Size;
    struct DICT *Next;
  };

  typedef struct DICT DICT;

  struct NODE
  {
    int  ID;
    int  Value;
    DICT *Var;
    struct NODE *Left;
    struct NODE *Right;
  };

  typedef struct NODE NODE;
}

%{
  NODE *Tree = 0;

  NODE *Node(int ID, int Value, DICT *Var, NODE *Left, NODE *Right);

  void yyerror(char *s)
  {
    errors++;
    printf("%s\n", s);
  }
%}

  %no_lines

  %union
  {
    int     Value;
    char    *ID;
    NODE    *Node;
  }

编辑: “%code requires”问题得到了解决,但又出现了另一个问题:

parser.tab.h:40:错误:重新定义'struct DICT'

parser.tab.h:47:错误:重新定义typedef'DICT'

parser.tab.c:145:错误:之前的'DICT'声明就在这里

1 个答案:

答案 0 :(得分:1)

使用%code top不会将代码插入标题中,而只会插入源文件中。它是well documented here

我猜%code provides(或%code requires)会更合适,因为它会在源文件和头文件中插入定义。

相关问题