【问题标题】:Yacc / Bison parent ruleYacc / Bison 父规则
【发布时间】:2015-02-02 17:59:25
【问题描述】:

在 Yacc / Bison 中,我如何知道父规则以便我可以采取相应的措施?

例如:

Module
    :ModuleName "=" Functions

Functions
    :Functions Function
    | Function

Function
    : DEF ID ARGS BODY
      {
           /* here, I would like to identify the parent rule and do something like this*/
          if ($parent_rule == "Module") {
              /* take some actions */
          } else {
              /* this means the Function is matched recursively from the Functions rule. */
          }
      }

【问题讨论】:

    标签: bison yacc


    【解决方案1】:

    LR(1) 解析器是自下而上的。当减少给定的生产时,“父规则”尚不清楚。

    无论如何,您的生产 Function 只会在 Functions 的上下文中减少,尽管有两种可能的生产可能适用。

    当然,确定哪个产品适用于产品本身是没有问题的。所以通常会做这样的事情:

    %type <FunctionList> Functions
    %type <FunctionDef>  Function
    ...
    
    %%
    
    ...
    
    Functions
        : Functions Function
          { $$ = $1;
            $$.append($2);
          }
        | Function
          { $$ = newFunctionList();
            $$.append($1);
          }
    
    Function
        : DEF ID ARGS BODY
          {
              $$ = newFunctionDef($2, $3, $4);
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多