Incorrect parsing of exponentiation operation

9 views
Skip to first unread message

Alex_Gaynor

unread,
Nov 23, 2009, 8:26:35 PM11/23/09
to ply-hack
I've got a grammar with precedence:

precedence = (
("nonassoc", "COMPARISON"),
("left", "AMPER", "VBAR", "CIRCUMFLEX"),
("left", "PLUS", "MINUS"),
("left", "STAR", "SLASH", "PERCENT"),
("right", "UNARY"),
("right", "POWER"),
)

Which I believe is correct. However given: a ** b ** c; it parses it
as:
[('BinOpNode', ('BinOpNode', ('NameNode', 'a'), ('NameNode', 'b'),
'**'), ('NameNode', 'c'), '**')]

Whereas I would expect:
[('BinOpNode', ('NameNode', 'a'), ('BinOpNode', ('NameNode', 'b'),
('NameNode', 'c'), '**'), '**')]

Hopefully that syntax is clear. Any suggestions as to why I'm getting
an incorrect parse.

Alex

David Beazley

unread,
Nov 23, 2009, 9:07:50 PM11/23/09
to ply-...@googlegroups.com, David Beazley
Hmmm. It's hard to say for certain. However, I just tested the calculator example with an added power operator (right precedence) and it worked correctly. There are some debugging modes for yacc.parse() that might be worth investigating here.

http://www.dabeaz.com/ply/ply.html#ply_nn46

Cheers,
Dave
> --
>
> You received this message because you are subscribed to the Google Groups "ply-hack" group.
> To post to this group, send email to ply-...@googlegroups.com.
> To unsubscribe from this group, send email to ply-hack+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ply-hack?hl=en.
>
>

Alex Gaynor

unread,
Nov 23, 2009, 9:20:23 PM11/23/09
to ply-...@googlegroups.com
http://paste.pocoo.org/show/152562/ is the debug output. This leads
me to believe the issue is somehow related to my rule being:
expression : expression STAR STAR expression %prec POWER

instead of expressing STAR STAR as a single token. If that's the
issue I guess I just need to update my lexer.

Alex
--
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

Alex Gaynor

unread,
Nov 23, 2009, 11:39:02 PM11/23/09
to ply-...@googlegroups.com
Yep, moving to a STARSTAR token solved the issue.

Alex
Reply all
Reply to author
Forward
0 new messages