Interpreters in Scala

You might think of interpreters as only good for writing programs. Many people learned programming on some kind of interpreter — like BASIC — because you get immediate feedback and don’t have to deal with the complexities of a compiler. But interpreters can have other uses like parsing configuration files, for example. [Sakib] has a very complete tutorial about writing an interpreter in Scala, but even if you use another language, you might find the tutorial useful.

We were impressed because the tutorial uses formal parsing using a lexer and a parser. This is how you’d be taught to do it in a computer science class, but not how everyone does it.

For example, if you wanted to parse commands of HELP, PRINT, and EXIT you could compare each string, but it is nicer to break the input into tokens (lexing) and then examine the tokens for combinations.

Doing it this way lets you identify types of tokens like “floating point number” or “integer” or “operator” and that makes interpreting things like math expressions easier. You can also more easily deal with issues like handling binding so that multiplication, for example, happens before addition.

Tiny computers can benefit from tiny interpreters. Of course, we like Forth, but that’s a different style of interpreter.

By admin