在野牛中出现错误没有声明类型
Getting error no declared type in bison
我正在做家庭作业,但 bison 文件中有一些错误。我收到类似 'blabla' 没有声明类型的错误。我的代码和错误如下。我正在尝试做一个类型检查器,我有一个 header、flex 和一个 bison 文件。 Flex 返回我需要的令牌。我写了一个函数来制作一个属性,然后我检查两个属性类型是否相同。如果不一样,我会给出一个错误输出,但是当我编译它时,我得到了这些错误。我没有弄清楚。我做错什么了?
在野牛文件中:
AttrNode * makeATTR(char * v, AttrType t);
%union
{
ValType val;
char *text;
AttrNode *attrPtr;
}
%token <text> tOPEN tCLOSE tSELF tLEND tCOURSE tCLASS ....continue
day_type: tMON { $$ = makeATTR (,DAY);}
| tTUE { $$ = makeATTR (,DAY);}
| tWED { $$ = makeATTR (,DAY);}
| tTHU { $$ = makeATTR (,DAY);}
| tFRI { $$ = makeATTR (,DAY);}
;
attr_val: tSTRING {$$= makeATTR(,STRING);}
| tNUM {$$= makeATTR(,NUMBER);}
| tTIME {$$ = makeATTR(,TIME);}
| day_type { $$ = makeATTR (,DAY);}
;
attr_name: tNAME {$$= makeATTR(,STRING);}
| tCODE {$$= makeATTR(,STRING);}
| tTYPE {$$= makeATTR(,STRING);}
| tCRN {$$= makeATTR(,NUMBER);}
| tSECTION {$$= makeATTR(,STRING);}
| tCAPACITY {$$= makeATTR(,NUMBER);}
| tSTART {$$ = makeATTR(,TIME);}
| tEND {$$ = makeATTR(,TIME);}
| tDAY { $$ = makeATTR (,DAY);}
;
attribute: attr_name attr_val {if(.type!=.type)
printf("Mismatch error");}
....
AttrNode * makeATTR(char * v, AttrType t)
{
AttrNode * ret = (AttrNode*) malloc(sizeof(AttrNode));
result->thisNodeType = Attr;
result->attrNodePtr = (AttrNode*)malloc(sizeof(AttrNode));
result->attrNodePtr->AttrNode.value=v;
result->attrNodePtr->AttrNode.type=t;
return (result);
}
在 .h 文件中
typedef enum {Attr,Tree } NodeType;
typedef enum {STRING,NUMBER,TIME,DAY} AttrType;
//For String
typedef struct AttrNode
{
char * value;
AttrType * type;
}AttrNode;
typedef struct TreeNode
{
NodeType thisNodeType;
AttrNode *attrNodePtr;
}TreeNode;
AttrNode * makeATTR(char * v, AttrType t);
错误
hw4.y:25.24-25: $$ of `day_type' has no declared type
hw4.y:26.27-28: $$ of `day_type' has no declared type
hw4.y:27.27-28: $$ of `day_type' has no declared type
hw4.y:28.27-28: $$ of `day_type' has no declared type
hw4.y:29.27-28: $$ of `day_type' has no declared type
hw4.y:31.26-27: $$ of `attr_val' has no declared type
hw4.y:32.26-27: $$ of `attr_val' has no declared type
hw4.y:32.39-40: integer out of range: `'
hw4.y:33.26-27: $$ of `attr_val' has no declared type
hw4.y:33.40-41: integer out of range: `'
hw4.y:34.30-31: $$ of `attr_val' has no declared type
hw4.y:34.45-46: of `attr_val' has no declared type
hw4.y:36.24-25: $$ of `attr_name' has no declared type
hw4.y:37.26-27: $$ of `attr_name' has no declared type
hw4.y:38.32-33: $$ of `attr_name' has no declared type
hw4.y:39.26-27: $$ of `attr_name' has no declared type
hw4.y:40.25-26: $$ of `attr_name' has no declared type
hw4.y:40.38-39: integer out of range: `'
hw4.y:41.29-30: $$ of `attr_name' has no declared type
hw4.y:42.30-31: $$ of `attr_name' has no declared type
hw4.y:42.43-44: integer out of range: `'
hw4.y:43.27-28: $$ of `attr_name' has no declared type
hw4.y:43.41-42: integer out of range: `'
hw4.y:44.25-26: $$ of `attr_name' has no declared type
hw4.y:44.39-40: integer out of range: `'
hw4.y:45.26-27: $$ of `attr_name' has no declared type
hw4.y:47.41-42: of `attribute' has no declared type
hw4.y:47.50-51: of `attribute' has no declared type
如果你告诉bison/yacc有不止一种语义类型(带有%union
声明),那么你只能使用$n
和$$
作为终端和具有声明类型的非终端。在这种情况下,bison 告诉您您正在尝试为没有声明类型的非终端设置 $$
。这是不可能的,因为 bison 在不知道要分配给哪个联合成员的情况下无法生成适当的赋值语句。
所以你需要声明非终端的类型:
%type <something> day_type attr_val ...
此外,您有 $n
个超出范围的引用。比如制作中:
attr_val: ...
| tNUM {$$= makeATTR(,NUMBER);}
右边只有一个符号(tNUM
),所以没有</code>。因此出现错误消息 ("integer out of range: `'")。我猜你的意思是 <code>
.
我正在做家庭作业,但 bison 文件中有一些错误。我收到类似 'blabla' 没有声明类型的错误。我的代码和错误如下。我正在尝试做一个类型检查器,我有一个 header、flex 和一个 bison 文件。 Flex 返回我需要的令牌。我写了一个函数来制作一个属性,然后我检查两个属性类型是否相同。如果不一样,我会给出一个错误输出,但是当我编译它时,我得到了这些错误。我没有弄清楚。我做错什么了?
在野牛文件中:
AttrNode * makeATTR(char * v, AttrType t);
%union
{
ValType val;
char *text;
AttrNode *attrPtr;
}
%token <text> tOPEN tCLOSE tSELF tLEND tCOURSE tCLASS ....continue
day_type: tMON { $$ = makeATTR (,DAY);}
| tTUE { $$ = makeATTR (,DAY);}
| tWED { $$ = makeATTR (,DAY);}
| tTHU { $$ = makeATTR (,DAY);}
| tFRI { $$ = makeATTR (,DAY);}
;
attr_val: tSTRING {$$= makeATTR(,STRING);}
| tNUM {$$= makeATTR(,NUMBER);}
| tTIME {$$ = makeATTR(,TIME);}
| day_type { $$ = makeATTR (,DAY);}
;
attr_name: tNAME {$$= makeATTR(,STRING);}
| tCODE {$$= makeATTR(,STRING);}
| tTYPE {$$= makeATTR(,STRING);}
| tCRN {$$= makeATTR(,NUMBER);}
| tSECTION {$$= makeATTR(,STRING);}
| tCAPACITY {$$= makeATTR(,NUMBER);}
| tSTART {$$ = makeATTR(,TIME);}
| tEND {$$ = makeATTR(,TIME);}
| tDAY { $$ = makeATTR (,DAY);}
;
attribute: attr_name attr_val {if(.type!=.type)
printf("Mismatch error");}
....
AttrNode * makeATTR(char * v, AttrType t)
{
AttrNode * ret = (AttrNode*) malloc(sizeof(AttrNode));
result->thisNodeType = Attr;
result->attrNodePtr = (AttrNode*)malloc(sizeof(AttrNode));
result->attrNodePtr->AttrNode.value=v;
result->attrNodePtr->AttrNode.type=t;
return (result);
}
在 .h 文件中
typedef enum {Attr,Tree } NodeType;
typedef enum {STRING,NUMBER,TIME,DAY} AttrType;
//For String
typedef struct AttrNode
{
char * value;
AttrType * type;
}AttrNode;
typedef struct TreeNode
{
NodeType thisNodeType;
AttrNode *attrNodePtr;
}TreeNode;
AttrNode * makeATTR(char * v, AttrType t);
错误
hw4.y:25.24-25: $$ of `day_type' has no declared type
hw4.y:26.27-28: $$ of `day_type' has no declared type
hw4.y:27.27-28: $$ of `day_type' has no declared type
hw4.y:28.27-28: $$ of `day_type' has no declared type
hw4.y:29.27-28: $$ of `day_type' has no declared type
hw4.y:31.26-27: $$ of `attr_val' has no declared type
hw4.y:32.26-27: $$ of `attr_val' has no declared type
hw4.y:32.39-40: integer out of range: `'
hw4.y:33.26-27: $$ of `attr_val' has no declared type
hw4.y:33.40-41: integer out of range: `'
hw4.y:34.30-31: $$ of `attr_val' has no declared type
hw4.y:34.45-46: of `attr_val' has no declared type
hw4.y:36.24-25: $$ of `attr_name' has no declared type
hw4.y:37.26-27: $$ of `attr_name' has no declared type
hw4.y:38.32-33: $$ of `attr_name' has no declared type
hw4.y:39.26-27: $$ of `attr_name' has no declared type
hw4.y:40.25-26: $$ of `attr_name' has no declared type
hw4.y:40.38-39: integer out of range: `'
hw4.y:41.29-30: $$ of `attr_name' has no declared type
hw4.y:42.30-31: $$ of `attr_name' has no declared type
hw4.y:42.43-44: integer out of range: `'
hw4.y:43.27-28: $$ of `attr_name' has no declared type
hw4.y:43.41-42: integer out of range: `'
hw4.y:44.25-26: $$ of `attr_name' has no declared type
hw4.y:44.39-40: integer out of range: `'
hw4.y:45.26-27: $$ of `attr_name' has no declared type
hw4.y:47.41-42: of `attribute' has no declared type
hw4.y:47.50-51: of `attribute' has no declared type
如果你告诉bison/yacc有不止一种语义类型(带有%union
声明),那么你只能使用$n
和$$
作为终端和具有声明类型的非终端。在这种情况下,bison 告诉您您正在尝试为没有声明类型的非终端设置 $$
。这是不可能的,因为 bison 在不知道要分配给哪个联合成员的情况下无法生成适当的赋值语句。
所以你需要声明非终端的类型:
%type <something> day_type attr_val ...
此外,您有 $n
个超出范围的引用。比如制作中:
attr_val: ...
| tNUM {$$= makeATTR(,NUMBER);}
右边只有一个符号(tNUM
),所以没有</code>。因此出现错误消息 ("integer out of range: `'")。我猜你的意思是 <code>
.