[antlr-interest] how do i throw my exception and get the rule text and line number

hardaway hardawaycg at gmail.com
Wed Apr 2 08:16:25 PDT 2008


in antlr 3,how do i throw my exception in an rule,raw grammar just like
below:

grammar test;
@header
{
 import java.util.HashMap;
}
@member
{
  HashMap reportMap= new HashMap();
}

prog:line+
line: reportStat | indexStat;

reportStat :  a=ID '=' '[' b=ID ']'  ( a=ID '=' '[' b=ID ']' )*
                  {
                     reportMap.put($a.text, $b.text);
                   };
indexStat  :  a=ID '(' b=INT ',' c=INT ')' '=' d=INT
                   {
                     if(reportMap.containsKey($a.text)
                      {
                          //System.out.println("reportID=" + $a.text + "has
not been defined");
                          //just here i want to throw my Exception maybe
like this
                           throws new MyException("report not defined",
ruleText, lineNumber);
                           //and more i want to record the line number and
the text,how can i ge the two values?.$a.line doesn't work.and is this the
only way to record the full text : $a.text + '(' $b.text + "," + $c.text +
")=" + $d.text; is there any simple way to get the full text with the
indexStat rule?
                       }
                    }
                   ;
and i have text like this:
a=[REPA],b=[REPB],c=[REPC]
a(1,1)=100
e(5,5)=2

when i throw exception to warning that "e(5,5)=2" e has not been defined ,i
also want to alert with the full text "e(5,5)=2" and also with line number
"3".how can i get these two value?

besides i can't define the rule like this
indexStat  throws MyException
               :  a=ID '(' INT ',' INT ')' '=' INT
then  when the parser file generated ,i need to modify the java file with
the indexStat method ,adds" throws MyException".what should i do with the
grammar file to make i easy?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080402/2c4e8ec4/attachment.html 


More information about the antlr-interest mailing list