Lecture 1- Introduction to compilers (1)
Lecture 1- Introduction to compilers (1)
construction
Compiler construction
Introduction
• To bridge the gap between human-readable code and
machine-executable instructions, there is need of a
software that converts source code written in high
programming language into machine code or an
intermediate code that can be executed by a
computer.
• This software is referred to as a translator, it performs
the function of converting code from one language to
another language.
• A translator is a program that takes one form of
program as the input and converts it into another
form.
• The input program is called source language and the
output program is
• called target language
• The rationale behind translators is to provide a
general-purpose solution for converting code from one
Compiler construction
Types of translators
• There are four types of translators :
• Interpreter is a translator that executes code written
in a high programming language directly, translating it
into machine code line by line or statement by
statement at runtime.
Compiler construction
Compiler
Compiler
• A Compiler is an essential part of the system software
stack.
• Its job consists in translating programs from a high-
level programming language like C or Java into a
sequence of machine instructions of an actual
processor.
• Compilers are comparatively complex programs.
• Their construction involves ideas and approaches from
many different areas of computer science e.g,
Programming Languages, Automata Theory, Data
Structures, Algorithms, Software Engineering,
Operating Systems, Computer Architecture)
Compiler construction
Compiler
• A compiler is a program that reads a program written in one
language, the source language, and translates it into an
equivalent program in another language, the target language.
The translation process should also report the presence of
errors in the source program.
• Features of Compiler
• Correctness
• Speed of compilation
• Preserve the correct the meaning of the code
• The speed of the target code
• Recognize legal and illegal program constructs
• Good error reporting/handling
• Code debugging help
Compiler construction
History of Compiler
Compiler construction
Types of Compiler
4. One pass compiler Cont……..
• The compilation is done in one pass over the
source program, hence the compilation is
completed very quickly.
• This is used for the programming language PASCAL,
COBOL, FORTAN.
Compiler construction
Steps for Language processing
systems
• Assembler – It translates assembly language code
into machine understandable language. The output
result of assembler is known as an object file which is
a combination of machine instruction as well as the
data required to store these instructions in memory.
• Linker – The linker helps you to link and merge
various object files to create an executable file. All
these files might have been compiled with separate
assemblers. The main task of a linker is to search for
called modules in a program and to find out the
memory location where all modules are stored.
• Loader - The loader is a part of the OS, which
performs the tasks of loading executable files into
memory and run them. It also calculates the size of a
program which creates additional memory space.
Compiler construction
Overview Of Language Processing
System ….
• Preprocessor – collects all the separate files to the source
program.
• A preprocessor is a program that processes its input
data to produce output that is used as input to
another program.
Compiler construction
Overview Of Language Processing
Assembler
System ….
• Assembler is a translator which takes assembly code
and generates equivalent machine code.
Compiler construction
Overview Of Language Processing
• Assembler
System ….
• Programmers found it difficult to write or read
programs in machine language.
• They began to use a mnemonic (symbols) for each
machine instruction, which they would
subsequently translate into machine language.
• Such a mnemonic machine language is now called
an assembly language.
• Programs known as assembler were written to
automate the translation of assembly language into
machine language.
• The Assembler is responsible for translating the target
code–usually assembly code–into an executable machine
code.
• The assembly code is a mnemonic version of machine
code in which:
1. Names are used instead of binary codes for operations
Compiler construction
(Code Table).
Overview Of Language Processing
System
• Linker is a computer ….
program that links and
merges various object files together in order to
make an executable file.
Compiler construction
Loader and Linker
• The machine code generated by the Assembler can be executed only if
allocated in Main Memory starting from the address “0”.
• So Loader will alter the re-locatable addresses of the code to place both
instructions and data in the right place in Main Memory.
Compiler construction
Phases of Compiler
• The analysis and synthesis phases of a compiler are
crucial for transforming high-level source code into
executable machine code.
Compiler construction
Phases of Compiler
• The analysis and synthesis phases of a compiler are
crucial for transforming high-level source code into
executable machine code.
Compiler construction
Phases of Compiler
• A compiler operates in phases. A phase is a logically
interrelated operation that takes source program in
one representation and produces output in another
representation. There are two major parts of a
compiler: Analysis and Synthesis
1. Analysis Phase(Machine Independent/Language
Dependent)
• Breaks the source program into constituent
pieces and creates intermediate representation.
An intermediate representation is created from
the given source program.
• Lexical Analyzer, Syntax Analyzer and Semantic
Analyzer are the phases in this part.
2. Synthesis phase(Machine Dependent/Language
independent)
• In synthesis phase, the equivalent
Compiler target program
construction
One Pass Compiler vs Multi-pass
• Compiler
The pass structure of a compiler refers to how the
compilation process is organized into distinct stages
or "passes." Each pass performs specific tasks, and
the results from one pass are often used as input for
the next.
One-pass compiler
• is a compiler that passes through the parts of each
compilation unit only once, immediately translating
each part into its final machine code.(processes the
source code in one pass, performing all necessary
tasks in a linear fashion.)
Characteristics
• Faster compilation time due to a single traversal
of the source code.
• Limited ability to perform complex optimizations
since the entire code structure isCompiler
not fully analyzed.
construction
One Pass Compiler vs Multi-pass
Compiler
Multi-pass compiler
• A compiler that converts the program into one or more
intermediate representations in steps between source
code and machine code, and which reprocesses the
entire compilation unit in each sequential pass.
• It processes the source code in multiple passes, allowing
for more thorough analysis and optimization.
• Characteristics:
• Each pass has a specific focus (e.g., lexical analysis,
syntax analysis, semantic analysis, optimization).
• Intermediate representations may be generated and
refined over multiple passes.
• Passes:
• First Pass: Lexical analysis and syntax analysis.
• Second Pass: Semantic analysis and intermediate code
generation.
• Subsequent Passes: Code optimization and final code
Compiler construction
Phases of a Compiler
• .
Compiler construction
Phases of a Compiler
• The compilation process is a sequence of various phases.
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. They
communicate with error handlers and the symbol table.
• Below are the phases of a compiler.
• 1. Lexical Analysis
• 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 lexemes.
Lexical analyzer represents these lexemes in the form
of tokens as:
Compiler construction