[antlr-interest] Tricky vector constructor syntax

Paul Johnson gt54-antlr at cyconix.com
Tue Jul 19 01:50:44 PDT 2005


Richard Matthias wrote:
> Thanks Nigel and Paul for taking a look at my problem. Of course I should
> have included the original yacc grammar with my query for comparison so I've
> done so here. I don't really understand LALR parsers that well but if you do
> maybe you can gain some insight. I don't know if yacc produces any ambiguity
> warnings/errors with that grammar as I couldn't get the cygwin copy of bison
> I have installed to grok it.

At first sight, this grammar seems a bit (a lot?) wacky.  Bison runs 
with 122 warnings and 44 reduce/reduce conflicts. The warnings can be 
fixed with appropriate action code, which has presumably been removed 
from the grammar. The R/R conflicts arise from:

> typecast
> 	: '(' typename ')' lvalue				
> 	| '(' typename ')' constant				
> 	| '(' typename ')' unarypostfixexpression
> 
> unarypostfixexpression
>						
> 	| lvalue											
> 	| constant									
> 	<etc>

In other words, in 'a = (int)b', is 'b' an lvalue/constant or a 
unarypostfixexpression? Bison selects the typecast by default, but this 
is badly designed.

Bison fixes your priority problem by explicitly assigning a high 
priority to vector_initializer:

> vector_initializer
> 	: '<' expression ',' expression ',' expression '>'	%prec INITIALIZER
> 	| ZERO_VECTOR
> 	;

where INITIALIZER has the highest operator priority (see the 
%left/right/nonassoc list). I can't quite get my head around this, but I 
suspect that if you modify your test from

> vector v = <1,2, 5>6 > > <1,2,3>;

to either 'vector v = <1,2, 5<6 > > <1,2,3>;' or 'vector v = <1,2, 5>6 > 
< <1,2,3>;'

then you'll get a syntax error. I can't immediately see how the '<' 
relational works at all - does it?

Can't comment on your ANTLR fix... still trying to learn it myself...

:)



More information about the antlr-interest mailing list