help with an ambiguous grammar

2 views
Skip to first unread message

idad...@gmail.com

unread,
Apr 17, 2006, 3:09:27 AM4/17/06
to pyggy
Hello again,

I'm trying to write a grammar that allows for space separated lists.
Here's my grammar:

%right NEG;
%left MINUS;
%nonassoc SHORT;
%nonassoc LONG;

expression ->
%prec(MINUS) expression SUB expression
| %prec(NEG) SUB expression
| CONSTANT
| %prec(LONG) LPAREN expression (expression)* RPAREN
| %prec(SHORT) LPAREN expression RPAREN
;

What I want is for something like this: "[1 - 2]" to be parsed to the
python form of [(1-2)], not [1, -2]. However, I can't seem to get this
to be unambiguous. For a more complicated example: this: "[1 2 3 - 4 5
- - 6]" should be parsed as this: [1,2,(3-4),(5 - (-6))]. Any idea how
I can get this grammar to work? Thanks again,

-e

Tim Newsham

unread,
Apr 17, 2006, 4:46:17 PM4/17/06
to idad...@gmail.com, pyggy

I think the best solution here is to require parenthesis around unary
minus or to use a seperator other than space (ie. comma).

If you still want to implement your parsing scheme, you can have it
parse all ambiguous alternatives and then filter out the ones you
don't want in post-processing.

> -e

Tim Newsham
http://www.lava.net/~newsham/

Reply all
Reply to author
Forward
0 new messages