[antlr-interest] Python target for nested braces handling (multiple alternatives)

Reynold Xin rxin at berkeley.edu
Mon Jul 12 19:11:26 PDT 2010


I have the following grammar file. When I was running it in Java using ANTLR
works, it compiles without any problem. However, when I was using the Python
target:

# java -cp antlr-3.1.2.jar org.antlr.Tool bibtex.g

It prompts the error:
warning(200): bibtex.g:42:35: Decision can match input such as "'{'" using
multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input


The grammar file is (I've labeled line 42 below). What's happening?


grammar bibtex;

options {
    language=Python;
}


/*------------------------------------------------------------------
 * Parser rules
 *------------------------------------------------------------------*/

bibtex_file
    :    bibtex_object*
    ;

bibtex_object
    :    '@' ID '{' (ID | LITERAL)? assignment_list '}'
    |   '@' ID '(' (ID | LITERAL)? assignment_list ')'
    ;

assignment_list
    :    assignment (',' assignment)*
    ;

assignment
    :    ID '=' value
    ;

value
    :    simple_value ('#' simple_value)*
    ;

simple_value
    :    ID
    |    LITERAL
    |   STRING
    |   value_braced
    ;

// matches {...} while allowing nested braces.
value_braced
    :   '{' ( value_braced | ~'}' )* '}'              *<--------------- this
is line 42*
    ;


/*------------------------------------------------------------------
 * Lexer rules
 *------------------------------------------------------------------*/

ID  :    ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

LITERAL
    :    ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-'|'/')+
    ;

COMMENT
    :   '%%' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    ;

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

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

CHAR:  '\'' ( 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
    ;

DO_NOT_DELETE_THIS_RULE
    :    '!' | '>' | '<' | '*' | '&' | '.' | ';'
    ;

--
Reynold Xin


More information about the antlr-interest mailing list