[antlr-interest] how do i generate an error if input is not fully consumed

Brian Smith brian-l-smith at uiowa.edu
Wed Mar 19 12:17:38 PST 2003


I have been tripped up by this before. From memory, I believe that you 
just need to make sure your start rules explicitly end with EOF. You 
probably have a start rule that looks like this:

myRule: A B C;

Change it to:

myRule: A B C EOF;

Then, myRule will only be matched if nothing follows it.

- Brian



fedot wrote:
> I have the following java code (approximately) to go with some 
> simple calculator grammar:
> 
> MyLexer lexer = new MyLexer(new StringReader(string));
> MyParser parser = new MyParser(lexer);
> parser.start(); // start variable
> System.out.println(parser.getAST().toStringList());
> 
> If input string is such that only beginning of it matches lexer and 
> parser rules, an AST is properly constructed for the beginning of 
> the string and the remainder is silently ignored.
> 
> I need to detect such a state (syntactically invalid input remaining 
> unconsumed) and "raise an error flag". What are my options?
> 
> Thank you in advance!
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 



 

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



More information about the antlr-interest mailing list