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?
2 Answers
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.
- 48,749
- 7
- 106
- 173
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.)
- 25,388
- 6
- 66
- 89