Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a compiler or interpreter using Python [closed]

Right now I'm writing my PhD proposal to build a language processor for a new specification language for Java (cf. JML, or Spec# for C#) and need to nail down an implementation tool to start development. The research aspects of the language (syntax, semantics, theoretical results) are orthogonal to my choice of implementation, so I'd like to use Python (2.6+) for my own reasons. The end-product will be either a compiler or interpreter capable of verifying some specified properties for programs written in Java.

What's the best framework/library for building compilers/interpreters in Python? Are the "batteries included" for this problem?

Bonus points awarded to solutions that have reference compilers for Java 6+.

like image 239
Steve Shaner Avatar asked Jul 30 '10 17:07

Steve Shaner


People also ask

Is Python a compiler or interpreter?

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from major compiled languages, such as C and C + +, as Python code is not required to be built and linked like code for these languages.

How do you create an interpreter in Python?

To create an interpreter first you need to create a lexer to get the tokens of your input program. Next you create a parser that takes those tokens and, by following the rules of a formal grammar, returns an AST of your input program. Finally, the interpreter takes that AST and interprets it in some way.

Why does Python use interpreter instead of compiler?

Python does not need a compiler because it relies on an application (called an interpreter) that compiles and runs the code without storing the machine code being created in a form that you can easily access or distribute.

What interpreter does Python use?

The Python interpreter is a bytecode interpreter: its input is instruction sets called bytecode. When you write Python, the lexer, parser, and compiler generate code objects for the interpreter to operate on.


2 Answers

I personally can't stand antlr, I use lex/yacc as my parser generator. Here is a Python implementation http://www.dabeaz.com/ply/ that you could use.

That just deals with parsing though, that really doesn't even begin to construct your interpreter. For that, you'll probably be building it from the ground up - I've never heard of a library specifically geared towards this (I would be excited to see some of them, please link me there in the comments if you know of any).

Check out this SO post how to start writing a very simple programming language it has good ideas.il.

like image 143
sholsapp Avatar answered Sep 18 '22 16:09

sholsapp


maybe you want to have a look at this

like image 21
AndersK Avatar answered Sep 21 '22 16:09

AndersK