Você está na página 1de 6

Compiler Design Practical: 1

Practical: 1

Aim :- Introduction to Compiler Design

A) Define basic terminology related to Compiler Design.

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

Compiler design principles provide an in-depth view of translation and optimization


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

 Interpreter
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.

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

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

 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

15012011019:Krisha Doshi 1
Compiler Design Practical: 1

data) and creates memory space for it. It initializes various registers to initiate
execution.

 Application Software

Application software are the software that are designed to satisfy a particular need of
a particular environment. Examples of application software are-student record
software, railway reservation software, income tax software, word processors etc.
Examples include enterprise software, accounting software, office suites, graphics
software and media players.

 System Software

System software is computer software designed to operate the computer hardware, to


provide basic functionality, and to provide a platform for running application
software. System software includes device drivers, operating systems, servers,
utilities, and window systems. System software is responsible for managing a variety
of independent hardware components, so that they can work together harmoniously.

 Cross Compiler

A compiler that runs on platform (A) and is capable of generating executable code for
platform (B) is called a cross-compiler.

B) Study about each modules of compilation process with program.

Preprocessing

The first stage of compilation is called preprocessing. In this stage, lines starting with
a # character are interpreted by the preprocessor as preprocessor commands. These
commands form a simple macro language with its own syntax and semantics. This
language is used to reduce repetition in source code by providing functionality to inline
files, define macros, and to conditionally omit code.Before interpreting commands, the
preprocessor does some initial processing. This includes joining continued lines (lines
ending with a \) and stripping comments.

To print the result of the preprocessing stage, pass the -E option to cc:
gcc -E process.c

15012011019:Krisha Doshi 2
Compiler Design Practical: 1

Compilation

The second stage of compilation is confusingly enough called compilation. In this stage,
the preprocessed code is translated to assembly instructions specific to the target
processor architecture. These form an intermediate human readable language.

15012011019:Krisha Doshi 3
Compiler Design Practical: 1

The existence of this step allows for C code to contain inline assembly instructions and
for different assemblers to be used.Some compilers also supports the use of an
integrated assembler, in which the compilation stage generates machine code directly,
avoiding the overhead of generating the intermediate assembly instructions and
invoking the assembler.

To save the result of the compilation stage, pass the -S option to cc:

gcc -S process.c

This will create a file named hello_world.s, containing the generated assembly
instructions.

Assembly

During this stage, an assembler is used to translate the assembly instructions to object
code. The output consists of actual instructions to be run by the target processor.

To save the result of the assembly stage, pass the -c option to cc:

gcc -c process.c

Running the above command will create a file named process.o, containing the object
code of the program. The contents of this file is in a binary format.

15012011019:Krisha Doshi 4
Compiler Design Practical: 1

Linking

The object code generated in the assembly stage is composed of machine instructions
that the processor understands but some pieces of the program are out of order or
missing. To produce an executable program, the existing pieces have to be rearranged
and the missing ones filled in. This process is called linking.

The linker will arrange the pieces of object code so that functions in some pieces can
successfully call functions in other ones. It will also add pieces containing the
instructions for library functions used by the program. In the case of the “Hello,
World!” program, the linker will add the object code for the puts function.

The result of this stage is the final executable program. When run without
options, cc will name this file a.out. Execute:

gcc process.o
gcc –C process.s

15012011019:Krisha Doshi 5
Compiler Design Practical: 1

15012011019:Krisha Doshi 6

Você também pode gostar