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

John B. Brodie jbb at acm.org
Mon Jul 12 19:32:36 PDT 2010


On Mon, 2010-07-12 at 22:11 -0400, Reynold Xin wrote:
> 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

when i comment out your options{} block i get the very same error!

so this is NOT a java vs python issue (i believe), see below

> 
> 
> 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*
>     ;

value_braced starts with a '{' 

and ~'}' also matches a '{'

which alternative are we expected to follow? is ambiguous

> 
> 
> /*------------------------------------------------------------------
>  * 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
>     :    '!' | '>' | '<' | '*' | '&' | '.' | ';'
>     ;
> 

Hope this helps...
   -jbb




More information about the antlr-interest mailing list