1

I'm looking for a book to learn how to implement interpreters for programming languages. Thing is there are much more 'compiler books' than 'interpreter books'. So my question is: can I read a book that teaches how to build compilers, to learn how to build interpreters (at a very beginner level)? Is this a good idea? If so, what do I need to keep in mind while reading?

closed as off-topic by Bart van Ingen Schenau, Ampt, Kilian Foth, ChrisF Aug 7 '14 at 13:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Bart van Ingen Schenau, Ampt, ChrisF
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Definitely yes. See this answer and the references there. Mostly because even interpreters are transforming the source code byte streams (into AST or bytecode). – Basile Starynkevitch Jul 23 '14 at 10:41
7

Absolutely - an interpreter is just a "one line at a time" compiler. It performs much the same task, that of taking some form of human-understandable source code and turning it into something a computer processor can understand. A compiler will do this for entire source file(s), whereas an interpreter will do this on an as-read basis.

You will need to handle a few differences around loading source as needed, and handling parsing source files to find the next line to read, but otherwise you'll be implementing a compiler fundamentally.

  • Thanks. Two questions: A- Is there anything I need to keep in mind while reading a compiler book and trying to understand interpreters from it? B- A good beginner-friendly book you can recommend? I hear the dragon book is too much for a complete beginner. – Aviv Cohn Jul 23 '14 at 10:52
  • See references here – Basile Starynkevitch Jul 23 '14 at 11:15
  • So basically: a compiler book teaches me what to do to an entire source code file, so in an interpreter I take that and simply do it to each individual line of code, from the top down? I'm asking because I want to understand how to apply what Im learning from my compiler book to implementing an interpreter. – Aviv Cohn Jul 23 '14 at 15:38
3

Is a book that teaches how to build compilers good for learning to implement interpreters?

Yes - A good book on compilers will cover a wide range of topics, many of which are directly relevant to interpreters / interpreted languages. For example:

  • lexical analysis
  • parsing
  • creation of an AST
  • type checking, identifier resolution and other kinds of semantic analysis
  • "compiler" error reporting
  • possibly ... generation of an abstract machine code that the interpreter will "execute".

If so, what do I need to keep in mind while reading?

Keep in mind that some of the material in the book may not be that relevant. For example, a typical abstract machine is register-less, so the sections of the book on register allocation during code generation are typically not relevant to an interpreter.

(But that's just common sense. You wouldn't normally read a book like that from cover to cover. You'd typically skim the bits that don't seem relevant to your goals.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.