【发布时间】:2019-06-21 08:39:07
【问题描述】:
我想完成一个关于 varlist 声明的解析,
像 varlist:id 逗号 varlist|id。
这时候需要建立一个关于var的列表。
所以我写了这段代码:
varlist: id comma varlist{ createtnode($1.idcontext);}
|id{createtnode($1.idcontext);};
但是我发现$1.idcontext不是我想要的idcontext,应该是这个id token的idcontext。
现在,$1.idcontext 就是这句话“varlist”。如果没有代码操作,此语法可以正常工作。
typedef struct{
int* TC;
int* FC;
}boolcode;
typedef struct {
char* idcontext;
int constvalue;
int chain;
boolcode ftentry;
}includes;
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef struct{
int classify;
includes unique;
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
#endif
VARList: IDENcode COMMA VARList{
if(YYDEBUG)
{printf("6\n");
printf("%s\n",$1.idcontext);
}
varlistnode* newvar=malloc(sizeof(varlistnode));
newvar->varname=$1.idcontext;
newvar->value=0;
newvar->next=NULL;
mynotes->next=newvar;
mynotes=mynotes->next;
}|IDENcode{
if(YYDEBUG)
{printf("7\n");printf("%s\n",$1.idcontext);}
varlistnode* newvar=malloc(sizeof(varlistnode));
newvar->varname=$1.idcontext;
newvar->value=0;
newvar->next=NULL;
mynotes->next=newvar;
mynotes=mynotes->next;
};
单词等待识别:
a,b,c,d
printf()函数的结果:
7
d:
6
c,d:
6
b,c,d:
6
a,b,c,d:enter code here
【问题讨论】:
-
请尽量让您的问题可读。见stackoverflow.com/help/formatting
-
另外,read the manual 总是好的
-
@melpomene '我想完成解析' 也不是英语。如果你要修复它,修复它。
-
@user207421 这只是“不是英语”,因为它没有任何意义。我也不知道它是什么意思,所以我无法改写它。我至少修复了拼写(和一些格式)。
标签: c compiler-construction bison yacc