Compilation Stages - New
Compilation Stages - New
•describe program compilation stages: lexical and syntactic analysis, code generation and
optimization.
•demonstrate understanding of the program compilation stages: lexical, syntactic
analysis.
•demonstrate understanding of the program compilation stage: code generation.
•demonstrate understanding of code optimization as the program compilation stage.
Assessme
nt criteria:
Keywords Absolute, and, array, asm, begin, case, const, constructor, destructor, div, do,
(Pascal) downto, else, end, file, for, function, goto, if, implementation, in, inherited, inline,
interface, label, mod, nil, not, object, of, operator, or, packed, procedure,
program, record, reintroduce, repeat, self, set, shl, shr, string, then, to, type, unit,
until, uses, var, while, with, xor.
What a compiler
is?
Compiler
When a programmer uses a computer language (high level) to write a program the
statements are called source code.
The compiler translates source code into machine code (low level).
The code that is compiled is stored as an executable file also called object file. When
the file runs the machine code is processed by the CPU.
Advantages and disadvantages
Disadvantages of
Advantages of compilers:
compilers:
1. Source code is not 1. Object code needs to be
included, therefore compiled produced before a final
code is more secure than executable file, this can be a
interpreted code. slow process.
2. Tends to produce faster 2. The source code must be
code than interpreting 100% correct for the
source code. executable file to be
3. Produces an executable produced.
file, and therefore the
program can be run without
need of the source code.
The steps involved print(“Hell
in compilation: o world!”)
Syntactic Analysis
Code generation
Object File
Question:
Why would a company not
want to distribute source
code when they sell a
software package?
time = 5 + 3;
time = 5 + 3;
The first thing the computer must do is
categorize the characters into ‘Tokens’.
3 Lexical analysis
The compiler looks at the incoming stream of characters and
tries to decide where one ‘thing’ ends and another ‘thing’
begins:
time = 5 + 3;
The compiler sees the word time. The word time is a basic unit
& is called a Lexeme.
time = 5+3;
2 Syntax Analysis(Parsing)
time = 5+3;
What could
go wrong?
3 Syntax Analysis(Parsing)
Semantic analysis is the task of ensuring that the declarations
and statements of a program are semantically correct, i.e, that
their meaning is clear and consistent with the way in which
control structures and data types are supposed to be used.
#include <iostream>
#include <string>
using namespace std;
int main() {
string time;
return 0;
}
Lexical analysis
Code generation
Creating a sequence of instructions and the order
of the execution of the instructions. Converting
source code (via the output of lexical and
syntactic analysis) into machine code.
Code generation
In computing, code generation is the process by which a
compiler's code generator converts some intermediate
representation of source code into a form (e.g., machine
code) that can be readily executed by a machine.
This involves:
Stops at the first error during execution, Identifies all errors during the
Error Handling making it easier to find and fix errors on compilation process, but can be harder to
the spot. debug due to less context.