[antlr-interest] How to add code to the Parser class?

Jeff Vincent JVincent at Novell.Com
Tue Apr 20 14:22:13 PDT 2004


Here is a quick (and probably not 100% accurate) example.  Remember
also, you can pass data as arguments and return values to and from rules
(at least you can in the AST parser rules).
 
class myParser extends Parser;
 { 
    //User defined class members, including any helper methods you may
want to include.
    String myString = null;
 }

 start : 
    { myString = "start";
       ...
    }
    rule1
    ;
 
  rule1 : 
    { boolean b;
        //do something w/ myString
        if (myString.equals("start"))
           System.out.println("myString = " + myString);
     }
    b = rule2[myString]
    ;
 
  rule2 [ String s] returns [ boolean bool ] :
   {   if (s.length() == 4))
         bool = true;
        else 
          bool = false;
   }
    ...
     ;
}



>>> jpdv at medialog.pt 4/20/2004 4:00:14 AM >>>

To have more control over the parsing I added some Semantic 
Predicates to my Gramar.

These predicates rely on a Variable. The Variable has to known to 
all the rules.

How can I set this Variable as a member of the Parser class of my 
Gramar? I don't seem to find out how, except opening the generated 
Parser file and adding the Variable as a Member.

This way, everytime I change the Gramar and the Parser class is 
reconstruted, I need to edit it and insert a line of code declaring 
the Variable.

Is there any other way to do this?

Thanks a lot.




Yahoo! Groups Links






-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20040420/0ed86032/attachment.html


More information about the antlr-interest mailing list