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

Rift Valley University Department of Computer Science

The document discusses compilers and their functions. It describes compilers as computer programs that translate source code written in high-level languages into machine language. The compiling process includes lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. It also discusses different types of compilers like single pass, two pass, and multipass compilers and summarizes the typical phases involved in compilation.

Uploaded by

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

Rift Valley University Department of Computer Science

The document discusses compilers and their functions. It describes compilers as computer programs that translate source code written in high-level languages into machine language. The compiling process includes lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. It also discusses different types of compilers like single pass, two pass, and multipass compilers and summarizes the typical phases involved in compilation.

Uploaded by

Girma Marga
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

RIFT VALLEY UNIVERSITY

Department of Computer Science

CoSc483 : Computer Design

Chapter One: Introduction

Fikru Tafesse (Lecturer, MSc. in Computer Science)


[email protected]
[email protected]

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 1


Compiler
• A compiler is a computer program which helps you to transform source
code written in a high-level language into low-level machine language.

• It translates the code written in one programming language to some other


language without changing the meaning of the code.

• The compiler also makes the end code efficient which is optimized for
execution time and memory space.

• The compiling process includes basic translation mechanisms and error


detection.
05/25/2020 Fikru Tafesse(MSc. in Computer Science) 2
… Cont’d

• Compiler process goes through lexical, syntax, and semantic analysis at


the front end, and code generation and optimization at a back-end.

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 3


Features of Compilers
• Correctness
• Speed of compilation
• Preserve the correct meaning of the code
• The speed of the target code
• Recognize legal and illegal program constructs
• Good error reporting/handling
• Code debugging help

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 4


Types of Compiler
1.  Single Pass Compilers
2. Two Pass Compilers
3. Multipass Compilers
1. Single Pass Compilers

• In single pass Compiler, source code directly transforms into machine code.

• For example, Pascal language.

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 5


… Cont’d

2. Two Pass Compilers

• Two pass Compiler is divided into two sections:

1. Front end: It maps legal code into Intermediate Representation (IR).


2. Back end: It maps IR into the target machine

• The Two pass compiler method also simplifies the retargeting process.
• It also allows multiple front ends.
05/25/2020 Fikru Tafesse(MSc. in Computer Science) 6
… Cont’d

3. Multipass Compilers

• The multipass compiler processes the source code or syntax tree of a program
several times.

• It divided a large program into multiple small programs and process them.

• It develops multiple intermediate codes.

• All of these multipass take the output of the previous phase as an input.

• So it requires less memory.

• It is05/25/2020
also known as Wide Compiler.
Fikru Tafesse(MSc. in Computer Science) 7
Tasks of Compiler
• Main tasks performed by the Compiler are:
• Breaks up the source program into pieces and impose grammatical structure on
them
• Allows you to construct the desired target program from the intermediate
representation and also create the symbol table
• Compiles source code and detects errors in it
• Manage storage of all variables and codes.
• Support for separate compilation
• Read, analyze the entire program, and translate to semantically equivalent
• 05/25/2020
Translating the source code into object code depending upon the type of machine
Fikru Tafesse(MSc. in Computer Science) 8
History of Compiler
• Important Landmark of Compiler's history are 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 were pursued in the 1960s and 1970s to
provide a complete solution

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 9


Why use a Compiler?
• Compiler verifies entire program, so there are no syntax or semantic errors

• The executable file is optimized by the compiler, so it is executes faster

• Allows you to create internal structure in memory

• Platform independent

• Translate entire program in other language

• Generate files on disk

• Link the files into an executable format


05/25/2020 Fikru Tafesse(MSc. in Computer Science) 10
… Cont’d

• Check for syntax errors and data types

• Helps you to enhance your understanding of language semantics

• Helps to handle language performance issues

• Opportunity for a non-trivial programming project

• The techniques used for constructing a compiler can be useful for other
purposes as well

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 11


Application of Compilers
• Compiler design helps full implementation Of High-Level
Programming Languages

• Support optimization for computer architecture parallelism

• Design of new memory hierarchies of machines

• Widely used for translating programs

• Used with other software productivity tools

05/25/2020 Fikru Tafesse(MSc. in Computer Science) 12


List of Compilers
1. Ada compilers 9. ECMAScript interpreters

2. ALGOL compilers 10. Fortran compilers

3. BASIC compilers 11. Java compilers

4. C# compilers 12. Pascal compilers

5. C compilers 13. PL/I compilers

6. C++ compilers 14. Python compilers

7. COBOL compilers 15. Smalltalk compilers

8. Common Lisp compilers


13
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 phases of compilation.


a. Analysis (Machine Independent/Language Dependent)
b. Synthesis(Machine Dependent/Language independent)

• Compilation process is partitioned into no-of-sub processes called


‘phases’.
14
Source Program Phases …

Lexical Analyzer

Syntax Analyzer

Symbol Table Semantic Analyzer Error Handler


Manager
Intermediate Code
Generator

Code optimizer

Code Generator
Target Program Fig: Phases of Compiler
15
• A source program may be divided into modules stored in separate
files. Phases …

1. Lexical Analysis:
• Lexical analyzer phase is the first phase of compilation process.
• It takes source code as input.
• It reads the source program one character at a time and converts it into
meaningful lexemes.
• Lexical analyzer represents these lexemes in the form of tokens.
2. Syntax Analysis
• Syntax analysis is the second phase of compilation process.
• It takes tokens as input and generates a parse tree as output.
• In syntax analysis phase, the parser checks that the expression made by the
tokens is syntactically correct or not. 16
3. Semantic Analysis Phases …
• Semantic analysis is the third phase of compilation process.
• It checks whether the parse tree follows the rules of language.
• Semantic analyzer keeps track of identifiers, their types and expressions.
• The output of semantic analysis phase is the annotated tree syntax.
4. Intermediate Code Generation
• In the intermediate code generation, compiler generates the source code into
the intermediate code.
• Intermediate code is generated between the high-level language and the
machine language.
• The intermediate code should be generated in such a way that you can easily
translate it into the target machine code.
17
5. Code Optimization Phases …
• Code optimization is an optional phase.
• It is used to improve the intermediate code so that the output of the
program could run faster and take less space.
• It removes the unnecessary lines of the code and
• arranges the sequence of statements in order to speed up the program
execution.
6. Code Generation
• Code generation is the final stage of the compilation process.
• It takes the optimized intermediate code as input and maps it to the target
machine language.
• Code generator translates the intermediate code into the machine code of
the specified computer. 18
Phases …
7. Symbol Table Manager

• This is the portion to keep the names used by the program and records
essential information about each.

• The data structure used to record this information called a symbol table

8. Error Handler

• It is invoked when an error in the source program is detected.

19
Example: Phases …

20
Cousins of Compiler
• The input to compiler may produced by one or more preprocessors, and
further processing of compiler’s output may be needed before running
machine code obtained.

• The following are the cousins of compilers:


i. Preprocessors
ii. Assemblers
iii. Compilers
iv. Loaders
v. Link editors. 21
Cousins …
Skeletal Source Program

Preprocessor
Source Program

Compiler
Target Assembly Program

Assembler
Relocatable Machine Code

Loader/Linker-Editor Library, relocatable obj.


File

Absolute Machine Code

Fig. Cousins of Compiler 22


Preprocessor
• A preprocessor produce input to compilers.
• They may perform the following functions.
1. Macro processing: A preprocessor may allow a user to define macros that are short
hands for longer constructs.
2. File inclusion: A preprocessor may include header files into the program text.
3. Rational preprocessor: these preprocessors augment older languages with more
modern flow-of-control and data structuring facilities.
4. Language Extensions: These preprocessor attempts to add capabilities to the
language by certain amounts to build-in macro
5.
23
Translator
• A software system which converts the source code from one form of language
to another form of language is known as translator.
• Beside program translation, the translator performs another very important
role, the error-detection.
• Any violation of d HLL specification would be detected and reported to the
programmers.
• Important role of translator are:
1. Translating the HLL program input into an equivalent ML program.
2. Providing diagnostic messages wherever the programmer violates specification
of the HLL. 24
• There are three types of translator:
Translator …
• Assembler
• Compiler
• Interpreter
1. Compiler: is a translator which converts the source code from high level
language to low level language.
• The machine-language target program produced by a compiler is usually much
faster than an interpreter at mapping inputs to outputs .
2. Assembler: is a translator that takes the assembly program as input and generates
the machine code as an output.
• An assembly is a mnemonic version of machine code, in which names are used
instead of binary codes for operations.
• There are two types of assembler
1. Single pass: performs on analysis
25
…Cont’d
• 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.

26
Interpreter
• An interpreter is a program that appears to execute a source program as if it
were machine language.

• Languages such as BASIC, SNOBOL, LISP can be translated using interpreters.

• JAVA also uses interpreter.


• The process of interpretation can be carried out in following phases.
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Direct Execution 27
Interpreter …
Advantages:
• Modification of user program can be easily made and implemented as
execution proceeds.
• Type of object that denotes a various may change dynamically.
• Debugging a program and finding errors is simplified task for a program
used for interpretation.
• The interpreter for the language makes it machine independent.
Disadvantages:
• The execution of the program is slower.
• Memory consumption is more. 28
Loader And Link-Editor
• Once the assembler procedures an object program, that program must be
placed into memory and executed.
• The assembler could place the object program directly in memory and transfer
control to it, thereby causing the machine language program to be execute.
• This would waste core by leaving the assembler in memory while the user’s
program was being executed.

• Also the programmer would have to retranslate his program with each
execution, thus wasting translation time.

• To over come this problems of wasted translation time and memory.

• System programmers developed another component called loader.


29
Loader …
• Once the assembler procedures an object program, that program must be
placed into memory and executed.

• A loader is a program that places programs into memory and prepares them
for execution.
• It would be more efficient if subroutines could be translated into object form
the loader could relocate directly behind the user’s program.
• The task of adjusting programs they may be placed in arbitrary core locations
is called relocation.

30
• Linking and Loading has four functions: Loader …

1. Allocation:
• It means get the memory portions from operating system and storing the
object data.
2. Relocation:
• It maps the relative address to the physical address and relocating the
object code.
3. Linker:
• It combines all the executable object module to pre single executable file.
4. Loader:
• It loads the executable file into permanent storage.

31
Grouping of Phases
• Front end : machine independent phases
• Lexical analysis
• Syntax analysis
• Semantic analysis
• Intermediate code generation
• Some code optimization
• Back end : machine dependent phases
• Final code generation
• Machine-dependent optimizations 32
Compiler Construction Tools
• These are specialized tools that have been developed for helping implement
various phases of a compiler. The following are the compiler construction tools:

1. Parser Generators: These produce syntax analyzers, normally from input that
is based on a context-free grammar.
• It consumes a large fraction of the running time of a compiler.
• Example: YACC (Yet Another Compiler-Compiler).

2. Scanner Generator: These generate lexical analyzers, normally from a


specification based on regular expressions.
• The basic organization of lexical analyzers is based on finite automation.
33
… Tools
3. Syntax-Directed Translation: These produce routines that walk the parse
tree and as a result generate intermediate code.
• Each translation is defined in terms of translations at its neighbor nodes in
the tree.

4. Automatic Code Generators: It takes a collection of rules to translate


intermediate language into machine language.
• The rules must include sufficient details to handle different possible
access methods for data.

34
… Tools
5. Data-Flow Engines: It does code optimization using data-flow analysis,
that is, the gathering of information about how values are transmitted from one
part of a program to each other part.

? ?
?
s , …
End e nt
of C m
hapt o m
er 1 . ,C
. We ns
ll Do t i o
ne es
Qu
35

You might also like