Yet another bison wrapper in pure python

Christopher Li chrisl at gnuchina.org
Fri Mar 23 14:32:40 EST 2001


Hi,

I have write yet another bison wrapper, yes I know there are many of them.
I like to use bison because it has a very nice output to help me debug
the grammer conflict in the .y file.

The idea is that
1. put the python statment in .y file just like C statment.
   it need to pay attention about the indent though.
2. invoke bison to generate the .tab.c file.
3. scan the .tab.c file to extract the tables(array) and the action
   statment, which is the python code.
4. put them together in another python program using the table.
   I write a  python version of the bison parser C code. It is just
   a python exactly remap of the C code.

I use this trick to write the chasm, and feel this tool is very handy
because do not need to compile the C module. Just the edit-run-debug
cycle. BTW, chasm is a C style Asm, it is in pure python,
http://sourceforge.net/projects/ch-asm/

I really enjoy python as a rapid phototype language.

I take the bison wrapper and some help function to do lexer in a tar ball.
http://gnuchina/~chrisl/pb.tgz

It include an example of the Multi-function Calc which is the python
version of the third Bison info example. I use it to debug the python
parser itself. The python version is smaller than the C version and
more powerful (It support all the math function in one blow)
-----------------cut--------------------
[root at chris pb]# python pbison -p bisonparser ptest2.y
ptest2.y contains 4 shift/reduce conflicts.
[root at chris pb]# python ptest2.py
>>>a = sin(1)+4-5
        -0.158529015192
>>>b = a+6
        5.84147098481
>>>
--------------------------------------
The code can be improved if there is a need. And if want to speed it up.
We can build a reusable C version of the parser module, table and
action still be the python. I guess the "PyBison" does similar things
but I can't download it, its link is broken.

BTW, seems the I have to use the python2.0 "pre" module as re. otherwise
scan big tables will have some problem.

And, is there a better way to do a long "switch" like statment?
Right now I map a deduce action as a hash of functions. and all the
state of parser is function too. anyway, it is not aim at run fast
at the first place.

Any idea and suggestion is welcome.

Chris





More information about the Python-list mailing list