3

I want to change the definition of getTree() methode of generated return class like statement_return (see below example). See below Sample parser grammar and respecting generated code which includes statement() and statement_return().

parser grammar

options {
    output          = AST;
}

statement
    :   rule*  
       -> ^(STATEMENT rule* )
   ;

Generated Tree Parser

    public static class statement_return extends ParserRuleReturnScope {
        Object tree;

        public Object getTree() {
            return tree;
        }
    };


    public final Grammar.statement_return statement() throws RecognitionException {
           ...
           ...
    }

I want to change the definition of getTree() methode of generated class like statement_return where PLSQLTree (extends CommonTree) is custom object. I have custom treeAdoptor which created PLSQLTree type object I want to capture start and end token for each parser rule which is required in TREE PARSER. I want the same for all the rules in my grammar.

My Overrided definition

    public static class statement_return extends ParserRuleReturnScope {
        Object tree;

        public Object getTree() {
            /*
             start - Add some code in generated class 
            */
              PLSQLTree plsqlTree = (PLSQLTree) tree;

              Token start = this.start;
              plsqlTree.params.put(ParserConstants.START_TOKEN, start);
              Token stop = this.stop;
              plsqlTree.params.put(ParserConstants.STOP_TOKEN, stop);

            /*
             end - Add some code in generated class 
            */
            return plsqlTree;
        }
    };

Please help me how this can be achieved?

Sanjiv
  • 1,795
  • 1
  • 29
  • 45
  • I'm afraid you'd have to modify the StringTemplate used to generate the Java code to do this. Maybe it would be a good idea to elaborate on why you need such change - perhaps there's another way to accomplish your goal in another way. – Jiri Tousek Jan 19 '16 at 13:05
  • @JiriTousek : I have sample code to overridden method. Also added description for the same. Let me know if you need other details. – Sanjiv Jan 19 '16 at 13:18
  • @JiriTousek : it would be helpfull if you can guide to modify the StringTemplate used to generate the Java code to do this. – Sanjiv Jan 19 '16 at 13:22
  • 1
    The only way I know about to do this would be to basically create a new "target language" that would copy almost everything from the existing Java templates and only modify the relevant part responsible for tree return. See [here](https://theantlrguy.atlassian.net/wiki/display/ANTLR3/How+to+build+an+ANTLR+code+generation+target) for a guide. I don't personally like this solution, but I can't think of any better way to actually modify this part of parser generation. My approach would probably be to add start and end tokens using a postprocessing. – Jiri Tousek Jan 19 '16 at 16:54
  • Thanks @jiri ..will try creating new language . I would also prefer to add start and end tokens using postprocessing ..possibably through aspect oriented programming (AOP) – Sanjiv Jan 20 '16 at 02:32

0 Answers0