Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Lecture 1- Introduction to compilers (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 1- Introduction to compilers (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Introduction to compiler

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 is a translator that translates a high-level


language program such as FORTRAN, PASCAL, C++, to
low-level language program such as an assembly
language or machine language.

• Assembler - An Assembler is a translator that


translates the assembly language program (mnemonic
program) to machine language program.

• Pre-processor: is a translator Translate program


written by high level language Into an construction
Compiler equivalent
Why translators

• If there are no translators, then we must program in


machine language.
• With machine language we must be able to
communicate directly with a computer in terms of bits,
registers, and very primitive machine operations.
• Since a machine language program is nothing more
than a sequence of 0's and 1's, programming a
complex

• Translators bridge the gap between human-readable


code and machine-readable instructions, facilitating
the development of software that is efficient, portable,
and easier to maintain.
• Translators are essential for converting code written
by developers into a form that computers can
understand and execute. Compiler construction
Preprocessor
Preprocessor
• A preprocessor produce input to compilers. They
may perform the following functions.
• Macro processing: A preprocessor may allow a
user to define macros that are short hands for
longer constructs.
• File inclusion: A preprocessor may include
header files into the program text.
• Rational preprocessor: these preprocessors
augment older languages with more modern
flow-of-control and data structuring facilities.
• Language Extensions: These preprocessor
attempts to add capabilities to the language
by certain amounts to build-in macro
Compiler construction
Interpreter
Interpreter is a program that reads a program written in a
source language and translates it into an equivalent program
in target language line by line.

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)

• It plays a crucial role in software development by


enabling high-level language programming while
Compiler construction
Compiler
• Compiler
• A program that 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.
• A compiler is an application that can convert programs
written in higher level language to lower-level language
that the computer can understand and execute
• Compilation - Translation of a program written in a source
language into a semantically equivalent program written
in a target language.

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.

• The program written in higher level language is called the


source program/source code, while the output of the compiler
that can be executed is called target program/machine code.
Compiler construction
Compiler vs Interpreter
Interpreter Compiler

Translates program one statement Scans the entire program and


at a time. translates it as a whole into
machine code.

It takes less amount of time to It takes large amount of time to


analyze the source code but the analyze the source code but the
overall execution time is slower. overall execution time is
comparatively faster.

No intermediate object code is Generates intermediate object


generated, hence are memory code which further requires
efficient. linking, hence requires more
memory.

Continues translating the program It generates the error message


until the first error is met, in only after scanning the whole
which case it stops. Hence program. Hence debugging is
debugging is easy. comparatively hard.
Compiler construction
Native compiler vs cross compiler
A Native-compiler
• A compiler that runs on a computer platform and
produces code for that same computer platform.
• Native compilers generate code that is optimized
for a specific CPU architecture (e.g., x86, ARM).
• This means the compiled code can run directly on the
hardware without the need for an interpreter or virtual
machine.
Cross-compiler
• A compiler that generates executable code for a
platform different from the one on which the compiler
is running.
• A compiler that runs on one computer platform and
produces code for another computer platform.
• Produces code for various architectures (e.g., x86,
ARM, MIPS) while running on a different architecture.
Compiler construction
Native compiler vs cross compiler
Cross-compiler
• Cross-compilers are often used in embedded systems
development, where the target environment (like an
embedded device) has limited resources or different
architecture compared to the development
environment (like a PC).
• They are also useful in scenarios where the target
platform is not easily accessible for development and
testing.
Advantages of cross compiler
• Efficiency: You can develop on a powerful machine
and deploy to a less powerful target.
• Access to Tools: Development tools and libraries
available on the host system can be utilized.
• Faster Development Cycle: You can compile and test
code without needing to transfer it to the target
Compiler construction
Compilers and high-level languages

• A compiler is a translator that converts the high-level


language into the machine language.
• High-level language is written by a developer and
machine language is a language understood by the
processor.
• Compiler is used to show errors to the programmer.
• The main purpose of compiler is to change the code
written in one language without changing the meaning of
the program.
• When you execute a program which is written in HLL
programming language then it executes into two parts.
• In the first part, the source program compiled and
translated into the object program (low level
language).
• In the second part, object program translated into
the target program through the assembler.
Compiler construction
High-level language programming
languages
• Using a high-level language for programming has a
large impact on how fast programs can be
developed. The main reasons for this are:
• Compared to machine language, the notation used by
programming languages is closer to the way humans
think about problems.

• The compiler can spot some obvious programming


mistakes.

• Programs written in a high-level language tend to be


shorter than equivalent programs written in machine
language.

• The same program can be compiled to many different


machine languages and, hence, Compiler
be brought to run on
construction
Features of a compiler

• 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

• Important Landmark of Compiler’s history is as


follows:
• The “compiler” word was first used in the early
1950s by Grace Murray Hopper.
• The first compiler was build by John Backum
and his group between 1954 and 1957 at IBM.
• COBOL was the first programming language
which was compiled on multiple platforms in
1960.
• The study of the scanning and parsing issues
Compiler construction
Types of Compilers
1. Native code compiler
• A compiler may produce binary output to run
/execute on the same computer and operating
system.
• This type of compiler is called as native code
compiler.
2. Cross Compiler
• A cross compiler is a compiler that runs on one
machine and produce object code for another
machine.

3. Bootstrap 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.

5. Multi-pass compiler(2 or 3 pass compiler)


• In this compiler , the compilation is done step by
step .
• Each step uses the result of the previous step and it
creates another intermediate result.
Compiler construction
Summary
• In computing, a compiler is a computer program
that translates computer code written in one
programming language (the source language) into
another language (the target language).

• The name "compiler" is primarily used for programs


that translate source code from a high-level
programming language to a low-level programming
language (e.g. assembly language, object code, or
machine code) to create an executable program

• There are many different types of compilers which


produce output in different useful forms. A cross-
compiler produces code for a different CPU or
operating system than the one on which the cross-
compiler itself runs. A bootstrap compiler is often a
temporary compiler, used for compiling a more
Compiler construction
Summary
• Related software include decompilers, programs
that translate from low-level languages to higher
level ones; programs that translate between high-
level languages, usually called source-to-source
compilers or transpilers; language rewriters, usually
programs that translate the form of expressions
without a change of language; and compiler-
compilers, compilers that produce compilers (or
parts of them), often in a generic and reusable way
so as to be able to produce many differing
compilers.

• A compiler is likely to perform some or all of the


following operations, often called phases:
preprocessing, lexical analysis, parsing, semantic
analysis (syntax-directed translation), conversion of
input programs to an intermediate representation,
Compiler construction
Summary
• Compilers generally implement these phases as
modular components, promoting efficient design
and correctness of transformations of source input
to target output.
• Program faults caused by incorrect compiler
behavior can be very difficult to track down and
work around; therefore, compiler implementers
invest significant effort to ensure compiler
correctness.

• Compilers are not the only language processor used


to transform source programs. An interpreter is
computer software that transforms and then
executes the indicated operations.

• The translation process influences the design of


computer languages, which leads to a preference of
Compiler construction
Some of the list of compliers are :
1. » Ada compiler
2. » ALGOL compiler
3. » BASIC compiler
4. » C# compiler
5. » C compiler
6. » C++ compiler
7. » COBOL compiler
8. » Smalltalk compiler
9. » Java 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.

5. Multi-pass compiler(2 or 3 pass compiler)


• In this compiler , the compilation is done step by
step .
• Each step uses the result of the previous step and it
creates another intermediate result.
Compiler construction
Steps for Language processing
systems
• Before knowing about the
concept of compilers, you first
need to understand a few other
tools which work with
compilers.
• Pre-Processor – The pre-
processor removes all the
#include directives by including
the files called file inclusion and
all the #define directives using
macro expansion. It performs
file inclusion, augmentation,
macro-processing etc.

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.

• The output is said to be a preprocessed form of the


input data, which is often used by some subsequent
programs like compilers.

• The preprocessor is executed before the actual


compilation of code begins, therefore the preprocessor
digests all these directives before any code is
generated by the statements.

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.

• All these files might have been compiled by


separate assemblers.

• The major task of a linker is to search and locate


referenced module/routines in a program and to
determine the memory location where these
codes will be loaded, making the program
instruction to have absolute references.

• Loader is a part of operating system and is


responsible for loading executable files into
memory and execute them. It calculates the size
of a program (instructions and data) and creates
Compiler construction
Loader and Linker
• Role of Loader and Linker

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.

• The starting free address, L, in Main Memory to allocate the program is


called the Relocation Factor. The Loader must add to each re-locatable
address the relocation factor L;

• The Linker links together the different files/modules of a single program


and, possibly, adds library files.

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:

• The source code is scanned to convert it into tokens,


which are the basic building blocks of the
Compiler language
construction
Phases of a Compiler
2. Syntax Analysis
• The next phase is called the syntax analysis or parsing.
It takes the token produced by lexical analysis as input
and generates a parse tree (or syntax tree) as output. 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.
• This phase checks the tokens against the grammatical
rules of the programming language to form a parse tree
or abstract syntax tree (AST).
3. Semantic Analysis
• Semantic analysis checks whether the parse tree constructed
follows the rules of language e.g , 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
Compiler construction
Phases of a Compiler
4. Intermediate Code Generation
• 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.
5. Code Optimization
• The next phase does code optimization of the
intermediate code.
• It is optional phase of compiler.
• Optimization can be assumed as something that
removes unnecessary code lines, and arranges the
sequence of statements in order to speed up the
program execution without wastingCompiler
resources (CPU,
construction
Phases of a Compiler
6. Code Generation
• In this phase, the 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.

• The optimized intermediate code is


translated into the target machine
Compiler construction
Symbol table & Error handler
7. 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.
8. Error Handler
• It is invoked when an error in the source program is
detected.

Compiler construction

You might also like