Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a programming language in Python [closed]

I have seen a lot of tutorials for making a programming language, but very few for writing one in Python. I would like to know how to (relatively easily) create a programming language using Python.

like image 265
elijaheac Avatar asked Jan 14 '12 16:01

elijaheac


People also ask

Can I create a programming language in Python?

PLY stands for Python Lex Yacc. It is a library you can use to make your own programming language with python. Lex is a well known library for writing lexers. Yacc stands for "Yet Another Compiler Compiler" which means it compiles new languages, which are compilers themself.

What language is Python close to?

Python is often compared to other interpreted languages such as Java, JavaScript, Perl, Tcl, or Smalltalk. Comparisons to C++, Common Lisp and Scheme can also be enlightening.

Can I create my own programming language?

You can just take a subset of an existing language or come up with a simple variation of it and get started. However, if you have plans for creating your very own programming language, you will have to give it some thought. I think of designing a programming language as divided two phases: The big-picture phase.


2 Answers

Not sure what you mean by "creating a programming language". But I think you might like to read Peter Norvig's excellent article (How to Write a (Lisp) Interpreter (in Python)). This shows how you can build a Lisp interpreter in only 90 lines of Python!

Once you understood that, try (An ((Even Better) Lisp) Interpreter (in Python)).

like image 57
Tim Pietzcker Avatar answered Oct 30 '22 16:10

Tim Pietzcker


  1. Imagine your language. What do you want it to look like? What features should it have?
  2. Think of an existing language that is as similar as possible to your desired language. It's fine if the keywords are all different, but if you decided to make Python you wouldn't start with Lisp because the structures are fundamentally very different.
  3. Find an existing grammar for the language you chose in step 2. I'd look here: http://www.antlr3.org/grammar/list.html . If you can't find one, do step 2 again.
  4. Using ANTLR (or whatever parser generator understands the grammar you found in step 3), build a Python module that understands the language you chose in step 2. ANTLR has some level of support for a Python "target" (meaning that the parser code will be in Python, as opposed to making a parser that understands the Python language). If you get stuck with parser code in C (which you may), write Python bindings for it (probably easiest using Boost Python, but you could use the Python C API directly if you are pretty familiar with both C and Python).
  5. Start making modifications (in small steps at first) to the grammar from step 3 to make it more like the language you designed in step 1.

Do these things carefully and deliberately, and after a few days of work you may have a halfway-decent parser for your language. Then you'll need to consume the output of the parser (if using ANTLR, consider using the Abstract Syntax Trees, or ASTs, that it can generate for you). Then you'll need to convert the parsed syntax into a target language, such as x86 assembly or some intermediate bytecode such as the one used by Java, Lua, Microsoft .NET, or whatever.

Good luck, and be forewarned: this process will take a long time to do right.

like image 41
John Zwinck Avatar answered Oct 30 '22 18:10

John Zwinck