dabeaz / ply

Python Lex-Yacc

Home Page:http://www.dabeaz.com/ply/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Warn user about tuple flattening in precedence table

mrontio opened this issue · comments

When creating a precedence table, the example in the documentation suggests to do something like this:

precedence = (
     ('left', 'PLUS', 'MINUS'),
     ('left', 'TIMES', 'DIVIDE'),
 )

There's a very easy mistake to make here, if you have a single entry in the table and you miss the end comma, e.g.

precedence = (
    ('left', 'ARROW')
)

Python flattens the tuple, so the precedence would simply be ('left', 'ARROW') instead of (('left', 'ARROW'),).
The way to fix this is to simply add a , to the end of the entry, but its not clear what the issue is once you make a mistake.
This flattening trips the following line:

if not isinstance(p, (list, tuple)):

My suggestion is to make this line a bit more descriptive, including what it expected, what it got, and a note about tuple flattening:

self.log.error('Bad precedence table')

I intend on making a pull request with a solution later.

My bad. Didn't read the README.

I would accept a pull request for this.

Fantastic, I do think it will save some time in the labs.