How to create a programming language in Python [closed] How to create a programming language in Python [closed] python python

How to create a programming language in Python [closed]


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)).


  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.