[antlr-interest] NoViableAltException

Ric Klaren klaren at cs.utwente.nl
Mon Nov 1 02:29:53 PST 2004


On Wed, Oct 27, 2004 at 11:55:01AM -0000, meenachan wrote:
> I am getting a NoViableAltException in the ANTLR . But I want to get 
> the full string from the place where it occurs . For example:
> 
> The input is :
> create antlr antlrname=antlr1 antlrype=type1 
> 
> where the lexer definition is as follows:
> CREATE:"create ";
> ANTLR:"antlr";
> ANTLRNAME:"antlrn"('a' ('m' ('e')?)?)?;
> ANTLRTYPE:"antlrt"('y' ('p'('e')?)?)?;
> 
> In the above example since  it was not able to identify the 
> token "antlrype=type1" it throws NoViableAltException with the LT(1) 
> as "antlr". But I need to display the whole string "antlrype=type1 " 
> as invalid.
> 
> My question is : How to get the string (antlrype=type1 )from the 
> place where the NoViableAltException occurs??

You'd probably have to make a custom error handler(s) for the rule(s) that
need this. You can access the text that got parsed so far with the
getText() method of the lexer object. You might have to use the
consume(Until) method to skip forward to a sane point in the input, to get
the complete text (not 100% sure if you need to call commit afterwards
think not..). The $FOLLOW / $FIRST / $FOLLOW(rulename) / $FIRST(rulename)
shorthands give the bitset names (as can be used in consumeUntil etc.) for
the follow/first sets of the current/named rule.

Hope this helps,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
  "Good judgement comes from experience.
     Experience comes from bad judgement." --- Unknown



 
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