0% found this document useful (0 votes)
4 views

1-Phases of compiler

The document is a lecture on Compiler Construction by Muhammad Fawad Nasim, outlining the course structure, prerequisites, and key concepts related to compilers, including the differences between compilers and interpreters, phases of compilation, and tools used in compiler design. It emphasizes the importance of compilers in translating high-level programming languages to machine code and includes assignments for students to analyze and implement various programming concepts. The lecture also discusses the roles of linkers, loaders, and the semantic analysis process in compiler functionality.

Uploaded by

Abu Bakr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1-Phases of compiler

The document is a lecture on Compiler Construction by Muhammad Fawad Nasim, outlining the course structure, prerequisites, and key concepts related to compilers, including the differences between compilers and interpreters, phases of compilation, and tools used in compiler design. It emphasizes the importance of compilers in translating high-level programming languages to machine code and includes assignments for students to analyze and implement various programming concepts. The lecture also discusses the roles of linkers, loaders, and the semantic analysis process in compiler functionality.

Uploaded by

Abu Bakr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

COMPILER CONSTRUCTION

LECTURE 1

Muhammad Fawad Nasim


[email protected]
What do you think
about CC
 It is an easy subject.
 It is a tough subject.
 Not sure
Marks Distribution
 Final Project 10 [Individual]
 Quiz 10
 Assignments 30 [Individual]
 Mid 20
 Final 30
Pre requisite
 Programming Fundamentals
 Object Oriented Programming
 Data Structure and Algorithms
 Analysis Of Algorithms
 Computer System Architecture
 Theory Of Automata
Compilers
 The software systems that do the translation from
Human readable to machine readables are called
compilers.
First Compiler
Compile
r
c es
odu
t pr
tha
bler
sem
as
ram
r og
a p
by t.
sed utpu
ces s o
o it
pr as
e is de
u ag e co
g n
lan chi
bly a
em le m
s
s ab
e a t
c a
elo
How Compiler Works
Suppose you coded a calculator in c++
Compiler compiled it and converted to executable machine code.
Once in executable machine code, you can run it any time.
Interpre
ter
Guess what is the difference?
Compiler VS Interpreter
 Compiler is offline
 Interpreter is online
 Compiler translate the whole program and saves into an other
language , usually in executable machine code
 Interpreter does it live, executes line by line and takes input
and output during execution
In short,
 Compiler makes a permanent .exe file

 Interpreter does not make any such file


Who
is
this?
What if there is a person who translate all the
conversation in Punjabi and record it on some media or
upload on sound cloud?
This way we can listen to the conversation in our
language any time.
 So online or live is __________________

 And offline or recorded is _________________


The execution of java application, a
hybrid approach
 A Java source program first compiled into
an intermediate form called bytecodes.
 The bytecodes are then interpreted by a
virtual machine.
Java Hybrid Model
 bytecodes compiled on one machine can be interpreted on
another machine, perhaps across a network.
 some Java compilers, called just-in-time (JIT) compilers,
translate the bytecodes into machine language immediately
before they run the intermediate program to process the input.
Java Model
Preprocessor
 A source program may be divided into modules stored in
separate files.
 The task of collecting the source program is sometimes
entrusted to a separate program, called a preprocessor.
Linkers and Loaders
 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.
LINKER VS LOADER
1 The main function of Linker is to generate executable files. Whereas main objective of Loader is to load
executable files to main memory.

2 The linker takes input of object code generated by compiler/assembler. And the loader takes input of
executable files generated by linker.

3 Linking can be defined as process of combining various pieces of codes and source code to obtain
executable code. Loading can be defined as process of loading executable codes to main memory for
further execution.

4 Linkers are of 2 types: Linkage Editor and Dynamic Linker.Loaders are of 4 types: Absolute, Relocating,
Direct Linking, Bootstrap.

5 Another use of linker is to combine all object modules. It helps in allocating the address to executable
codes/files.

6 Linker is also responsible for arranging objects in program’s address space. Loader is also responsible for
adjusting references which are used within the program
Source-to-source
translator/Compiler
 A compiler that translates a high-level language into
another high-level language is called a source-to-
source translator.

 Closure compiler, CoffeeScript, Dart, Haxe, TypeScript


Task
 Prepare 5 Groups present on following topics

 Closurecompiler, CoffeeScript, Dart, Haxe, TypeScript

Deadline = Next Lecture


Compiler
 A compiler translates the code written in one language
to some other language without changing the meaning
of the program.
 It is also expected that a compiler should make the
target code efficient and optimized in terms of time
and space.
Previous revision
 What is this?

input ? output
Previous revision
 What is this?

input ? .Exe output


Cross-compiler
 A compiler that runs on platform (A) and is capable of
generating executable code for platform (B) is called a
cross-compiler.
 Till now the compiler is a
Let’s open the Box.

• Analysis part

• Synthesis part.
Phases Of compiler
 Each phase takes
input from its previous
stage, has its own
representation of
source program, and
feeds its output to the
next phase of the
compiler.
The first phase of scanner works as a text
scanner. This phase scans the source
code as a stream of characters and
converts it into meaningful sequence
called lexemes. For each lexeme,Lexical
analyzer represents these lexemes in the
form of tokens as:
<token-name, attribute-value>

and pass to next phase


Token
 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.
 Information from the symbol-table entry is needed
for semantic analysis and code generation.
Lexical Analyzer
 Blanks separating the lexemes would be discarded by
the lexical analyzer .
Tools
 Scanner generators that produce lexical analyzers
from a regular-expression description of the tokens of
a language.
 flex is a tool for generating scanners: flex reads the
given input files, or its standard input if no file names
are given, for a description
Parser takes the token produced by lexical
analysis as input and generates a parse tree (or
syntax tree). In this phase, token arrangements
are checked against the source code grammar,
i.e. the parser checks if the expression made by
the tokens is syntactically correct.
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

This tree shows the order in which the operations must execute
The tree has an interior node labeled *
with (id, 3) as its left child and the
integer 60 as its right child. The node
(id,3) represents the identifier r a t e .
The node labeled * makes it explicit
that we must first multiply the value of
rate by 60

The node labeled + indicates that we must add the result of this multiplication to the value of i n i t i a l .

The subsequent phases of the compiler use the grammatical structure to help analyze the source program
and generate the target program.
Tools
 Parser generators that automatically produce syntax
analyzers from a grammatical description of a
programming language
Semantic analysis checks whether the parse tree
constructed follows the rules of language. For
example, type checking,assignment of values is
between compatible data types, and adding string to
an integer. Also, the semantic analyzer keeps track of
identifiers, their types and expressions; whether
identifiers are declared before use or not etc. The
semantic analyzer produces an annotated syntax tree
as an output.
Coercion
 The language specification may permit some
type conversions
 Suppose that position, i n i t i a l , and r a t e
have been declared to be floating-point
numbers, and that the lexeme 60 by itself
forms an integer. The type checker in the
semantic analyzer discovers that the operator *
is applied to a floating-point number r a t e and
an integer 60. In this case, the integer may be
converted into a floating-point number.The
output of the semantic analyzer has an
 extra node for the operator inttofloat, which
explicitly converts its integer argument into a
floating-point number.
After semantic analysis the compiler generates an
intermediate code of the source code for the target
machine. It represents a program for some abstract
machine. It is in between the high-level language and
the machine language. This intermediate code should
be generated in such a way that it makes it easier to
be translated into the target machine code
The machine-independent code-optimization phase
attempts to improve the intermediate code so that better
target code will result. Optimization removes unnecessary
code lines, and arranges the sequence of statements in
order to speed up the program execution without wasting
resources (CPU, memory).
code generator takes the optimized representation of
the intermediate code and maps it to the target
machine language. The code generator translates the
intermediate code into a sequence of (generally) re-
locatable machine code. Sequence of instructions of
machine code performs the task as the intermediate
code would do.
KeyNote:
 The organization of storage at run-time depends on the
language being compiled.
 Storage-allocation decisions are made either during
intermediate code generation or during code generation.
Functional Division
1. Analysis Phase
2. Synthesis Phase
1-Analysis Phase
 Reads the source program, divides it into core parts
and then checks for lexical, grammar and syntax
errors.
 The analysis phase generates
1. intermediate representation of the source program
2. Symbol table
which should be fed to the Synthesis phase as input.
Symbol Table
 It is a data-structure maintained throughout all the
phases of a compiler.
 All the identifier's names along with their types are
stored here.
 The symbol table makes it easier for the compiler to
quickly search the identifier record and retrieve it.
 The symbol table is also used for scope management.
Key Note :
 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.
2-Synthesis Phase

 Generates the target program with the help of intermediate


source code representation and symbol table.
The analysis part is often called the front end of the compiler; the synthesis part is the back end .
Compiler Design
 Compiler design covers basic translation mechanism
and error detection & recovery. It includes lexical,
syntax, and semantic analysis as front end, and code
generation and optimization as back-end.
Tools
Phoenix is a compiler-construction toolkit that provides an integrated
framework for building the program analysis, code generation,
and code optimization phases of compilers
Why Study Compilers
 Computers are a balanced mix of software and hardware.
 Hardware is just a piece of mechanical device and its functions are being
controlled by a compatible software.
 Hardware understands instructions in the form of electronic charge, which is
the counterpart of binary language in software programming.
 Binary language has only two alphabets, 0 and 1.
 To instruct, the hardware codes must be written in binary format,in a series of
1s and 0s.
 It would be a difficult task for computer programmers to write such codes,
which is why we have compilers to write such codes
 Compilers are critical in making high-performance computer architectures
effective on users' applications.
Assignments
 Write Java program and analyze its structure
 List of Lexical errors in C++ and Java
 Implement Hash table and remove conflicts
Assignment = Lexical Analyzer

You might also like