[antlr-interest] Match the start and end of a line

Gavin Lambert antlr at mirality.co.nz
Thu Dec 25 15:43:15 PST 2008


At 09:58 26/12/2008, Gokulakannan Somasundaram wrote:
>Suppose i need #include to always to be in the beginning of line
>
>INCLUDE : '#include';
>
>stmtInclude :  INCLUDE { if ($INCLUDE.pos!=0)  <<error>> };
>
>Is my idea correct?  Or does it have any disadvantage?

The way I would prefer to go is similar to this, but performs the 
check at the lexer level:

INCLUDE
   : { $pos == 0 }? => '#include';

or alternatively:

fragment INCLUDE: '#include';
HASH
   : '#'
     ( {$pos == 1}? =>
       ( ('include') => 'include' { $type = INCLUDE; }
       )
     )?
   ;

This way an INCLUDE token is only generated if the # is at the 
correct column.  (I'm not entirely certain about the syntax of 
$pos, though.  You might need to play with that a bit.)



More information about the antlr-interest mailing list