CS4031 Compiler Construction Lecture 1
CS4031 Compiler Construction Lecture 1
Compiler Construction
Lecture 1
Mahzaib Younas
Lecturer, Department of Computer Science
FAST NUCES CFD
Outlines
1. Compilers and Interpreters
2. The structure of a compiler
3. Why learn about compilers?
4. The Evolution of Programming Language
5. Summary
Compiler
• “Compilation”
• Translation of a program written in a source language into a
semantically equivalent program written in a target language
Definition
• A compiler is a program translates (or compiles) a program written in
a high-level programming language (the source language) that is
suitable for human programmers into the low-level machine language
(target language) that is required by computers.
• During this process, the compiler will also attempt to spot and report
obvious programmer mistakes that detect during the translation
process.
Why we use High Level Language for
Programming?
Using a high-level language for programming has a large impact on
how fast programs can be developed. The main reasons for this are:
1. Compared to machine language, the notation used by programming
languages is closer to the way humans think about problems.
2. The compiler can spot some obvious programming mistakes.
3. Programs written in a high-level language tend to be shorter than
equivalent programs written in machine language.
4. The same program can be compiled to many different machine
languages and, hence, be brought to run on many different machines.
Interpreter
• Performing the operations implied by the source program
Compiler vs. Interpreter
Compiler Interpreter
Takes Entire program as input Take single instruction as input
It is Faster It is Slower
intermediate object code is generated. No intermediate code is generated
Required more memory Due to intermediate object Required less memory As no intermediate code is
code generated
Program not need compile every time Every time higher level program is converted into
lower level program.
Errors are displayed after entire program is checked. Errors are displayed for every instruction
interpreted.
Debugging is comparatively hard. Debugging is easy.
A compiler is translator that convert the high level language into the
machine level language,.
Assembler
• The third stage is the assembly stage, this stage is to convert the
assembly code generated in the second stage into our executable file,
which is to convert our assembly language into a machine language
that can be executed by our machine.
• Remove comments
• Remove white spaces
• Also known as Scanner or tokenizers
Example
• A simple statement of C or C++ code
sum = 2 + c;
• Create the token of above line
sum = 2 + C
identifier Assignment Constant operator Identifier
operator
Syntax Analysis (Parser)
• The second phase of a compiler is syntax analysis, also known as
parsing. This phase takes the stream of tokens generated by the lexical
analysis phase and checks whether they conform to the grammar of the
programming language. The output of this phase is usually an Abstract
Syntax Tree.
Identifier Expression
2 * Identifier
Semantic Analysis
In semantic analysis, compiler check the logical type error