[antlr-interest] tree rewrite ambiguous enclosing rule/referenced in production

Gavin Lambert antlr at mirality.co.nz
Thu Aug 13 05:34:24 PDT 2009


At 13:45 13/08/2009, Tim Williams wrote:
 >For the scopedClause rule below, I'm getting this error(132):
 >"$scopedClause is ambiguous; rule scopedClause is enclosing rule 

 >and referenced in the production (assuming enclosing rule)"
[...]
 >scopedClause:	
 >	LPAREN (scopedClause->scopedClause) ((OR_TOKEN s2=scopedClause) 

 >->
 >^(OR $scopedClause $s2))+ RPAREN
 >    | LPAREN (scopedClause->scopedClause) ((AND_TOKEN
 >s2=scopedClause)
 >-> ^(AND $scopedClause $s2))+ RPAREN
 >    | LPAREN (scopedClause->scopedClause) ((NOT_TOKEN
 >s2=scopedClause)
 >-> ^(NOT $scopedClause $s2))+ RPAREN
 >	| searchClause
 >		-> searchClause
 >	;

I think the warning will go away if you explicitly label the first 
path -- ie. replace all of your (scopedClause->scopedClause) 
fragments with (s1=scopedClause->$s1).  (Using $scopedClause in 
the second rewrite is correct though, and should be left like 
that.)

You could, however, improve performance by first left factoring 
it:

scopedClause
   :  LPAREN (s1=scopedClause -> $s1)
      (  OR_TOKEN s2=scopedClause -> ^(OR $scopedClause $s2)
      |  AND_TOKEN s2=scopedClause -> ^(AND $scopedClause $s2)
      |  NOT_TOKEN s2=scopedClause -> ^(NOT $scopedClause $s2)
      )+ RPAREN
   |  searchClause -> searchClause
   ;

(Although that begs the question: is NOT really a binary operator 
in your language?  And is 'LPAREN searchClause RPAREN' supposed to 
be illegal?  Because as it stands, both are the case.)



More information about the antlr-interest mailing list