parser.bison: error: invalid use of undefined type ‘struct X’
parser.bison: error: invalid use of undefined type ‘struct X’
我正在为 Matlab 在 C 上构建一个编译器,它使用算术和布尔运算符,然后我尝试获取变量属性野牛开始抱怨:
parser.bison:62:5: error: invalid use of undefined type ‘struct List’
cmdlist: cmdss {$$ = ast_list(,NULL);}
Flex 似乎工作正常,而 bison 不识别 ast 的 headers。
弹性:
%{
// 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"); }
%%
野牛:
%启动程序;
%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 = ; }
cmdlist: cmdss {$$ = ast_list(,NULL);}
| cmdss NL cmdlist {$$ = ast_list(,);}
cmdss: VAR ATR expr {$$ = ast_atrib(,);}
'...'
%%
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;
}
这只是一个普通的 C 错误(并说明了为什么 typedef
指针类型的名称不是一个好主意)。
ast_list
的声明不正确;你 return struct CommandList*
但声明说 struct List
。 (你打算只写 List
。)
当您尝试编译时,您应该会看到一个明显的错误 ast.c
。
我正在为 Matlab 在 C 上构建一个编译器,它使用算术和布尔运算符,然后我尝试获取变量属性野牛开始抱怨:
parser.bison:62:5: error: invalid use of undefined type ‘struct List’ cmdlist: cmdss {$$ = ast_list(,NULL);}
Flex 似乎工作正常,而 bison 不识别 ast 的 headers。
弹性:
%{
// 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"); }
%%
野牛: %启动程序;
%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 = ; }
cmdlist: cmdss {$$ = ast_list(,NULL);}
| cmdss NL cmdlist {$$ = ast_list(,);}
cmdss: VAR ATR expr {$$ = ast_atrib(,);}
'...'
%%
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;
}
这只是一个普通的 C 错误(并说明了为什么 typedef
指针类型的名称不是一个好主意)。
ast_list
的声明不正确;你 return struct CommandList*
但声明说 struct List
。 (你打算只写 List
。)
当您尝试编译时,您应该会看到一个明显的错误 ast.c
。