[antlr-interest] How to do preprocessing in antlr v4?

Bernard Kaiflin bkaiflin.ruby at gmail.com
Tue Nov 20 08:23:03 PST 2012


After reading this
http://www.antlr.org/pipermail/antlr-interest/2012-November/045768.html
I have rewritten the grammar to use fuzzy parsing. There is a
reportAmbiguity message I don't understand.

grammar Cmacros_e;

/* Process #define statements in a C file using fuzzy parsing.
*/

file
@init {System.out.println("Cmacros_e last update 1717");}
    :   .*? ( define .*? )+
    ;

define
    :   '#define' ID_LPAR ID ( ',' ID )* ')' replacement
            {System.out.print(">>> macro(parm) : " + $define.text);}
    |   '#define' ID_SPACE replacement
            {System.out.print(">>> simple macro : " + $define.text);}
    ;

replacement
    :   ~'\n'+ '\n'
    ;

ID_LPAR // no space between ID and ( for a macro definition with parameters
    :   ID '(' ;
ID_SPACE
    :   ID SPACE ;
ID  :   ( ID_FIRST (ID_FIRST | DIGIT)* ) ;

DIGIT    : [0-9] ;
fragment ID_FIRST : LETTER | '_' ;
fragment LETTER   : [a-zA-Z] ;
fragment SPACE    : ' ' ;

/* from
http://media.pragprog.com/titles/tpantlr2/code/reference/FuzzyJava.g4 */

CharacterLiteral
    :   '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
    ;

StringLiteral
    :  '"' ( EscapeSequence | ~('\\'|'"') )* '"'
    ;

fragment
EscapeSequence
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UnicodeEscape
    |   OctalEscape
    ;

fragment
OctalEscape
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UnicodeEscape
    :   '\\' 'u' HexDigit HexDigit HexDigit HexDigit
    ;

fragment
HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;

COMMENT
    :   '/*' .*? '*/'    -> channel(HIDDEN) // match anything between /*
and */
    ;

LINE_COMMENT
    : '//' ~[\r\n]* '\r'? '\n' -> channel(HIDDEN)
    ;

WS  :   [ \r\t\u000C\n]+ -> channel(HIDDEN)
    ;

OTHER : . -> channel(HIDDEN) ;

HTH
BK


More information about the antlr-interest mailing list