How to create a simple interpreter? [closed]

4

I've 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?

    
asked by anonymous 23.09.2015 / 21:52

1 answer

5

A few years ago I needed to allow the user to set up label species.

Basically he wrote a text something 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 set up various 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

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

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

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

In short, ANTLR reads a language definition written in Backus-Naur Form (BNF) and generates a parser for codes 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 execute what was determined by the code you just interpreted.

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

    
24.09.2015 / 00:34