CSC 437 Chapter 1
CSC 437 Chapter 1
Basic Concepts
1
Computer system
2
Application Level
Assembly Level
Machine Level
Micro-Programming Level
3
Logic Gate Level
Introduction
4
CSC 437 (Compilers) 2/28
Definition of Program
Definition: A program is a set of instructions which is
developed by a programmer using any of the computer
programming languages. These languages are human
readable languages.
5
Definition of Machine Language
• Definition: Machine code or machine language is
a set of instructions executed directly by
a computer's central processing unit (CPU).
• Computers can only understand machine language
- a language of numbers that is impractical, if not
impossible, for most humans to understand.
• Machine language consists of binary digits or bits,
which come in the form of zeros (0) and ones (1).
• Machine language is also called Binary language
6
Language Processors
Computer can only understands instructions in machine
code, i.e. in the form of 0s and 1s.
It is difficult to write computer program directly in machine
code.
Nowadays, the programs are written mostly in high-level
languages, i.e. BASIC, C-H-, PASCAL, etc. A program
written in any high-level programming language (or written
in assembly language) is called the Source Program.
7
Language Processors
The source code cannot be executed directly by the
computer.
The source program must be converted into machine code to
run it on the computer.
The program translated into machine code is known as
Object Program.
Every language has its own language processor (or translator).
Therefore, language processor is defined as: The
special translator system software that is used to translate the
program written in high-level language (or Assembly
language) into machine code is called language processor or
translator program.
8
Language Processors
The language processors are divided into three types.
Compilers
Interpreters
Assemblers
9
Language Processors
1. Compiler: The language processor that translates the
complete source program as a whole in machine code before
execution is called compiler. The C and C++ compilers are
best examples of compilers.
The program translated into machine code is called the object
program.
The source code is translated to object code successfully if it
is free of errors.
If there are any errors in the source code, the compiler
specifies the errors at the end of compilation.
The errors must be removed before the compiler can
10
successfully compile the source code.
Language Processors
2. Interpreter
The language processor that translates (converts) each
statement of source program into machine code and executes
it immediately before to translate the next statement is called
Interpreter.
If there is an error in the statement the interpreter
terminates its translating process at that statement and
displays an error message. The OWBASIC is an example of
interpreter.
11
Language Processors
2. Interpreter
12
Language Processors
3. Assembler
An assembler is third type of translator program. It is used to
translate the program written in Assembly language into
machine code. An assembler performs the translation process
in similar way as compiler. But assembler is the translator
program for low-level programming language, while a
compiler is the translator program for high-level
programming languages.
13
Language Processors — continued
14
6/28
Interpreter:
An interpreter is a program which translates statements of a program into
machine code. It translates only one statement of the program at a time.
Difference between Interpreter & Compiler :
Interpreter reads only one statement of program, translates it and executes it.
Then it reads the next statement of the program again translates it and executes
it. In this way it proceeds further till all the statements are translated and
executed.
On the other hand, a compiler goes through the entire program and then
translates the entire program into machine codes. A compiler is 5 to 25 times
faster than an interpreter.
By the compiler, the machine codes are saved permanently for future reference.
On the other hand, the machine codes produced by interpreter are not saved.
An interpreter is a small program as compared to compiler. It occupies less
memory space, so it can be used in a smaller system which has limited memory
space.
Difference between Interpreter & Compiler :
16
Difference between Interpreter & Compiler :
17
Introduction to Compiler
• A compiler is software that converts programs from the
high-level language into machine language.
18
Importance of Compiler
For performing different task on the computer, a compiler is
communication medium that translate source language
into target language.
Compiler allows the user to perform customized task on
machine
it allows us to communicate with hardware.
It is also use to cover the "GAP" between Humans and the
computer language.
19
Advantage of Compiler
When there is only Machine Language then programmers write
their compilers in this language. But it is very difficult and tedious
job.
The role of compiler is take source code written in high level
language (Java, C++, VB.Net etc).
The High Level Languages are easily understood by humans. So
compiler converts the program written in formal language (Source
language) into machine language (target language). As we know
that computers can easily understand machine language.
here are different programs related to compiler that works before
compilation such as editor, preprocessor, assembler, linker or
loader, debugger and profiler.
20
Error & diagnostic
message
22
Example of Hybrid Compiler
24
8/28
Language Processors — continued
25
. CSC 347 (Compilers) 9/28
Language Processors — continued
• A macro or "macro-instruction"
in computer science is a
rule or pattern that specifies how a
certain input sequence (often a
sequence of characters) should be
mapped to a replacement input
sequence (also often a sequence of
characters) according to a defined
procedure.
• The mapping process that instantiates
(transforms) a macro use into a specific
sequence is known as macro expansion
27
. CSC 347 (Compilers) 9/28
Language Processors — continued
28
. CSC 347 (Compilers) 9/28
Linker & Loader
Linker: In high level languages, some built in header files or libraries
are stored. These libraries are predefined and these contain basic
functions which are essential for executing the program. These functions
are linked to the libraries by a program called Linker. If linker does not
find a library of a function then it informs to compiler and then compiler
generates an error.
32
. CSC 347 (Compilers) 10 / 28
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.
33
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.
34
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.
In practice, several phases may
be grouped together, and the
intermediate representations
between the grouped phases
need not be constructed
explicitly.
35
The symbol table, which stores
information about the entire
source program, is used by all
phases of the compiler.
36
Some compilers have a
machine-independent
optimization phase between the
front end and the back end.
The purpose of this optimization
phase is to perform
transformations on the
intermediate representation, so
that the back end can produce a
better target program than it
would have otherwise produced
from an unoptimized intermediate
representation.
Since optimization is optional,
one or the other of the two
optimization phases shown may
37 be missing.
LexicalAnalysis
38
. CSC 347 (Compilers) 12 / 28
LexicalAnalysis — continued
39
. CSC 347 (Compilers) 13 / 28
LexicalAnalysis — continued
40
. CSC 347 (Compilers) 14 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
41
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
42
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
43
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
44
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
45
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
46
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
47
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
〈token-name, attribute-value〉
position = initial + rate ∗ 60
48
. CSC 347 (Compilers) 15 / 28
LexicalAnalysis — continued
49
. CSC 347 (Compilers) 16 / 28
SyntaxAnalysis
50
. CSC 347 (Compilers) 17 / 28
SyntaxAnalysis
51
. CSC 347 (Compilers) 17 / 28
SyntaxAnalysis
52
. CSC 347 (Compilers) 17 / 28
SyntaxAnalysis
53
. CSC 347 (Compilers) 17 / 28
SyntaxAnalysis
54
. CSC 347 (Compilers) 17 / 28
SyntaxAnalysis
55
. CSC 347 (Compilers) 17 / 28
SyntaxAnalysis
56
. CSC 347 (Compilers) 17 / 28
SemanticAnalysis
57
. CSC 347 (Compilers) 18 / 28
SemanticAnalysis
58
. CSC 347 (Compilers) 18 / 28
SemanticAnalysis
59
. CSC 347 (Compilers) 18 / 28
SemanticAnalysis
60
. CSC 347 (Compilers) 18 / 28
SemanticAnalysis
61
. CSC 347 (Compilers) 18 / 28
Intermediate Code Generation
62
. CSC 347 (Compilers) 19 / 28
Intermediate Code Generation
63
. CSC 347 (Compilers) 19 / 28
Intermediate Code Generation
64
. CSC 347 (Compilers) 19 / 28
Intermediate Code Generation
t1 = inttofloat(60)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3
65
. CSC 347 (Compilers) 19 / 28
Intermediate Code Generation
66
. CSC 347 (Compilers) 19 / 28
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.
67
. CSC 347 (Compilers) 20 / 28
Code Optimization
68
. CSC 347 (Compilers) 20 / 28
Code Optimization
69
. CSC 347 (Compilers) 20 / 28
Code Optimization
70
. CSC 347 (Compilers) 20 / 28
Code Generation
72
. CSC 347 (Compilers) 21 / 28
Symbol-T
able Management
73
. CSC 347 (Compilers) 22 / 28
Symbol-T
able Management
74
. CSC 347 (Compilers) 22 / 28
Symbol-T
able Management
75
. CSC 347 (Compilers) 22 / 28
The Grouping of Phases into Passes
76
. CSC 347 (Compilers) 23 / 28
The Grouping of Phases into Passes — continued
77
. CSC 347 (Compilers) 24 / 28
Compiler-Construction T
ools
78
. CSC 347 (Compilers) 25 / 28
Compiler-Construction T
ools — continued
79
. CSC 347 (Compilers) 26 / 28
Compiler-Construction T
ools — continued
80
. CSC 347 (Compilers) 27 / 28
Compiler-Construction T
ools — continued
81
. CSC 347 (Compilers) 27 / 28
End of
82
Slides
. CSC 347 (Compilers) 28 / 28