I have the following grammar:
rule: 'aaa' | 'a' 'a';
It can successfully parse the string "aaa", but cannot parse "aa" with the following error:
line 1:2 mismatched character '<EOF>' expecting 'a'
FYI, this is a lexer problem, not a parser, because I don't even call the parser. The main function looks like this:
@members { public static void main(String[] args) throws Exception { RecipeLexer lexer = new RecipeLexer(new ANTLRInputStream(System.in)); for (Token t = lexer.nextToken(); t.getType() != EOF; t = lexer.nextToken()) System.out.println(t.getType()); } }
The result is the same with the more obvious version:
rule: AAA | AA; AAA: 'aaa'; A: 'a';
Obviously, lexer ANTLR is trying to map the input "aa" to the AAA rule, which fails. In addition, ANTLR is an LL (*) parser or something else, the lexer should work separately from the analyzer and should be able to resolve ambiguity. Grammar works fine with good old lex (or flex), but it doesn't look like ANTLR. So what's the problem?
Thanks for the help!
source share