[antlr-interest] implementing C-style #define macros in ANTLR
Andy Tripp
antlr at jazillian.com
Wed Mar 25 07:37:40 PDT 2009
Bruce Bauman wrote:
> I am trying to convert 4 million lines of Pascal code from the MetaWare Pascal compiler to Free Pascal. (yes, really). Unfortunately, MetaWare Pascal supports many non-standard extensions to Pascal, including C-style macros. Our existing Pascal code base uses the macro feature extensively..
>
> Any suggestions on how to implement this? Any examples in existing ANTL grammars?
>
> Thanks.
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
Maybe you could just run the C preprocessor on the pascal file.
IIRC, cpp works on any code, not just C/C++.
Or, you could add the rules below in your grammar to recognize the preprocessor directives.
The problems here are:
a) There's no one "correct" place in your grammar to put the "ppDirective" rule -
cpp directives can go *anywhere* in your code.
b) Your parser may not be able to parse certain code, even with these rules added.
Andy
Parser rule:
ppDirective
: PPspace
| PPdefine
| PPif
| PPelse
| PPelif
| PPendif
| PPinclude
| PPifdef
| PPifndef
| PPundef
| PPpragma
| PPerror
| PPline
;
Lexer rules:
// atripp: added support for preprocessor
protected RestOfPP :
(
'\\'! Newline! // atripp: note the "!"s to delete escaped newlines
| Comment
| CharLiteral // 6/11/04: avoid matching '"' as StringLiteral
| StringLiteral // atripp:without this, "//" inside string gets processed
| {LA(2) != '*' && LA(2) != '/'}? '/'
| '#' // "stringification"
| ~('\r' | '\n' | '/')
)+
;
// PPSpaceRule will convert "# define" to "#define", etc.
PPspace : "#" (Whitespace)+ RestOfPP
;
PPdefine : "#define" (Whitespace)+ RestOfPP
;
PPif : "#if" (Whitespace)+ RestOfPP
;
PPelse : "#else"
;
PPelif : "#elif" (Whitespace)+ RestOfPP
;
PPendif : "#endif"
;
// Note the "*" instead of "+" here, to handle "#include<whatever.h>"
PPinclude : "#include" (Whitespace)* RestOfPP
;
PPifdef : "#ifdef" (Whitespace)+ RestOfPP
;
PPifndef : "#ifndef" (Whitespace)+ RestOfPP
;
PPundef : "#undef" (Whitespace)+ RestOfPP
;
PPpragma : "#pragma" RestOfPP
;
PPerror : "#error" (RestOfPP)?
;
PPline : "#line" RestOfPP
;
More information about the antlr-interest
mailing list