0

I'm using .cup file to generate parser based on specific derivation rules.

After every reduce, I want to call a function, Is it possible to define it anywhere in the .cup file, so that it will be injected into the generated code after each reduce?

BOB123
  • 176
  • 10

1 Answers1

0

Apparently so, if you are using the version of Java CUP maintained by Jochen Hoenecke on github. Quoting the manual (from that version, end of section 2.2):

after reduce {: ... :};

Defines code that is executed whenever a production rule is reduced. The arrays symbols contains all terminal and non-terminal symbols of the current production rule. RESULT can be used to access and modify the value of the nonterminal created by the production rule. Example usage:


after reduce {:
   int lineNumber = symbols[0].left + 1;
   if (RESULT instanceof AST_Node) {
      ((AST_Node) RESULT).lineNumber = lineNumber
   }
 :}

According to the change log, the feature was added in 2019.

rici
  • 234,347
  • 28
  • 237
  • 341