Writing an interpreter for language similar to python!!

Paul Boddie paul at boddie.org.uk
Fri Mar 2 13:03:03 CET 2007


On 28 Feb, 18:38, "luvsat" <luv.... at gmail.com> wrote:
>
> I am new to python and working on a project that involves designing a
> new language. The grammar of the language is very much inspired from
> python as in is supports nearly all the statements and expressions
> that are supported by python. Since my project is in initial stage, so
> I think it would be appropriate if I clarify the following questions:

Sounds interesting!

> 1. Would it make sense if I parse the whole program from scratch and
> then construct the valid python strings back so that they can be
> executed using ''exec'' and ''eval'' commands?

I wouldn't bother parsing the program from scratch - there's a module
called "compiler" in the standard library which will give you an
abstract syntax tree for virtually all of the syntax supported by the
version of Python you're using. Despite complaints about the API, it's
quite easy to work with and will save you from dealing with the
tedious details of actually parsing the source code. If you want to
produce the source code from the AST, some people have written visitor
classes which will probably do what you want.

Paul




More information about the Python-list mailing list