[antlr-interest] Getting a list of possible tokens at a Parser NoViableAltException in ANTLR v3.1

Floor Goddijn Floor.Goddijn at aimms.com
Mon Jun 29 06:14:35 PDT 2009


Hi, 

 

I was wondering whether it is possible to get a list of expected
(possible) tokens at a NoViableAltException. 

Consider this small example grammar: 

 

grammar NoViableAltGrammar;

 

statement 

                :    statementA

                |   statementB

                ; 

                

statementA 

                :   ID Min ID Semi

                ;

                

statementB

                :   ID Plus ID Semi

                ;               

 

fragment

CHAR :  'a'..'z' ;

 

ID : CHAR+;

Semi :    ';'    ;

Plus:    '+'    ; 

Min :    '-'    ;          

Assign:    '='    ;  

 

WS:        (' '|'\t'|'\n'|'\r')+ {$channel=HIDDEN;}    ;

 

 

Starting with "statement"  and  this input:

 monkey = banana

 

I get a NoViableAlt  at token '=' . 

In a user error message I would like to tell that  "a  '-' or '+'  was
expected" .

The information is available in the generated parser code of the rule
statement, but as it seems, I cannot access this information through the
noViableAltException.

 

Best regards, 

 

Floor

 

 

 


CONFIDENTIALITY: This e-mail and any attachments are confidential and may be privileged. If you are not a named recipient, please notify the sender immediately and do not disclose the contents to another person, use it for any purpose or store or copy the information in any medium.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090629/0651d1cb/attachment.html 


More information about the antlr-interest mailing list