简体   繁体   English

在BISON语义规则中实现多种返回类型

[英]Implementing Multiple Return Types in BISON Semantic Rules

I'm building an AST in BISON, and have created classes to handle building the tree. 我正在BISON中构建一个AST,并创建了用于处理构建树的类。 I've utilized some inherited classes to store different information (an AddExprNode stores an op (ie "+"), LiteralNode stores a literal "1.49", etc.) These inherit from the superclass ASTNode. 我使用了一些继承的类来存储不同的信息(AddExprNode存储一个op(即“+”),LiteralNode存储一个文字“1.49”等)这些继承自超类ASTNode。

I've been provided a grammar file for the language that I'm building my parser for (Micro.) I run into an issue when I could receive either a literal, variable, or full expression as the return type for a primary. 我已经为我正在构建我的解析器(Micro。)的语言提供了一个语法文件。当我收到一个文字,变量或完整表达式作为主要的返回类型时,我遇到了一个问题。

primary           : _OPAREN expr _CPAREN  {
                                          $<node>$ = $2;
                                      };
                | id  {
                          $<var_node>$ = new VarRefNode($1, ASTNodeType::VAR_REF);
                      };
                | _INTLITERAL {
                                  $<lit_node>$ = new LiteralNode(LiteralType::isINT, $1, ASTNodeType::LITERAL);
                                  //$$->printNode();
                              }; 
                | _FLOATLITERAL {
                                  $<lit_node>$ = new LiteralNode(LiteralType::isFLOAT, $1, ASTNodeType::LITERAL);
                                };

Here's my union declaration, and the type definitions: 这是我的联合声明和类型定义:

%type <s> id str var_type any_type
%type <node> expr_prefix factor factor_prefix postfix_expr expr primary expr_list expr_list_tail call_expr
%type <add_node> addop 
%type <mul_node> mulop 
%type <var_node> primary
%type <lit_node> primary

%type <s_table> decl  var_decl param_decl_tail param_decl_list   
%type <s_entry> string_decl param_decl
%type <str_list> id_list id_tail
%type <st_list> func_declarations if_stmt stmt stmt_list func_body func_decl else_part loop_stmt while_stmt

%union{
  SymbolTableEntry * s_entry;
  char * s;
  SymbolTable * s_table;
  std::vector<SymbolTable *> * st_list;
  std::vector<char *> * str_list;
  ASTNode * node;
  AddExprNode * add_node;
  MulExprNode * mul_node;
  VarRefNode * var_node;
  LiteralNode * lit_node;
}

As can be seen above, BISON does not allow you to make the return type ($$) for a single rule (like primary) different based on what tokens you encounter in the rule. 如上所示,BISON不允许您根据您在规则中遇到的令牌,使单个规则(如主要规则)的返回类型($$)不同。

Is there a way to accomplish this? 有没有办法实现这个目标? My google-fu and ability to read the BISON manual seem to be failing me. 我的google-fu和阅读BISON手册的能力似乎让我失望了。

 ASTNode * node; AddExprNode * add_node; MulExprNode * mul_node; VarRefNode * var_node; LiteralNode * lit_node; 

A variable of type ASTNode * can store a pointer to an instance of any subclass of ASTNode . ASTNode *类型的变量可以存储指向ASTNode的任何子类的实例的指针。 So you can get rid of all the foo_node definitions above, just keep the ASTNode * node; 所以你可以摆脱上面的所有foo_node定义,只需保留ASTNode * node; definition and store all of your nodes in that one. 定义并存储该节点中的所有节点。

That way the type of your primary rule can just be node and there's no problem. 这样,您的primary规则的类型可以只是node并且没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM