Compiler writing spans programming languages, machine architecture, languagetheory, algorithms, and software engineering. Fortunately, a few basic compiler-writing techniques can be used to construct translators for a wide variety oflanguages and machines.A
is a program that reads a program written in one language – thesource language – and translates it into an equivalent program in another language– the target language as illustrated in Figure 1.1 in which the important part ofthe translation process is that the compiler reports to its user the presence oferrors in the source program.At first glance, the variety of compilers may appear overwhelming. There arethousands of source languages, ranging from traditional programming languages suchas Fortran and Pascal to specialized languages that have arisen in virtually everyarea of computer application. Target languages are equally as varied; a targetlanguage may be another programming language, or the machine language of anycomputer between a microprocessor and a supercomputer.A
translates a source program into machine language. An
program reads individual words of a source program and immediately executescorresponding machine-language segments. Interpretation occurs each time theprogram is used. Thus, once it has been compiled, a program written into a compiledlanguage will run more quickly than a program in an interpreted language.An
is a computer program that translates commands written in ahigh-level computer language into machine-language commands that the computer canunderstand and execute.
An interpreter's function is thus similar to that of acompiler, but the two differ in their mode of operation.
translates acomplete set of high-level commands, producing a corresponding set of machine-language commands that are then executed, whereas an
translates andexecutes each command as the user enters it. Interpretive languages, such as thewidely used BASIC, are relatively easy to learn, and programs written in them areeasy to edit and correct. Compiled programs, on the other hand, operate morerapidly and efficiently.T
There are two parts of compilation: analysis and synthesis. The analysis partbreaks up the source program into constituent pieces and creates an intermediaterepresentation of the source program. The synthesis part constructs the desiredtarget program from the intermediate representation.During analysis, the operations implied by the source program are determinedand recorded in a hierarchical structure called a tree. It is a special kind oftree called a syntax tree, in which each node represents an operation and thechildren of a node represent the arguments of the operation. An example is shown in
Saint Paul University – San Nicolas Campus
Figure 1.1 A Compiler error messages