changing the Python grammar ?

Bengt Richter bokr at oz.net
Mon Nov 18 17:09:11 EST 2002


On 18 Nov 2002 12:21:52 -0800, mis6 at pitt.edu (Michele Simionato) wrote:

>Browsing on the on-line refencence manual you can find the formal
>definition of the Python grammar: http://python.org/doc/current/ref/grammar.txt
>
>It is pretty clear, for instance you see what is a valid identifier:
>
>identifier ::= 
>             (letter|"_") (letter | digit | "_")*
>  
>letter ::= 
>             lowercase | uppercase
>  
>lowercase ::= 
>             "a"..."z"
>  
>uppercase ::= 
>             "A"..."Z"
>  
>digit ::= 
>             "0"..."9"
>
>But now a question arise to me: can I modify the grammar ??
>
>Suppose for instance I want to make able Python to recognize identifiers
>starting with some funny symbol, say "@". All I must do is to modify
>the first line of the grammar as 
>
>identifier ::= 
>               ["@"] (letter|"_") (letter | digit | "_")*
>
>In the best of the worlds, I would look for some file in the Python
>source distribution containing the grammar, I would change that line, 
>and recompiling I would have a new Python able to understand identifiers
>starting with "@". Is this possible in practice ?
>
>Notice that I do NOT want to introduce ugly perlish-like identifiers in
>Python, I simply want to know how much it is possible to customize the
>Python grammar: is this a simple hack or a nightmare ?
>
>I find this possibility (if real) very intriguing, even if I see that
>this can be very dangerous.
>
>Reactions ? Comments ? Am I asking a too naive question ? I have no idea
>of how the grammar is implemented in the C code in reality.
>
The tools are all there, but depending on what kind of change you make, it
may imply the need for a host of other changes. Very tricky stuff. There are
several things you need to rebuild in the right order, to get core functionality
going with revisions.

If you have MSVC++6.x on windows, for starters read (your paths may vary):

    D:\Python-2.2.2\PC\readme.txt
    D:\Python-2.2.2\PC\example_nt\readme.txt
    D:\Python-2.2.2\PCbuild\readme.txt

Regards,
Bengt Richter



More information about the Python-list mailing list