[antlr-interest] Generating exceptions with location info

Ric Klaren klaren at cs.utwente.nl
Wed Apr 21 02:31:24 PDT 2004


On Tue, Apr 13, 2004 at 06:36:51PM -0400, FranklinChen at cmu.edu wrote:
> so I have a "begin" token and an "end" token 
> 
> BEGIN : '<'! ('a'..'z')+
> END: ('a'..'z')+ '>'!
> 
> and a parser that parses a "sentence" of tokens and pushes each
> "begin" tag found onto a stack of seen tags and then each "end" tag
> seen is checked for a matching one.  If there is a failure, I throw a
> SemanticException.  So far, so good.
> 
> Problem:  I don't know how to get the current line and column numbers
> from the parser, to pass to the SemanticException!  I looked at the
> ANTLR source and it looks like things are protected so that I can't
> get at the lexer information.  

The best way to get the line number is tag a token in the vicinity of the
error and use the line/column of that one. If everything else fails you can
get it from the inputState object (getInputState should return it but you
can probably reference it directly) Though the values there might be off
due to guessing mode.

Tip: try grep getLine on the antlr support lib header files (or java
files). All line handling methods are get/set/Line/Column.

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
   Words fly like arrows
      as if we knew what was right and wrong. --- Chuang Tsu


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list