在 yacc 中将多种数据类型分配给非终端
我正在开发一个班级项目,我们必须在其中构建一个解析器。我们目前正处于在 yacc 中构建解析器的阶段。目前让我困惑的是我读到您需要为每个非终结符分配一个类型。但在某些情况下,我会遇到这样的问题:
...
%union {
Type dataType;
int integerConstant;
bool boolConstant;
char *stringConstant;
double doubleConstant;
char identifier[MaxIdentLen+1]; // +1 for terminating null
Decl *decl;
List<Decl*> *declList;
}
%token <identifier> T_Identifier
%token <stringConstant> T_StringConstant
%token <integerConstant> T_IntConstant
%token <doubleConstant> T_DoubleConstant
%token <boolConstant> T_BoolConstant
...
%%
...
Expr : /* some rules */
| Constant { /* Need to figure out what to do here */ }
| /* some more rules */
;
Constant : T_IntConstant { $$=$1 }
| T_DoubleConstant { $$=$1 }
| T_BoolConstant { $$=$1 }
| T_StringConstant { $$=$1 }
| T_Null { $$=$1 }
...
你如何为 expr 分配一个类型,因为它有时不能是整数、双精度数或布尔值等?
I'm working on a project for class in which we have to build a parser. We're currently in the stage of building the parser in yacc. The thing currently confusing me is I've read that you need to assign a type to each nonterminal. In some cases though I'll have Something like:
...
%union {
Type dataType;
int integerConstant;
bool boolConstant;
char *stringConstant;
double doubleConstant;
char identifier[MaxIdentLen+1]; // +1 for terminating null
Decl *decl;
List<Decl*> *declList;
}
%token <identifier> T_Identifier
%token <stringConstant> T_StringConstant
%token <integerConstant> T_IntConstant
%token <doubleConstant> T_DoubleConstant
%token <boolConstant> T_BoolConstant
...
%%
...
Expr : /* some rules */
| Constant { /* Need to figure out what to do here */ }
| /* some more rules */
;
Constant : T_IntConstant { $=$1 }
| T_DoubleConstant { $=$1 }
| T_BoolConstant { $=$1 }
| T_StringConstant { $=$1 }
| T_Null { $=$1 }
...
How can you assing a type to expr since can't it sometimes be an integer or double, or bool, etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在规则中添加类型
您可以通过查看 https://www.gnu.org/software/bison/manual/html_node/Action-Types.html#Action-Types 了解更多详细信息。
You can add the type in the rule by
See https://www.gnu.org/software/bison/manual/html_node/Action-Types.html#Action-Types for more details.