One of the fun parts of pairing with Amy on Nagini was modifying the grammar of Python. You should try this – it’s easier than you think! Eli Bendersky has a great post with step-by-step instructions.1
Modifying Python’s grammar starts in the Grammar/Grammar file. I’ve recently learned how to read this (which, for me, mostly means learning how to pronounce the punctuation), so I want to walk through the
import example in some detail. The syntax here is Extended Backus-Naur Form, or EBNF. You read it like a tree, and your primary verb is “consists of”:
import_stmtconsists of one of two forms,
import_nameconsists of the literal word
dotted_as_namesconsists of a
dotted_as_name(note the singular), optionally followed by one or more pairs of a comma and another
dotted_as_nameconsists of a
dotted_name, optionally followed by the literal word ‘as’ and a NAME.
dotted_nameconsists of a
NAME, maybe followed by pairs of a dot and another
You can walk the other branches in a similar way.
1 2 3 4 5 6 7 8 9 10
accio-ify Python, we had to replace the occurences of
'accio'. There are only two – we were only interested in the literal string
import, not all the other names.
import_as_name and so on are just nodes in the tree, and only matter to the parser and compiler.
Every other keyword and symbol that has special meaning to the Python parser also appears in Grammar as a string.
Perusing the grammar is (goofy) way to learn about corner cases of Python syntax, too! For example, did you know that
with can take more than one context manager? It’s right there in the grammar:
1 2 3 4 5 6 7 8
Now go ahead and add your favorite keyword into Python!
Like Eli, I’m not advocating for Python’s actual grammar to change – it’s just a fun exercise.↩