Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreter in Python: Making your own programming language?

Remember, this is using python. Well, I was fiddling around with an app I made called Pyline, today. It is a command line-like interface, with some cool features. However, I had an idea while making it: Since its like a "OS", wont it have its own language?

Well, I have seen some articles online on how to make a interpreter, and parser, and compiler, but it wasn't really readable for me. All I saw was a crapload of code. I am one of those guys who need comments or a readme or SOME form or communication towards the user without the code itself, so I think that Stack Overflow would be great for a teenager like me. Can I get some help?

like image 314
Galilsnap Avatar asked Jun 25 '10 06:06

Galilsnap


People also ask

How do I 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.

Can I make 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.

Can you write a Python interpreter in Python?

A Python Python Interpreter Byterun is a Python interpreter written in Python. This may strike you as odd, but it's no more odd than writing a C compiler in C. (Indeed, the widely used C compiler gcc is written in C.) You could write a Python interpreter in almost any language.

Does interpreter generate code?

An Interpreter directly executes instructions written in a programming or scripting language without previously converting them to an object code or machine code.


2 Answers

You need some grounding first in order to actually create a programming language. I strongly suggest picking up a copy of Programming Language Pragmatics, which is quite readable (much more so than the Dragon book) and suitable for self study.

Once you are ready to start messing with parsers, ANTLR is the "gold" standard for parser generators in terms of usability (though flex+bison/yacc are quite capable).

like image 191
Yann Ramin Avatar answered Oct 13 '22 12:10

Yann Ramin


I just came by Xtext, a language development framework. Perhaps that's something you might want to take a look at.

Considering Python you might find it instructive to implement a version of Logo. If you want, you can skip the parsing/lexing stage for now and come up with a object oriented version first to get you going if your OOP skills are up to it. Later on you can hook it up with some graphics library to actually draw something.

In addition to Logo you might want to check out L-systems. See particularly The Algorithmic Beauty of Plants for inspiration.

like image 22
Juho Vepsäläinen Avatar answered Oct 13 '22 13:10

Juho Vepsäläinen