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
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.
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
- 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.)