Chapter 1 (Introduction)
Chapter 1 (Introduction)
Chapter 1 (Introduction)
An important role of the compiler is to report any errors in the source
program that it detects during the translation process.
1
Compiler
2
Compiler
• If the target program is an executable machine-language program, it
can then be called by the user to process inputs and produce outputs;
see Fig. 1.2.
3
Interpreter
An interpreter is another common kind of language processor. Instead
of producing a target program as a translation, an interpreter appears
to directly execute the operations specified in the source program on
inputs supplied by the user, as shown in Fig . 1.3.
4
Interpreter vs. Compiler
5
Language Processors
Example 1.1 : Java language processors combine compilation and interpretation,
as shown in Fig. 1.4. A Java source program may first be compiled into an
intermediate form called bytecodes. The bytecodes are then interpreted by a
virtual machine. A benefit of this arrangement is that byte codes compiled on one
machine can be interpreted on another machine , perhaps across a network.
In order to achieve faster processing of inputs to outputs, some Java compilers,
called just-in-time compilers, translate the bytecodes into machine language
immediately before they run the intermediate program to process the input.
6
Language Processors
7
pre-processor
8
Compiler
9
Linker
• Large programs are often compiled in pieces, so the relocatable machine
code may have to be linked together with other relocatable object files and
library files into the code that actually runs on the machine.
• The linker resolves external memory addresses, where the code in one file
may refer to a location in another file.
• The loader then puts together all of the executable object files into
memory for execution.
10
Language Processing
11
Structure of a Compiler
• The analysis part breaks up the source program into constituent pieces and
imposes a grammatical structure on them. It then uses this structure to
create an intermediate representation of the source program.
• If the analysis part detects that the source program is either syntactically ill
formed or semantically unsound, then it must provide informative
messages, so the user can take corrective action.
12
Structure of a Compiler
• The analysis part also collects information about the source program and
stores it in a data structure called a symbol table, which is passed along
with the intermediate representation to the synthesis part.
• The synthesis part constructs the desired target program from the
intermediate representation and the information in the symbol table.
• The analysis part is often called the front end of the compiler; the synthesis
part is the back end.
13
Structure of a Compiler
If we examine the compilation process in more detail, we see that it operates as a
sequence of phases, each of which transforms one representation of the source
program to another. A typical decomposition of a compiler into phases is shown
in Fig. 1 .6.
In practice, several phases may be grouped together, and the intermediate
representations between the grouped phases need not be constructed explicitly.
The symbol table, which stores information about the entire source program, is
used by all phases of the compiler.
14
Structure of a Compiler
15
Lexical Analysis
16
Lexical Analysis
In the token, the first component token- name is an abstract symbol that is
used during syntax analysis , and the second component attribute-value
points to an entry in the symbol table for this token.
17
Lexical Analysis
• The characters in this assignment could be grouped into the
following lexemes and mapped into the following tokens passed on to the
syntax analyser:
18
Lexical Analysis
2. The assignment symbol = is a lexeme that is mapped into the token {=).
Since this token needs no attribute-value, we have omitted the second
component . We could have used any abstract symbol such as assign for
the token-name , but for notational convenience we have chosen to use
the lexeme itself as the name of the abstract symbol.
3. Initial is a lexeme that is mapped into the token (id, 2 ) , where 2 points
to the symbol-table entry for initial .
19
Lexical Analysis
5. rate is a lexeme that is mapped into the token ( id, 3 ) , where 3 points to
the symbol-table entry for rate.
20
Lexical Analysis
21
Structure of a Compiler
22
Syntax Analysis
The second phase of the compiler is syntax analysis or parsing. The parser
uses the first components of the tokens produced by the lexical analyser to
create a tree-like intermediate representation that depicts the grammatical
structure of the token stream.
A typical representation is a syntax tree in which each interior node
represents an operation and the children of the node represent the
arguments of the operation.
A syntax tree for the token stream (1.2) is shown as the output of the
syntactic analyser in Fig. 1.7.
23
Syntax Analysis
This tree shows the order in which the operations in the assignment are to be
performed.
The tree has an interior node labelled * with ( id, 3 ) as its left child and the
integer 60 as its right child. The node (id, 3) represents the identifier rate. The
node labelled * makes it explicit that we must first multiply the value of rate by
60.
The node labelled + indicates that we must add the result of this multiplication to
the value of initial.
24
Syntax Analysis
• The root of the tree, labelled =, indicates that we must store the
result of this addition into the location for the identifier position.
25
Syntax Analysis
26
Semantic Analysis
The semantic analyser uses the syntax tree and the information in the symbol table to
check the source program for semantic consistency with the language definition.
It also gathers type information and saves it in either the syntax tree or the symbol table,
for subsequent use during intermediate-code generation.
An important part of semantic analysis is type checking, where the compiler checks that
each operator has matching operands. For example, many programming language
definitions require an array index to be an integer; the compiler must report an error if a
floating-point number is used to index an array.
27
Semantic Analysis
• Such a coercion appears in Fig. 1 . 7. Suppose that position, initial, and rate have
been declared to be floating-point numbers, and that the lexeme 60 by itself
forms an integer.
• The type checker in the semantic analyser in Fig. 1.7 discovers that the operator *
is applied to a floating-point number rate and an integer 60. In this case, the
integer may be converted into a floating-point number.
• In Fig. 1 .7, notice that the output of the semantic analyser has an extra node for
the operator inttofloat , which explicitly converts its integer argument into a
floating-point number. 28
Intermediate Code Generation
29
Intermediate Code Generation
After syntax and semantic analysis of the source program, many compilers
generate an explicit low-level or machine-like intermediate representation,
which we can think of as a program for an abstract machine.
30
Intermediate Code Generation
• The output of the intermediate code generator in Fig. 1 . 7 consists of the three-address
code sequence:
• There are several points worth noting about three-address instructions. First, each three-
address assignment instruction has at most one operator on the right side. Thus, these
instructions fix the order in which operations are to be done; the multiplication precedes
the addition in the source program. Second, the compiler must generate a temporary
name to hold the value computed by a three-address instruction. Third, some "three-
address instructions" like the first and last in the sequence (1 .3) , above, have fewer than
three operands. 31
Code Optimization
• The machine-independent code-optimization phase attempts to improve
the intermediate code so that better target code will result. Usually better
means faster, but other objectives may be desired, such as shorter code, or
target code that consumes less power.
32
Intermediate Code Generation
• A simple intermediate code generation algorithm followed by code
optimization is a reasonable way to generate good target code.
• The optimizer can deduce that the conversion of 60 from integer to floating
point can be done once and for all at compile time, so the inttofloat
operation can be eliminated by replacing the integer 60 by the floating-
point number 60.0. Moreover, t3 is used only once to transmit its value to
id1 so the optimizer can transform ( 1 .3) into the shorter sequence:
33
Intermediate Code Generation
• There are simple optimizations that significantly improve the running time
of the target program without slowing down compilation too much.
34
Code Generation
• The code generator takes as input an intermediate representation of the source
program and maps it into the target language. If the target language is machine
code, registers Or memory locations are selected for each of the variables used
by the program.
• Then, the intermediate instructions are translated into sequences of machine
instructions that perform the same task.
• A crucial aspect of code generation is the judicious assignment of registers to
hold variables.
35
Code Generation
• For example, using registers R1 and R2, the intermediate code in ( 1 .4)
might get translated into the machine code:
36
Code Generation
The code in ( 1 .5) loads the contents of address id3 into register R2, then
multiplies it with floating-point constant 60.0.
Finally, the value in register Rl is stored into the address of idl , so the code
correctly implements the assignment statement ( 1 . 1) .
37
Symbol-Table Management
• An essential function of a compiler is to record the variable names used in
the source program and collect information about various attributes of
each name.
• These attributes may provide information about the storage allocated for a
name, its type, its scope (where in the program its value may be used), and
in the case of procedure names, such things as the number and types of its
arguments, the method of passing each argument (for example, by value or
by reference), and the type returned.
38
Symbol-Table Management
39
Compiler- Construction Tools
40
Compiler- Construction Tools
4. Code-generator generators that produce a code generator from a collection of
rules for translating each operation of the intermediate language into the
machine language for a target machine.
5. Data-flow analysis engines that facilitate the gathering of information about how
values are transmitted from one part of a program to each other part. Data-flow
analysis is a key part of code optimization.
6. Compiler- construction toolkits that provide an integrated set of routines for
constructing various phases of a compiler.
41
Program Translations
42
Type Checking
• Type checking is an effective and well-established technique to catch
inconsistencies in programs.
• It can be used to catch errors, for example, where an operation is applied
to the wrong type of object, or if parameters passed to a procedure do not
match the signature of the procedure.
• Program analysis can go beyond finding type errors by analysing the flow
of data through a program.
• For example, if a pointer is assigned null and then immediately
dereferenced, the program is clearly in error.
43
Bounds Checking
• Because C does not have array bounds checks, it is up to the user to ensure
that the arrays are not accessed out of bounds. Failing to check that the
data supplied by the user can overflow a buffer, the program may be
tricked into storing user data outside of the buffer.
44
Environments and States
45
Environments and States
46
Environments and States
47