bison中的多个属性 [英] Multiple attributes in bison
问题描述
我在bison中做语义分析,我想使用与一个标记相关联的多个属性。我的代码的相关部分是:
I am doing semantic analysis in bison and i want to use multiple attribute associated with a token. A related part of my code is:
%union semrec
{
int Type;
char *id;
}
%start prog
%token <id> tIDENT
在这里,我只能使用tIDENT标记中的id属性。我还想将Type属性与tIDENT令牌相关联。为此,我尝试了以下操作:
Here, i can only use the "id" attribute witht the tIDENT token. I also want to associate the "Type" attribute with tIDENT token. To do this, i tried the following:
%token <id> tIDENT
%token <Type> tIDENT
但它给我一个重新声明符号tIDENT的警告。我还尝试了以下操作:
But it gives me a redeclaration warning for token tIDENT. I also tried the following:
%token <id> <Type> tIDENT
它也无效。我能做什么?我想这只是一个小的句法问题。
It also did not work. What can i do? I think this is just a little syntactic problem.
谢谢。
推荐答案
你不能这样做:你必须以这样的方式定义你的%union
,所有具有多个属性的符号都有 struct
定义所有这些属性。像
You cannot do it this way: you have to define your %union
in such a way that all the symbols that have multiple "attributes" have a struct
to define all these "attributes". Something like
%union
{
struct
{
int type;
char *id;
} type_id;
}
%type <type_id> tIDENT
并使用 $ 1.type
code> $ 1.id 等。
and use $1.type
or $1.id
etc.
注意,我非常怀疑你在做正确的事情。机会很高,你将需要一个AST(抽象语法树)。您应该查找有关信息。
Note however that I very much doubt that you're doing the right thing. Chances are high that you will need an AST (Abstract Syntax Tree). You should look for information about that.
这篇关于bison中的多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!