Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an interpreter?

I have decided to write a small interpreter as my next project, in Ruby. What knowledge/skills will I need to have to be successful?
I haven't decided on the language to interpret yet, but I am looking for something that is not a toy language, but would be relatively easy to write an interpreter for. Thanks in advance.

like image 964
bennybdbc Avatar asked Dec 18 '09 07:12

bennybdbc


People also ask

How do you write a language interpreter?

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.

What is an interpreter example?

An Interpreter directly executes instructions written in a programming or scripting language without previously converting them to an object code or machine code. Examples of interpreted languages are Perl, Python and Matlab. Following are some interesting facts about interpreters and compilers.

How do you describe an interpreter?

Interpreters interpret verbal communication from one language to another, and act as mediums where language barriers exist. At times, Interpreters translate written communication from one language to another. Interpreters work in education, health care, insurance, legal and other industries.


5 Answers

You will have to learn at least:

  • lexical analysis (grouping characters into tokens)
  • parsing (grouping tokens together into structure)
  • abstract syntax trees (representing program structure in a data structure)
  • data representation (assuming your language will have variables)
  • an evaluation loop that "runs" your program

An excellent introduction to some of these topics can be found in the introductory text Structure and Interpretation of Computer Programs. The language used in that book is Scheme, which is a robust, well-specified language that is ideally suited for your first interpreter implementation. Highly recommended.

like image 83
Greg Hewgill Avatar answered Oct 12 '22 07:10

Greg Hewgill


I haven't decided on the language to interpret yet, but I am looking for something that is not a toy language, but would be relatively easy to write an interpreter for. Thanks in advance.

Try some dialect of Lisp like Scheme or Clojure. (Now there's an idea: Clojure-in-Ruby, which integrates with Ruby as well as Clojure does with Java.)

With Lisp, there is no need to bother with idiosyncracies of syntax, as Lisp's syntax is much closer to the abstract syntax tree.

like image 31
Joshua Fox Avatar answered Oct 12 '22 05:10

Joshua Fox


This SICP chapter shows how to write a Lisp interpreter in Lisp (a metacircular evaluator). In my opinion this is the best place to start. Then you can move on to Lisp in Small Pieces to learn how to write advanced interpreters and compilers for Lisp. The advantage of implementing a language like Lisp (in Lisp itself!) is that you get the lexical analyzer, parser, AST, data/program representation and REPL for free. You can concentrate on the task of getting your great language working!

like image 41
Vijay Mathew Avatar answered Oct 12 '22 07:10

Vijay Mathew


There is Tree top project wich can be helpful for you http://treetop.rubyforge.org/

like image 28
Sebastian Avatar answered Oct 12 '22 07:10

Sebastian


You can checkout Ruby Draft Specification http://ruby-std.netlab.jp/

like image 36
allenwei Avatar answered Oct 12 '22 07:10

allenwei