Railroad diagrams for Python Grammar


(Pablo Galindo Salgado) #1

I was playing recently with parsers and I wanted to have a clear way of displaying railroad diagrams for the Python Grammar to make a quick comparison of DFAs and NDFAs (maybe even before and after some simplification steps). Because everything is already invented, I found this webpage that allows you to upload some grammar in W3C’s EBN notation and it generates nice interactive diagrams. I have adapted the Python Grammar to this notation and you can visualize the grammar very quickly:

  1. Go to https://www.bottlecaps.de/rr/ui
  2. Click the “Edit Grammar” tab.
  3. Paste the contents of this gist:
    https://gist.github.com/pablogsal/9014db8c895df0e831b1965b2c65d2e8
  4. Click the “View Diagram” tab.
  5. Profit. :moneybag:

Some cool examples:

Typedarglist

dictorsetmaker


(Neil Schemenauer) #2

The first real textbook I had on programming used railroad diagrams. It was for Object Pascal. The other day, I found this talk by Guy Steele. He covers the history of formal grammar specification.


(Guido van Rossum) #3

When I was a poor college student, Pascal was my favorite language. I borrowed the textbook from the library, photocopied the railroad diagrams from the back of the book, and carried those with me for many years.

Also, did you know that Python’s Grammar file once (about 25 years ago) had embedded comments that enabled some proprietary tool to create railroad diagrams from it? (https://github.com/python/cpython/commit/a322862eff7da979269f8f0f5d1f3d278fc64ced) I don’t know exactly when these were eradicated.