3. Overview
3.1 Interpreter
A practical way to study a programming language is to build a program that can run programs written in that language. Such a program that reads and evaluates a program is called an interpreter.
3.2 PLCC
PLCC, first mentioned in the previous chapter, is a powerful tool that can generate such an interpreter. PLCC generates an interpreter based on a textual description of the programming language we wish to design. We refer to the textual description as a specification.
The diagram below depicts the relationships among language designers, programmers, and PLCC. Language designers write a specification for the new language they are designing. They use PLCC to generate an interpreter for the new language from its specification. Then programmers write programs in this new language and use the generated interpreter to run these programs.
In the course of this textbook, we will take on the role of a language designer.
3.3 Language specification
Most interpreters are built around three successive phases: lexical analysis, syntactic analysis, and semantic analysis.
The composition of the specification read by PLCC reflects the typical interpreter's organization. As we can see in the figure below, the specification is broken up in three parts: lexical specification, syntactical specification, and semantic specification.
Based on the lexical specification, PLCC generates a scanner whose main responsibility is to convert a stream of characters (the source) into a stream of tokens. For now, it suffices to say that a token adds a layer of abstraction over text to simplify the job of the parser.
Based on the syntactical specification, PLCC generates a parser whose main responsibility is to generate an abstract syntax tree. The structure of the tree facilitates checking that the program is well formed and performing semantic actions in the next phase.
Based on the semantic specification, PLCC generates an interpreter that performs the actions programmed in the input according to the meaning imbued in said specification.
The next few chapters will go over each of these parts of the specification in greater detail.
3.4 Going beyond
Any program processing any input expressed in a text-based programming language must perform lexical analysis, syntactic analysis, and semantic analysis. Such programs include compilers, interpreters, and static analyzers (i.e., linters). So the concepts described in this chapter apply to all such tools.
We will see that the language we use to define these specifications is itself a small programming language. We could, in principle, write a specification for the PLCC tool in its own language and ask PLCC to generate an alternate version of itself.