[antlr-interest] on ANTLR reported ambiguity

Maurizio de Pascale mdepascale at dii.unisi.it
Sat Dec 16 07:00:57 PST 2006


Hi,
I'm slowly learning to use ANTLR and I would like to better understand 
the ANTLR warning: "warning:nondeterminism between alts 1 and 2 of block 
upon".
For what I understand it reports that on a particular sequence of tokens 
in the input stream (e.g. IDENTIFIER IDENTIFIER ...) the rules are 
ambiguous since more than one can be guessed.

back to practice, for the small piece of grammar below:

block
    :    LEFT_CURLYBRACKET     
        (                                                       //THIS 
IS LINE 83
            variable_declaration
        |    execution_statement
        )*
        RIGHT_CURLYBRACKET
    ;

variable_declaration
    :    type_identifier name_identifier SEMICOLON
    ;

type_identifier
    :    IDENTIFIER
    ;

name_identifier
    :    IDENTIFIER
    ;

execution_statement
    :    null_execution_statement
    |    return_statement                                    //IF THIS 
LINE IS COMMENTED NO MORE WARNING
    ;

null_execution_statement
    :    name_identifier SEMICOLON
    ;

return_statement
    :    KEYWORD_RETURN name_identifier SEMICOLON
    ;

antlr does report this warning:
ANTLR Parser Generator   Version 2.7.7 (20060930)   1989-2005
mururoa.g:83: warning:nondeterminism between alts 1 and 2 of block upon
mururoa.g:83:     k==1:IDENTIFIER
mururoa.g:83:     k==2:IDENTIFIER

however if I comment out the "| return_statement" alternative in the 
"execution_statement" rule no more warnings are reported.
but the "return_statement" starts with a token different than 
IDENTIFIER, so I can't really see why it should give ambiguity for the 
reported type of input tokens since the only rule that start with two 
IDENTIFIER is variable_declaration.
What am I doing wrong?

Moreover I suggest that eventually the tool should spit out not only the 
input the causes ambiguity, but also the rules that are conflicting just 
like compilers do on ambiguous argument type deduction. Or is there 
already such an option that I'm missing?


thank you in advance for your help
Maurizio de Pascale
mdepascale at dii.unisi.it

P.S. please, save me answers like "there are already c++ grammars, use 
those".


More information about the antlr-interest mailing list