←back to thread

160 points leontrolski | 1 comments | | HN request time: 0.202s | source
1. dalke ◴[] No.41890239[source]
In the bygone era of 2008, I had fun adding Perl/Ruby-style pattern matching.

  for line in open("python_yacc.py"):
      if line =~ m/def (\w+)/:
          print repr($1)
See http://www.dalkescientific.com/writings/diary/archive/2008/0...

I started by writing a Python grammar definition based on PLY (see http://www.dabeaz.com/ply/) , then tweaked it to handle the new feature and emit the right AST. It's when I discovered the following was valid Python:

  >>> a=b=c=d=e=1
  >>> del a, (b, (c, (((((d,e)))))))
I don't think PLY can handle the newest Python grammar, but I haven't looked into it.

For what it's worth, my Python grammar was based on an older project I did called GardenSnake, which still available as a PLY example, at https://github.com/dabeaz/ply/tree/master/example/GardenSnak... .

I've been told it's was one of the few examples of how to handle an indentation-based grammar using a yacc-esque parser generator.