[antlr-interest] C and #define

Gavin Lambert antlr at mirality.co.nz
Mon May 21 05:05:46 PDT 2007


At 21:08 21/05/2007, Paul Keir wrote:
 >I'm building on the C grammar from the examples, and would like
 >to modify the lexer rule which ignores lines starting with #;
 >I'd like not to ignore #define, but ignore the rest. Currently
 >the rule is:
 >
 >// ignore #line info for now
 >LINE_COMMAND
 >    : '#' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
 >    ;

My air-codegen suggests something along these lines:

PREPROCESSOR
   :  '#' (' ' | '\t')*
   (  'define' ~('\r' | '\n')* { $type = DEFINE; }
   |  'line' ~('\r' | '\n')* { $channel = HIDDEN; }
   )  '\r'? '\n'
   ;

This is by no means exhaustive, since it doesn't handle line 
folding or other directives.  But hopefully that'll get you 
started :)



More information about the antlr-interest mailing list