[antlr-interest] Parsing preprocessor

Mike Lischke lists at lischke-online.de
Sun Apr 18 02:26:33 PDT 2004


Hi list,

I have just finished my first MSVC resource file parser version and now I want to add support for preprocessor commands. This is simple if I only have complete resource entries between #ifdef/#endif etc. I'm using:

  // ----- The main entry point -----
  resource_definition: 
    (resource_entry)*
  ;
  
  resource_entry: 
    NUMBER_SIGN! preprocessor_entry
    | resource_statement
  ;
  
  //----- Preprocessor directives -----
  preprocessor_entry:
    unconditional_preprocessor_entry
    | conditional_preprocessor_entry
  ;

  unconditional_preprocessor_entry:
    include_statement
    | pragma_preprocessor
    | define_preprocessor
    | "undef"^ IDENTIFIER
  ;
    
  conditional_preprocessor_entry:
    "if"^ expression conditional_preprocessor_part
    | "ifdef"^ resource_identifier conditional_preprocessor_part
    | "ifndef"^ resource_identifier conditional_preprocessor_part
  ;
  
  conditional_preprocessor_part:
    resource_definition (NUMBER_SIGN! else_preprocessor_entry)? NUMBER_SIGN! "endif"
  ;
  
  else_preprocessor_entry:
    "else"^ resource_definition
    | "elif"^ expression conditional_preprocessor_part
  ;
  
This works very well. Unfortunately, #ifdef and the like can also appear everywhere in the source where also white spaces are allowed (just like in C, for example), so I wonder how I can add support for that. Using two parser, one for preprocessor stuff and one for the real stuff, would require a lot of extra work because the first had to feed the second where the AST from the first would need to be transformed to source code for the second parser. A tree parser does not help here as it still would leave the problem open that preprocessor tokens can appear everywhere.

Any hint to solve that problem would be much appreciated.

Mike
--
www.soft-gems.net



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list