Question about Python-Grammar

logistix at logistix at
Wed Jun 18 04:53:33 CEST 2003

Roman Meisl <roman at> wrote in message news:<6hhtev0t34c0lnj8i4g4b41iqcmhj7qr0d at>...
> Hello,
> On Mon, 16 Jun 2003 13:53:57 -0700, W Isaac Carroll
> <icarroll at> wrote:
> >The basic idea here is that "as" ought to be a keyword in Python, but 
> >for historical reasons isn't. My understanding is that making it a 
> >keyword has been looked into and it would be more trouble than it's worth.
> Thank you for your help.
> But I've to commit, with every glance I look deeper into
> python-grammar, I realize this all is weird crap!
> Why aren't things statet clear and correctly? (see also my posting
> above)
> Bye
>    Roman

They're using the grammar to enforce operator precidence.  That's what
makes things look so wierd.  Also, yes, the parser does perform
additional syntax checking that isn't explicitly enforced in the
grammar.  And thirdly, the grammar isn't LL(1) as written (although
it's close) and is transformed by PGen.

I don't know what you're trying to do, but in my experience it's much
easier to let the parser module generate a tree for you and then
reduce it into something meaningful with an algorithm like:

#psuedocode, won't execute
def reduceTree(node):
    if node is symbol and len(node) == 2: # eliminate intermediate
        return reduceTree(node[1])
    elif node is symbol: # this symbol means something, but it's
children might be intermediate nodes
        return (node, apply(reduceTree,node[1:]))
    else: # it's a token, return as is
        return node

The compiler package also transforms the ASTs into more meaningful

More information about the Python-list mailing list