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

hardaway hardawaycg at gmail.com
Thu Apr 3 01:12:02 PDT 2008


i 'm new to antlr and maybe my questions are very stupid.
  just like my question,
1: if i have text like those in red
 *a = [REPA], b=[REPB], c=[REPC]*
 *a(1,1)=100*
 b(1,4)=19
 e(1,1)=100
when i'm parsing the text,i'll put the first line to a map ,just like
map.put(a,REPA),map.put(b,REPB)....and when parsing line2-4,just like
a(1,1).i'll scan the map whether the variable has been defined,just like
map.containsKey(a),if it is ,i'll do some operations.and just like the above
text,when it turn to line 4: "map.containsKey(e)".obviously it'll return a
false to me.and i want to throw my custom Excpetion to warning the text
writer to do some correct.and i want to throw some custom messages with the
full text of  "e(1,1)=100" and the line number: 4,maybe message like this
"REPORT HAS NOT BEEN DEFINED WITH THE TEXT:  e(1,1)=100  in line 4.how can i
get the full text and the line number.
indexStat : a=ID '(' b=INT ',' c=INT ')'' '=' d=INT.
i only know that writing like this: $a.text + '(" + $b.text + ',' + $c.text
+ ')=' + $d.text to combine e '(' 1 ',' 1 ')=' 100 .but is it too
complicate?is there any way to get e(1,1)=100 as easy as possible?

and in your replys ,firstly a big thank to ur replays.u mentioned that use
the FailedPredicateException.but i'm afraid map.containsKey(e) returns only
true or false, it wont throw any exception.how can i catch it??

and the last question.i googled some pages that in an older version of
antlr,wen can write rule like this to throws Exception:
indexStat returns[String value] throws MyException   but in antlr 3,does the
format change? when i write like that,i got an tip and "indexStat  is not a
rule"...
and if in an action,i write ... throw new MyException()..i have to modify
the generated parser java file with the sentence like
public String indexStat() throws RecoginitionException
to
 public String indexStat() throws RecoginitionException, MyException..
to make the java code compileable.what is the right way to handle this?

thanks to ur help again

hardaway
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080403/6fa65963/attachment.html 


More information about the antlr-interest mailing list