parser.bison:错误:无效使用未定义类型'struct X'

时间:2016-10-29 11:05:35

标签: compiler-construction bison flex-lexer

我在Matlab上为 C 构建编译器,它正在使用算术和布尔运算符,然后我尝试获取bison开始抱怨的变量属性:

  

parser.bison:62:5:错误:无效使用未定义类型'struct List'    cmdlist:cmdss {$$ = ast_list($ 1,NULL);}

Flex似乎工作正常,野牛无法识别标题。

flex:

  %{
// HEADERS
#include <stdlib.h>
#include "parser.h"

// variables maintained by the lexical analyser
int yyline = 1;
%}

%option noyywrap

%%
[ \t\n]+ {  }
#.*\n { yyline++; }

\-?[0-9]+ {yylval.intValue = atoi(yytext); return INT;}


"=" { return ATR; }
'...'
";" { return NL;  }

\-?[a-z]+ {yylval.varval = strdup(yytext); return VAR;}

. { yyerror("unexpected character"); }
%%
野牛:     %start program;

%union {
  int intValue;
  struct Expression* exprValue; 
  struct Command* command;
  struct CommandList* list;
  char* varval 
}

%type <intValue> INT
%type <exprValue> expr
%type <command> cmdss
%type <list> cmdlist
%type <varval> VAR

%code requires {
  #include <stdio.h>
  #include <stdlib.h>
  #include "ast.h"

  extern int yylex();
  extern int yyline;
  extern char* yytext;
  extern FILE* yyin;
  extern void yyerror(const char* msg);
  List root;
}

%%
program: cmdlist { root = $1; }

cmdlist: cmdss {$$ = ast_list($1,NULL);}
       | cmdss NL cmdlist {$$ = ast_list($1,$3);}

cmdss: VAR ATR expr {$$ = ast_atrib($1,$3);}
'...'
%%

void yyerror(const char* err) {printf("Line %d: %s - '%s'\n", yyline, err, yytext  );}

ast.h:

#ifndef __ast_h__
#define __ast_h__

struct Expression {
  enum {E_INTEGER, E_OPERATION, E_VAR} kind;
  union {
    int value; // for integer values
    char* name; //for a variable
    struct {int operator; struct Expression* left; struct Expression* right;} op; // PLUS MINUS MOD MULT DIV ...
  } attr;
};

struct Command{
  enum{C_ATR, C_IF, C_WHILE, C_FOR}kind;
  union{
    struct{char var[30]; struct Expression* expr;} atrib;
  }attr;
};

struct CommandList{
  struct Command* command;
  struct CommandList* next;
};

typedef struct Expression* Expr; 
typedef struct CommandList* List;
typedef struct Command* Comd;

struct Expr ast_integer(int v);
struct Expr ast_operation(int operator, Expr left, Expr right);
struct List ast_list(Comd cmds, List next);
struct Expr ast_var(char* var);
struct Comd ast_atrib(char* var, Expr e);

#endif

和ast.c:

#include <stdlib.h> // for malloc
#include "ast.h" // AST header
#include <string.h>

List ast_list(Comd cmds, List next) { //LIST
  List node = (List) malloc(sizeof(CommandList));
  node->command = cmds;
  node->next=next;
  return node;
}

Expr ast_integer(int v){ //EXPRESSION -> INTEGER
  Expr node = (Expr) malloc(sizeof(Expression));
  node->kind = E_INTEGER;
  node->attr.value = v;
  return node;
}

Expr ast_operation(int operator, Expr left, Expr right) {  //EXPRESSION -> OPERATION
  Expr node = (Expr) malloc(sizeof(Expression));
  node->kind = E_OPERATION;
  node->attr.op.operator = operator;
  node->attr.op.left = left;
  node->attr.op.right = right;
  return node;
}

Expr ast_var(char* var){ //EXPRESSION -> VARIABEL
  Expr node =(Expr) malloc(sizeof(Expression));
  node->kind = E_VAR;
  node->attr.name = strdup(var);
  return node;
}

Comd ast_atrib(char* var, Expr e){  //COMMAND -> ATRRIBUTION
  Comd node = (Comd) malloc(sizeof(Command));
  node->kind = C_ATR;
  node->attr.atrib.var=strdup(var);
  node->attr.atrib.expr=e;
  return node;
}

1 个答案:

答案 0 :(得分:0)

这只是一个普通的C错误(并说明为什么指针类型的typedef名称是个坏主意。)

ast_list的声明不正确;你返回struct CommandList*,但声明说struct List。 (你的意思是只写List。)

当您尝试编译ast.c时,您会看到一个明确的错误。