[antlr-interest] NullPointer exception and inline template

Gabriel Miro gsmiro at gmail.com
Thu Sep 22 12:46:59 PDT 2011


Folks,

I'm trying to write a rule to rewrite '<>' to '!=' in some expression
strings, but every time I declare an inline template it gives me a
NullPointerException and I'm unable to compile the parser code. Any help is
appreciated.

Here's the grammar:

grammar test;
options {
    output=template;
    rewrite=true;
}
@header{
    package com.adp.acs.rules;
}

rule    :    expr+;
expr    :    (BOOLEAN_OP)? LPAREN WS? (calc|comp|in) WS? RPAREN WS?;
calc    :    lside WS? ARITH_OP WS? FIELD;
comp    :    lside WS? COMP_OP WS? STRING;
in    :    FIELD 'IN' WS? LPAREN (CHAR | CHAR ',') RPAREN;
lside    :    FIELD | sum;
sum    :    LPAREN WS? FIELD WS? ARITH_OP WS? FIELD RPAREN;
RPAREN    :    ')';
LPAREN    :    '(';
ARITH_OP:    '+' | '-' | '*';
COMP_OP    :     '<>' -> template() "!="
        | '<='
        | '<'
        | '>='
        | '>'
        ;
BOOLEAN_OP
    :     'AND' | 'OR';

FIELD    options{greedy=false;}:    'A'..'Z'+;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

CHAR
    :  '\'' ( ESC_SEQ | ~('\\'|'\'') ) '\''
    ;

STRING
    :  '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
    ;

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

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

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

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;


More information about the antlr-interest mailing list