Externalize java code ( Custom object return from walker rules ) from Walker files in ANTLR 3

63 Views Asked by At

I am using tree grammar to walk the AST generated after parsing the input in ANTLR 3. Inside the walker rules, there is java code embedded within to populate and return custom java objects out of the AST which are used for manipulating the output.

I am facing a challenge of externalizing java code from grammar files.

Consider following rule :

Walker.g

    val returns [Node node]
    @init  { Node val= new Node(); }
    @after { node = val; }
    :
     NUMERIC 
      {
       node.setValue($NUMERIC.text); // Node initialisation and population 
                                     //   inside .g file
      }

;

And this is a sample node class which I am using for storing and manipulating the information of AST

Node.java

    class Node{
      String value;

      public String getValue(){
         return this.value;
      }

      public void setValue(String text){
         this.value = text;
      }

    }

Is there any way that the node creation and population could be done outside the walker.g file?

0

There are 0 best solutions below