[antlr-interest] newbie problem with mismatched token in antr v3

John B. Brodie jbb at acm.org
Wed Oct 17 08:06:42 PDT 2007


Norbert Kiesel wrote:
>...snipped...
>Actually, I even get the same error with the following even further
>reduced grammar:
>
>itf	  	: 'interface' ' ' itfname EOF ;
>itfname	:  ITFNAME ;
>ITFNAME	: ( 'a'..'z' )+ ' ' ( '0' | '9' )+ ;

Please recall that Antlr lexers try to match the LONGEST possible
sequence of characters as a token - and do so in a greedy fashion.

So, for your test input of "interface xx 9", the lexer will first see the
'interface' keyword but it is followed by a blank, so the lexer
discards the possibility that "interface " is a keyword, rather the
lexer now commits to the notion that "interface " is a ITFNAME and
insists that there must be either a '0' or a '9' after it, throwing a
syntax error because it is not.

You might be able to play with the options{greedy=false;} stuff to
control the greedy nature of the lexer.  I tried a bit and had no
luck.

Or, as suggested by someone else, just ignore whitespace and have the
parser deal with ITFNAMEs as two tokens that must appear in proper
order.

Hope this helps...
   -jbb


More information about the antlr-interest mailing list