BOB123
BOB123

Reputation: 176

Java cup, How to Inject a function after each reduce in the generated parser file?

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?

Upvotes: 0

Views: 195

Answers (1)

rici
rici

Reputation: 241761

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.

Upvotes: 0

Related Questions