Python's parser.

Michael Hudson mwh21 at cam.ac.uk
Wed May 10 13:53:22 EDT 2000


I thought I might waste some time by having a crack at implementing +=
and friends in Python.  I think I know what I want to do on the
codegen side, but at the moment that hurdle looks some way off.

How much effort is involved to get Python to accept a '*=' token?

I've 

1) Added a line 

aexpr_stmt: testlist '*=' testlist

at the end of Grammar/Grammar, and changed

small_stmt: expr_stmt | print_stmt  | del_stmt | pass_stmt 
          | flow_stmt | import_stmt | global_stmt | exec_stmt 
          | assert_stmt

to

small_stmt: expr_stmt | aexpr_stmt | print_stmt | del_stmt | pass_stmt
          | flow_stmt | import_stmt | global_stmt | exec_stmt 
          | assert_stmt

2) changed

#define DOUBLESTAR      36
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
#define OP              37

in Include/token.h to 

#define DOUBLESTAR      36
#define TIMESEQUAL      37
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
#define OP              38

3) Added "TIMESEQUAL" to _PyParser_TokenNames in tokenizer.c

4) Changed 

                switch (c2) {
                case '=':       return GREATEREQUAL;
                case '>':       return RIGHTSHIFT;
                }
                break;

in Parser/tokenizer.c:PyToken_TwoChars to:

                switch (c2) {
                case '=':       return GREATEREQUAL;
                case '>':       return RIGHTSHIFT;
                }
                break;

5) Made clean, made the parser, made python.

But when I fire up the new ./python I get:

XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
XXX ambiguity!
Python 1.6a2 (#6, May 10 2000, 17:36:56)  [GCC 2.95.2 19991024 (release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  File "/home/mwh21/.pythonrc", line 7
    readline.set_completer(mycompleter.complete)
                                               ^
SyntaxError: invalid syntax
>>>

which, I take it, is bad.

Help?  Anyone?  This can't be *that* hard, can it?

TIA,
Michael 

-- 
  About the use of language: it is impossible to sharpen a 
  pencil with a blunt axe. It is equally vain to try to do 
  it with ten blunt axes instead.
     -- E.W.Dijkstra, 18th June 1975. Perl did not exist at the time.



More information about the Python-list mailing list