我使用C标记的并集样式结构来表示AST节点。它们看起来像这样:

struct A_var_ {
  enum {
    A_simpleVar,
    A_fieldVar,
    A_subscriptVar
  } kind;

  A_pos pos;

  union {
    S_symbol simple;

    struct {
      A_var var;
      S_symbol sym;
    } field;

    struct {
      A_var var;
      A_exp exp;
    } subscript;
  } u;
};

现在,我正在尝试对节点类型进行调度,如下所示:
switch(node->kind) {
  case A_simpleVar:
    ...
}

但是,我遇到了像Can't resolve variable A_simpleVar这样的错误。我怎样才能做到这一点?我在windows上使用Cmake,编译器是gcc。
对于以下代码,我得到的具体错误如下:
struct expty transExp(S_table venv, S_table tenv, A_exp a)
{
  switch (a->kind) {
    case A_letExp:
      struct expty exp;
      A_decList d;
      S_beginScope(venv);
      S_beginScope(tenv);
      for (d = a->u.let.decs; d; d=d->tail) {
        transDec(venv, tenv, d->head);
      }
      exp = transExp(venv, tenv, a->u.let.body);
      S_endScope(tenv);
      S_endScope(venv);
      return exp;

    case A_opExp:
      A_oper oper = a->u.op.oper;
      struct expty left = transExp(venv, tenv, a->u.op.left);
      struct expty right = transExp(venv, tenv, a->u.op.right);
      if (oper == A_plusOp) {
        if (left.ty->kind != Ty_int)
          EM_error(a->u.op.left->pos, "integer required");
        if (right.ty->kind != Ty_int)
          EM_error(a->u.op.right->pos, "integer required");
        return expTy(NULL, Ty_Int());
      } // I need to cover other operators as well, but not for prototype
  }
}

是:
C:\Users\Fotis\Documents\Devel\grimoire\Books\ModernCompilerC\semant.c: In function 'transExp':C:\Users\Fotis\Documents\Devel\grimoire\Books\ModernCompilerC\semant.c:28:7: error: a label can only be part of a statement and a declaration is not a statement struct expty exp; ^C:\Users\Fotis\Documents\Devel\grimoire\Books\ModernCompilerC\semant.c:29:7: error: expected expression before 'A_decList' A_decList d; ^C:\Users\Fotis\Documents\Devel\grimoire\Books\ModernCompilerC\semant.c:32:12: error: 'd' undeclared (first use in this function) for (d = a->u.let.decs; d; d=d->tail) {
还需要注意的是,函数的参数是指向typedefed的结构的指针,即A_expA_exp_ *

最佳答案

您正在编译C++编译器,而不是C编译器。两个句柄枚举不同。
在C中,所有枚举都在全局范围内可见。在C++中,它们是由包含类型(如果有的话)限定的。因此,您需要在枚举值前面加上结构名称:

switch(node->kind) {
  case A_var_::A_simpleVar:
    ...
}

10-06 06:02