[antlr-interest] Tricky vector constructor syntax
Nigel Sheridan-Smith
nbsherid at secsme.org.au
Sun Jul 17 16:40:32 PDT 2005
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Richard Matthias
> Sent: Monday, 18 July 2005 7:10 AM
> To: 'ANTLR Interest'
> Subject: [antlr-interest] Tricky vector constructor syntax
>
> Hi all,
>
> I'm trying to write a parser for a scripting language used in an online
> game.
> The language is predictably very C-like with a few twists, most of which
> are
> quite benign, but one is really twisting my melon.
>
> The language supports vectors and quaternions as data types and they have
> provided syntax for constructing instances of these types that make it
> just a
> little bit tricky to parse. For example:-
>
> vector v = <4.0 , 1.2, 0>;
>
> Of course that's just an initializer, the same angle bracket syntax can be
> used in expressions and each of the three elements of the vector can of
> course be expressions also. A quaternion is for the sake of parsing just
> the
> same except it has four components.
>
> The grammar (which I've attached) has an expression section shamelessly
> lifted from the java.g sample grammar but altered slightly to match the
> original yacc grammar supplied by the makers of the game. For the moment
> I've
> placed a rule for just the vector at the same level as the other constants
> (the last alt of the postfixExpression rule) and even with a syntactic
> predicate it still causes the same ambiguity warning. Does this look right
> or
> should I try and shoehorn it in at the same level as the < operator?
>
> I commented out some other stuff so at least without the production in
> question the grammar compiles without warnings to give a fighting chance!
> And
> the lexer isn't complete yet so it won't produce the right tokens. Oh and
> please don't ridicule my choice of token names too much :)
>
Okay this is a strange one, so I haven't fully worked it out. However, it
definitely is between your relationalExpression and postfixExpression rules.
relationalExpression
: shiftExpression ( (LEFT_ANGLE^ | LESSTHAN_EQUALS^ |
GREATER_EQUALS^ | RIGHT_ANGLE^) shiftExpression )*
;
postfixExpression
: lvalue ( INCREMENT | DECREMENT )?
| list_initializer
| IDENT LEFT_PAREN expressionlist RIGHT_PAREN
| constant
| LEFT_ANGLE expression COMMA expression COMMA expression
RIGHT_ANGLE
;
The reason you are getting the warning, is because in postfixExpression,
"expression" is followed by RIGHT_ANGLE, meaning that in some cases the
recursion will confuse the parser. If you change "expression" to something
like INTEGER_CONSTANT the warning will disappear. The problem is only with
RIGHT_ANGLE, not LEFT_ANGLE.
What this means is that the following bits of code are ambiguous:
x = < 1, 1, 1 > 5 >;
Not sure how to solve this one, as I have trouble getting syntactic
predicates in the optional alternatives right. Maybe someone else can
suggest an appropriate change?
One of your LEFT_SQUARE tokens is also spelt wrong (I think line 139). This
will lead to no error message in ANTLR but some confusion down the track.
Nigel
--
Nigel Sheridan-Smith
PhD research student
Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435
More information about the antlr-interest
mailing list