How to create a simple interpreter?

Asked

Viewed 1,311 times

4

I always wanted to create a language (something simple) for myself but I have no idea how. The question is: How to create a simple interpreter ?

  • 7

    This question seems too wide. Do you want an overview of the steps needed to create an interpreter? Or is your question at a more specific stage? (type, how to parse the text, or how to create an intermediate code that the interpreter can execute, etc)

  • 1

    This Codeproject article teaches how to create an interpreter on . NET. It’s basically done using a lot of regular expression. Going deeper into the pain, there is this manual that teaches to create a programming language. It doesn’t replace a compiler course yet, but it’s a start.

  • This technique of using regular expressions to transform one language into another is simple but surprisingly powerful! The project Processing.js (by John Resig, author of jQuery) for example managed to port the language Processing for Javascript simply using a series of regexes to translate one language to another, in its first version at least, and then doing the actual interpretation (using Canvas).

  • It’s true. I had a lot about regular expression in a subject called Computer Theory. It was the introduction to Compilers.

  • Depends, you can create an interpreter for a language that runs on a webbrowser as in this answer http://answall.com/a/5358/3635 -- However if you want to create a "language" that Compile or interpret without the browser, you will need another language first, for example c++ and you will then create an executable that will do a "parse" in your script (with language invented by you) and will run or compile and then run :)

  • 1

    Too bad sometimes the person asks, disappears, and receives almost immediate feedback that is left unanswered... @Math, it would not be the case to suck directly this tag and take out that soup that only increases the confusion? :)

  • 1

    @brasofilo hecho

Show 2 more comments

1 answer

5

A few years ago I needed to allow the user to configure species of labels.

Basically he wrote a text more or less like this:

    Valor medido: {valor_medido}, Valor considerado: {valor_medido * 0.9}
    Data da medição: {formatar(data_medicao, "dd/mm/aaaa")}

Finally, using system variables and previously defined functions, it configured several labels to be used in specific situations, and in the end the system printed something like:

    Valor medido: 80, Valor considerado: 72
    Data da medição: 23/09/2015

Features were quite extensive: formatting dates and numbers, conditional information (using if), etc..

In the end, we ended up delivering a real programming language interpreter, although we did very little on our own.

To implement this feature, we use the project ANTLR, and as a starting point we use the project State of the Art Expression Evaluation (using the ANTLR).

In a nutshell, the ANTLR reads a definition of written language in Backus-Naur Form (BNF) and generates a parser for code written in that language.

The generated parser is then able to interpret a code and deliver an object tree (kind of tokens) that describes the interpreted code. And then you go through the tree and you run what was determined by the code you just interpreted.

These two projects that Linkei should teach you a lot about programming language interpreters.

  • 1

    The question is too broad and I did not intend to answer it fully, but I thought it was worth a reference to these projects so valuable that they have much to teach.

  • 2

    I found the question great, pity that it requires some work to be pertinent to the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.