changing the Python grammar ?

Michele Simionato mis6 at pitt.edu
Mon Nov 18 15:21:52 EST 2002


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.

--
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/



More information about the Python-list mailing list