Why must implementing Python be hard unlike Scheme?

Jason tenax.raccoon at gmail.com
Wed Feb 20 10:14:02 EST 2008


On Feb 19, 11:22 pm, "seber... at spawar.navy.mil"
<seber... at spawar.navy.mil> wrote:
> On Feb 19, 9:49 pm, Kay Schluehr <kay.schlu... at gmx.net> wrote:
>
> > Building a
> > Python VM in a high level language is certainly not harder than
> > creating a Scheme interpreter.
>
> Does VM = interpreter?
> Are you saying implementing a toy Python interpreter is not any harder
> than implementing a toy Scheme interpreter?
>
> I don't understand why you think that.
> The Python grammar is much more complicated.  Python ASTs are much
> more complicated.  Parsing Python files with the whitespace is harder.
> Please prove me wrong.  I hope you are right.
>
> Chris

If you create a byte-code interpreter, you don't need to worry about
the AST or whitespace.  Python code compiles down into a byte-code
that runs on a simple virtual machine.  Values are put in a stack,
then popped off the stack to perform operations.  Jython compiles
Python into the Java byte-code machine, while IronPython
generates .NET / CLR byte codes.

The dis module allows you to "disassemble" the byte-code instructions,
at least for CPython.  Take a look at "http://docs.python.org/lib/
module-dis.html".  (There's a link at the bottom to a page that
describes what the byte-codes in CPython are.)

You also don't have to jump directly to doing everything yourself.
Python has modules that will already lex (module tokenize) and parse
(module parser) the language, so you can use of the modules to focus
on implementing the other module.

Python is somewhat more difficult to parse because it isn't a
functional language.  While functional languages are easier to
implement, they aren't as easy for the beginner or average programmer
to use.  I know how to work within the MFC and wxWidget/wxPython
frameworks, but darned if I know how Scheme could be used to implement
a COM object.

Take a look at Shalabh Chaturvedi's "Python Types and Objects" [1].
This describes how Python's type and object objects work.  Since this
is the top of Python's new-style classes, getting this right is
imperative.

I hope this gives you some idea on how to proceed.  Python's grammar
really isn't that difficult.  Python uses a LL parser [2], while many
languages uses LALR parsers [3].  The parts of the BNF are described
in detail in Python's "Python Reference Manual" [4].

I hope that the resources help.

  --Jason

[1] Python Types and Objects:
http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html
[2] LL Parsers: http://en.wikipedia.org/wiki/LL_parser
[3] LALR Parsers: http://en.wikipedia.org/wiki/LALR_parser
[4] Python Reference Manual: http://docs.python.org/ref/ref.html



More information about the Python-list mailing list