[antlr-interest] Using ANTLR's ~ to do match literals causes "no viable alternative" messages in generated Java but not in ANTLRWorks

Davy Landman davy.landman at gmail.com
Thu Feb 25 16:00:19 PST 2010


Hello,

I'm having some problems with my generated java parser/lexer, but I've
tried to reduce the problem to a small subset.

Let's assume the simple language which has identifiers and numbers.
And the only rules are, a number contains out of only numbers, and a
identifier can not contain `'?', ':', ' '` and can not begin with a
number.

I would create the following ANTLR specification of this language.

    grammar simple;

    prog:	expr* EOF;

    expr
    	:	ID | INT;


    ID  : ~('0'..'9'|SEPERATORS) ~(SEPERATORS)*;

    fragment
    SEPERATORS
    	:	' ' | ':' | '?';

    INT :	'0'..'9'+
        ;

And running this in ANTLRWorks debugging and parsing mode, no errors
are reported to the console.

But if I than generate the Parser and Lexer and try to consume it in a
java program as such:

    import org.antlr.runtime.ANTLRStringStream;
    import org.antlr.runtime.CommonTokenStream;
    import org.antlr.runtime.RecognitionException;


    public class runner {

    	public static void main(String args[])
    	{
    		simpleLexer lex = new simpleLexer(new ANTLRStringStream("a33 44"));
    		CommonTokenStream tokens= new CommonTokenStream(lex);
    		simpleParser parser = new simpleParser(tokens);
    		try {
    			parser.prog();
    		} catch (RecognitionException e) {
    			e.printStackTrace();
    		}
    	}
    }

The parser works, and the prog() continues succesfully. But in the
background the following error message is printed to the console.

    line 1:3 no viable alternative at character ' '

The same exact sequence causes no messages to the ANTLRWorks console,
so I was wondering, am I abusing the ~ ? Or is there a difference
between the ANTLRWorks debugger and default java runtime?

So if anybody can help me how to get rid of these messages? Because in
the original lexer and parser where this question is based, I get a
nice looking AST but only have messages printed to the eclipse
console.

Versions used:
ANTLRWorks 1.3.1
ANTLR-3.2
Linux x64

Kind regards,
Davy Landman


More information about the antlr-interest mailing list