Yacc/Bison 父规则

Yacc / Bison parent rule

在 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. */
          }
      }

LR(1) 解析器是自下而上的。 "parent rule" 尚不清楚何时减少给定的产量。

无论如何,您的产量 Function 只会在 Functions 的情况下减少,尽管有两种可能的产量可能适用。

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

%type <FunctionList> Functions
%type <FunctionDef>  Function
...

%%

...

Functions
    : Functions Function
      { $$ = ;
        $$.append();
      }
    | Function
      { $$ = newFunctionList();
        $$.append();
      }

Function
    : DEF ID ARGS BODY
      {
          $$ = newFunctionDef(, , );
      }