Interpreter is a computer program that reads the source code written in a particular language and executes the instructions. On the other hand compiler is a computer program that converts the source code written in one language into a lower level language which can then be run to execute the instructions.
Java
Based on book by Ronal Mak
- Compiler has two ends - frontend and a backend.
- Frontend does the initial translation and syntax checking etc.
- Frontend consists of Parser, Scanner, Source and Token.
- Parser controls the frontend execution.
- Parser calls scanner methods to get next token and/or current token.
- Scanner tokenizes the the source program.
- Source represents the source program.
This is truely awesome. The frontend generates an Intermediate code and a symbol table which the backend of a compiler can process. Intermediate code is predigested form of source code that backend can work with and symbol table is a table of the values of identifiers.
- Backend does not need to know the source language the program was written in as long as it has the intermediate code and the symbol table. Backend distinguishes between if code has to be interpreted or compiled.
- Frontend needs to know the source program language but doesn't care if the code is going to be compiled or interpreted.
- Basically we can use the same frontend to supply code to multiple backends and we can use a single backend to process multiple source languages. Awesome.
Messages allow for parser/source or any other entity to report information. For example, parser may want to report a syntax error. Similarly, a source may want to report a line read.
- Any
MessageProducer
(parser/source etc) does not care who receives the message or what they do with the message.
- In our case if someone wants to be a message producer, they need to implement
MessageProducer
interface and implement the methods it offers. - After implementing the interface, the
Parser
orSource
may not define the methods themselves, they delegate control toMessageHandler
class which defines these functionalities. It is like saying, I want to be a message producer, so I implement theMessageProducer
interface and its methods, but I letMessageHandler
take care of the functionalities for me. I love OOP.
- The functionality of a message producer is actually defined in
MessageHandler
class. This allows other classes to useMessageHandler
. This is more useful than defining the functionality in the message producer such asParser
orSource
as now any other class that wants to use this functionality doesn't need to extend fromParser
orSource
.