You are on page 1of 22

Language processor processing system

 Every computer system is made of hardware and software.


 The hardware understands a language, which we cannot
understand. So, we write programs in high –level language, which
is easier for us to understand and remember.
 These programs are then fed into a SERIES OF TOOLS and
OPERATING SYSTEM components to get the desired code that
can be used by the machine.

 SOURCE CODE: source code is a collection of computer


instructions, possibly with comments, written using a human
readable programming language usually as text.

Compiler Construction 1
Language Processing System

Compiler Construction 2
Preprocessor

 A preprocessor, generally considered as


a part of compiler, is a tool that produces
input for compilers. It deals with macro-
processing, augmentation, file inclusion,
language extension, etc.

Compiler Construction 3
Assembler

 An assembler translates assembly


language programs into machine
code.The output of an assembler is
called an object file, which contains a
combination of machine instructions as
well as the data required to place these
instructions in memory.

Compiler Construction 4
Linker

 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.

Compiler Construction 5
Loader

 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
memory space for it. It initializes various
registers to initiate execution.

Compiler Construction 6
Cross Compiler

 A compiler that runs on platform (A) and


is capable of generating executable code
for platform (B) is called a cross-
compiler.

Compiler Construction 7
Cross compilation Process
 A cross compiler is a compiler capable of creating
executable code for a platform other than the one on
which the compiler is run.

 Cross compiler tools are used to generate


executables for embedded system or multiple
platforms.

 It is used to compile for a platform upon which it is not


feasible to do the compiling, like microcontrollers that
don't support an operating system.

Compiler Construction 8
Uses of Cross Compilers
 The fundamental use of a cross compiler is to separate the build
environment from target environment. This is useful in a number
of situations:
 Embedded computers where a device has extremely limited
resources.
 For example, a microwave oven will have an extremely small
computer to read its touchpad and door sensor, provide output to
a digital display and speaker, and to control the machinery for
cooking food. This computer will not be powerful enough to run a
compiler, a file system, or a development environment.
 Since debugging and testing may also require more resources
than are available on an embedded system, cross-compilation
can be less involved and less prone to errors than native
compilation.

Compiler Construction 9
Cross Compilers cont’d
 Compiling for multiple machines.
 For example, a company may wish to support several
different versions of an operating system or to support
several different operating systems. By using a cross
compiler, a single build environment can be set up to
compile for each of these targets.
 Compiling on a server farm. Similar to compiling for
multiple machines, a complicated build that involves
many compile operations can be executed across any
machine that is free, regardless of its underlying
hardware or the operating system version that it is
running.
Compiler Construction 10
Source-to-source Compiler

 A compiler that takes the source code of


one programming language and
translates it into the source code of
another programming language is called
a source-to-source compiler.

Compiler Construction 11
Interpreter
•An interpreter is another common kind of language processor. Instead of
producing a target program as a translation, an interpreter appears to directly
execute the operations specified in the source program on inputs supplied by the
user

The machine-language target program produced by a compiler is usually much


faster than an interpreter at mapping inputs to outputs . An interpreter, however,
can usually give better error diagnostics than a compiler, because it executes the
source program statement by statement.

Compiler Construction 12
Interpreter cont’d
An interpreter, like a compiler, translates high-level
language into low-level machine language.
The difference lies in the way they read the source code
or input.
A compiler reads the whole source code at once,
creates tokens, checks semantics, generates
intermediate code, executes the whole program and
may involve many passes.
In contrast, an interpreter reads a statement from the
input, converts it to an intermediate code, executes it,
then takes the next statement in sequence. If an error
occurs, an interpreter stops execution and reports it.
whereas a compiler reads the whole program even if it
encounters several errors

Compiler Construction 13
Compilers and Interpreters

Like a compiler, interpreter translates high level language to


machine language.
The difference lies the way they read the source code or input.
compiler reads whole source code at once, creates tokens,
checks semantics, generates intermediate code, executes the
whole program and may involve many passes.
Interpreter reads a statement from the input , converts it to an
intermediate code, executes it, then go to the next statement
in sequence. If any ERROR occurs, an interpreter stops the
execution and reports it.
Where as compiler reads whole program even if it encounters
several ERRORS.

Compiler Construction 14
Overview of Compilers

Compilation Process:
Data

Source Object Executing Results


Compiler
program program Computer
Compile time run time
Interpretive Process:
Data

Source
Compiler Result
program

Compiler Construction 15
Example Of Combining Both Interpreter and Compiler

 Java language processors combine


compilation and interpretation,

 A Java source program may first be compiled into an intermediate form called
bytecodes.
 The bytecodes are then interpreted by a virtual machine. A benefit of this
arrangement is that bytecodes compiled on one machine can be interpreted on
another machine, perhaps across a network.
 In order to achieve faster processing of inputs to outputs, some Java compilers,
called just-in-time compilers, translate the bytecodes into machine language
immediately before they run the intermediate program to process the input.

Compiler Construction 16
Model of A Compiler

 A compiler must perform two tasks:


- analysis of source program: The analysis part breaks up
the source program into constituent pieces and imposes a
grammatical structure on them. It then uses this structure to
create an intermediate representation of the source program.
- synthesis of its corresponding program: constructs the
desired target program from the intermediate representation
and the information in the symbol table.
 The analysis part is often called the front end of
the compiler; the synthesis part is the back end.

Compiler Construction 17
Compiling Passes

 Several phases can be implemented as a


single pass consist of reading an input file
and writing an output file.
 A typical multi-pass compiler looks like:
 First pass: preprocessing, macro expansion
 Second pass: syntax-directed translation, IR
code generation
 Third pass: optimization

 Last pass: target machine code generation

Compiler Construction 18
Cousins of Compilers

 Preprocessors
 Assemblers
 Compiler may produce assembly code instead of
generating relocatable machine code directly.
 Loaders and Linkers
 Loader copies code and data into memory, allocates
storage, setting protection bits, mapping virtual
addresses, .. Etc
 Linker handles relocation and resolves symbol
references.
 Debugger
Compiler Construction 19
Tasks of Compilation Process and Its Output

 Each tasks is assigned to a phase, e.g. Lexical


Analyzer phase, Syntax Analyzer phase, and so on.
 Each task has input and output.
 Any thing between brackets in the last figure is
output of a phase.
 The compiler first analyzes the program, the result
is representations suitable to be translated later on:
- Parse tree
- Symbol table

Compiler Construction 20
Parse Tree and Symbol Table

 Parse tree defines the program structure; how to


combine parts of the program to produce larger part
and so on.

 Symbol table provides


- the associations between all occurrences of each
name given in the program.
- It provides a link between each name and it
declaration.

Compiler Construction 21
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.

Compiler Construction 22

You might also like