ANTLR solution can match input using multiple alternatives

I have this simple grammar:

expr: factor; factor: atom (('*' ^ | '/'^) atom)*; atom: INT | ':' expr; INT: ('0'..'9')+ 

when I run it, it says:

The solution may correspond to the input, for example, "*", using several alternatives 1,2

The solution may correspond to input, for example, '/', using several alternatives 1,2

I can not determine the ambiguity. How do the red arrows indicate? Any help would be appreciated.

enter image description here

+8
java antlr antlr3
source share
1 answer

Suppose you want to parse input:

 :3*4*:5*6 

The parser generated by your grammar can match this entry in the following parsing trees:

enter image description here

and

enter image description here

(I skipped the colons to make the trees clearer)

Please note that you only see a warning. Specially instructing ANTLR that (('*' | '/') atom)* needs to be greedily matched as follows:

 factor : atom (options{greedy=true;}: ('*'^ | '/'^) atom)* ; 

the parser “knows” which alternative to take and no warning is issued.

EDIT

I tested the grammar with ANTLR 3.3 as follows:

 grammar T; options { output=AST; } parse : expr EOF! ; expr : factor ; factor : atom (options{greedy=true;}: ('*'^ | '/'^) atom)* ; atom : INT | ':'^ expr ; INT : ('0'..'9')+; 

And then from the command line:

  java -cp antlr-3.3.jar org.antlr.Tool Tg 

which does not cause any warnings (or errors).

+7
source share

All Articles