[antlr-interest] Pattern to macth if no other match

Gavin Lambert antlr at mirality.co.nz
Wed Dec 24 11:11:59 PST 2008


At 03:35 25/12/2008, Mats Ekberg wrote:
>I want to have one action being executed if no patterns match, 
>like:
>
>stat:
>   'SWAP'  { fh.swap();}
>| 'DROP'  { fh.drop();}
>|       <default>{ fh.print(text);}

That's pretty much it, if you want to have a zero-length match 
(don't consume any of the input, but execute something if the next 
token isn't either SWAP or DROP):

stat
   : 'SWAP' { fh.swap(); }
   | 'DROP' { fh.drop(); }
   |        { fh.print(text); }
   ;

If you do want to consume the next token (and print the text of 
that), then you'd use this:

stat
   : 'SWAP' { fh.swap(); }
   | 'DROP' { fh.drop(); }
   | x=.    { fh.print($x.text); }
   ;



More information about the antlr-interest mailing list