[antlr-interest] 'match not' not working

Tom tbee at tbee.org
Tue Aug 4 05:52:40 PDT 2009


I have a pretty simply grammar to construct; it must find balanced 
tokens in a stream. For example:
    aaa %{ bbb }% ccc

These may be nested:
    aaa %{ aaa %{ bbb }% ccc }% ccc

or not present at all:
    aaa

or consequtive:
    aaa %{ bbb }% ccc %{ bbb }% ccc

So all a need to do is find the tokens with non-tokens in between. This 
is the grammar I expected that would do that:

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/
      
parse: loop;

loop: noloop LOOPSTART loop LOOPEND loop
    | noloop
    ;
   
noloop: (~( LOOPSTART | LOOPEND ))*
      ;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

LOOPSTART: '%{';
LOOPEND: '}%';



However, the "match not" is not working.

"X" results in
    line 1:0 no viable alternative at character 'X'

"%{X}%" results in
    line 1:2 no viable alternative at character 'X'


If I replace the match-not part with a dummy 'X'*, then the grammar 
works (or atleast the mentioned tests run through). There is very 
limited documentation on the "match not", does anyone have a suggestion 
for me?

Tom


NB: if this works I will introduce a third token; an %{ with an id in 
between, written like: %id$


More information about the antlr-interest mailing list