[antlr-interest] confused about the trained monkey

Don Reba don_reba at inbox.ru
Thu Jun 5 08:44:40 PDT 2008


antlr-interest at antlr.org

Hello,

I would really appreciate it if someone explained ANTLR's behavior on the following simple example.

I want the grammar to match the string "aa". Disambiguation betwen A and B requires infinite lookahead, but neither token is recursive, so I expect ANTLR to use that helpful trained monkey of its to do it.

In reality, the grammar does not match any string. Peeking in the generated code, I see that A is actually matched to "A EOF", so the first A in the pair can never be seen.

I presume that this has to do with B putting ~EOF in the FOLLOW of A. What confuses me is why ANTLR uses FOLLOW in this case instead of a cyclic DFA.

test
	: A A
	;
A
	: 'a'
	;
B
	: ~'b'+ 'b'
	;


More information about the antlr-interest mailing list