[antlr-interest] Problem with ambiguous grammar

Joseph Areeda newsreply at areeda.com
Mon Aug 17 02:28:22 PDT 2009


Not an expert here but I'll hazard a guess.

It seems to me that a statement like:
COMPUTE a EQUAL b
can be either an imperative_compute_statement or a conditional compute
statement.

What if you take the ? off the conditional's ON clause?  Is it really a
conditional if that's not there?

Joe
Thomas Woelfle wrote:
> Hi,
>
> I have a problem with following grammar:
>
> imperative_statement
>   : imperative_compute_statement
>   ;
>  
> imperative_statement_list
>   : imperative_statement+
>   ;
>
> imperative_compute_statement
>   : COMPUTE^ Identifier+ EQUAL Identifier END_COMPUTE?
>   ;
>  
> conditional_compute_statement
>   : COMPUTE^ Identifier+ EQUAL Identifier
>       (ON? SIZE ERROR imperative_statement_list)?
>       END_COMPUTE?
>   ;
>
> COMPUTE: 'COMPUTE';
> NOT : 'NOT';
> ON : 'ON';
> SIZE : 'SIZE';
> EQUAL : 'EQUAL';
> ERROR : 'ERROR';
> END_COMPUTE : 'END-COMPUTE';
>
> Identifier :
>   Letter (Letter | Digit)*;
>  
> fragment
> Letter :
>   'a'..'z' |
>   'A'..'Z';
>  
> fragment
> Digit :
>   '0'..'9';
>
> ANTLR tells me that the rule 'imperative_compute_statement' has more 
> than one alternative to match the optional 'END_COMPUTE'. Can anybody 
> give me a hint on how to rewrite this grammar so that the warning no 
> longer appears?
>
> Any help appreciated.
>
> Regards,
> Thomas
>
>   



More information about the antlr-interest mailing list