Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Experiment 01

AIM: - Study the Compilers and various related tools.

PROCEDURE:-
With reference to questions given do the following:
step1 ) Search and study the needed material in depth.
step2 ) (Preferable) use some implementation code snippets to
understand in detail. step3 ) Make a “word” file of your
answer and submit .

Questions:
A) Explain following terms and relation between them.
1) Compilers: Compilers are computer software that translates high-level
language program into assembly language that can be understood by a digital
computer’s CPU. This is one language processor that reads the complete source
program in one go and translates it into an equivalent program in machine
language.

2) Interpreters: An interpreter directly executes instructions written in a


programming language without previously converting them to an object code or
machine code. Interpreted code will show an error as soon as it hits a problem,
so it’s easier to debug than complied code.
3) Linkers: Linkers are tool that merges the object files produced by
separate compilation or assembly and creates an executable file. Linker is a
program in a system which helps to link object modules of program into single
object file. It performs the process of linking.

4) Loaders: It is Part of the OS that brings an executable file residing on


disk into memory and starts it running.
Steps followed by Loadrers:
–> Read executable file’s header to determine the size of text and data segments
–> Create a new address space for the program
–> Copies instructions and data into address space
–> Copies arguments passed to the program on the stack
–> Initializes the machine registers including the stack ptr
–>Jumps to a startup routine that copies the program’s arguments from the stack
to registers and calls the program’s main routine.

Relationship between these terms:


Converting any language, and then loading into memory will depend on these
terms. First complier will convert the any programming language code into
Assembly language code. Now, Assembler will convert this into Machine
language module. Whereas, if interpreter is used, then it will directly convert
into machine code. Now linker will convert this into Executable machine
language program. And finally loader will load this into memory. This is how
these terms are related with each other.

B) Pick any programming language and its compiler. Now explain


following with reference to it.
1) Your chosen language and its characteristics.
Chosen language: Java programming language.
Characteristics:
Java is Familiar: Java is similar to C/C++ but it removes the drawbacks and
complexities of C/C++ like pointers and multiple inheritances. So if you have
background in C/C++, you will find Java familiar and easy to learn.

Java is an Object-Oriented programming language: Unlike C++ which is


semi object-oriented, Java is a fully object-oriented programming language. It
has all OOP features such as abstraction, polymorphism, encapsulation,
inheritance etc.
Java supports Functional programming: Since Java SE version 8 (JDK 8),
Java is updated with functional programming feature like functional interfaces
and Lambda Expressions. This increases the flexibility of Java.
Java is Robust: With automatic garbage collection and simple memory
management model (no pointers like C/C++), plus language features like
generics, Java guides programmer toward reliable programming habits for
creating highly reliable applications.

Java is Secure: The Java platform is designed with security features built into
the language and runtime system such as static type-checking at compile time
and runtime checking (security manager), which let you creating applications
that can’t be invaded from outside. You never hear about viruses attacking Java
applications.
Java is High Performance: Java code is compiled into bytecode which is
highly optimized by the Java compiler, so that the Java virtual machine (JVM)
can execute Java applications at full speed. In addition, compute-intensive code
can be re-written in native code and interfaced with Java platform via Java
Native Interface (JNI) thus improve the performance.

Java is Multithreaded: The Java platform is designed with multithreading


capabilities built into the language. That means you can build applications with
many concurrent threads of activity, resulting in highly interactive and
responsive applications.
Java is Platform Independence: Java code is compiled into intermediate
format (bytecode), which can be executed on any systems for which Java virtual
machine is ported. That means you can write a Java program once and run it on
Windows, Mac, Linux or Solaris without re-compiling. Thus the slogan “Write
once, run anywhere” of Java.
2) Your chosen compiler and its characteristics.

Chosen complier for Java: javac (Java programming language complier) is


one complier which is used for Java.
Characteristics of javac:
Programmatic Interface: The javac command supports the new Java Compiler
API defined by the classes and interfaces in the javax.tools package.
Implicitly Loaded Source Files: To compile a set of source files, the compiler
might need to implicitly load additional source files. Such files are currently not
subject to annotation processing. By default, the compiler gives a warning when
annotation processing occurs and any implicitly loaded source files are
compiled. The -implicit option provides a way to suppress the warning.
Command-Line Argument Files: An argument file can include javac options
and source file names in any combination. The arguments within a file can be
separated by spaces or new line characters. If a file name contains embedded
spaces, then put the whole file name in double quotation marks.

Cross-Compilation Options for javac: javac also supports cross-compiling, in


which classes are compiled against bootstrap classes of a different Java platform
implementation. It’s important to use the -bootclasspath and -extdirs options
when cross-compiling.
Java compiler can be activated by using “Javac.exe” command from the
command prompt. Some of the compiler options are as follows:
-help: Display all the compiler option.
-version: Returns the compiler version.

-verbose: Output compiler messages.


–deprecation: Identify retired APIs.
–nowarn: Generate no warnings

The Java compiler never converts the source code to machine code like that of
the C/C++ compiler.Instead, it converts the source code into an intermediate
code called the byte code and this byte code is further translated to machine-
dependent form by another layer of software called JVM (Java Virtual
Machine).

C) Pick any programming language and find at least two


compiler/interpreter for it. Now compare the two compilers and
explain the benefits and drawbacks of each compiler .
Here I am taking C language as programming language.
Two compliers for C language: GCC and MinGW
Comparison between GCC and MinGw:
GCC stands for GNU Compiler Collection — on open source collection of
compilers for various languages based on a common core compiler and linker.
MinGW stands for Minimal GNU for Windows. This is a copy GCC plus other
common utilities and libraries used in compiling and linking C or C++ code
using the GNU tools. Unlike many of the platforms that GCC is found on,
Windows lacks most of the common utilities and tools, so those are included so
that a lot of open source software originally developed in POSIX environments
can be compiled. In summary, MinGW contains GCC which is in the collection
of GNU free software.

Benefits and Drawbacks of these two:


1) GCC:
GCC contains support for many language. It’s highly portable, and
widely used, and tends to procedure good code. It can be used as a cross
complier (compiling for a system other than the one running GCC). GCC
is very mature, easy to install and is the default complier for lots of
systems. But some extensions in other compliers are not there in GCC.
GCC sometimes ignore the warnings and some non-standard features. It
is not more accessible for complier research. It’s not human-readbale.

2) MinGW:
MinGW also support c, c++, fortran, ada, java and pascal. It can create
32-bit or 64-bit binaries, for any versions of Windows since windows 98.
It is more faster than VS code. This is very capable of producing good
64-bit code for windows. Unix shells and GNU make do a lot of forking,
so running these under MinGW results in “emulated” forks, which are
really slow.

D) Study and explain : Clang


The Clang project provides a language front-end and tooling infrastructure for
languages in the C language family (C, C++, Objective C/C++, OpenCL,
CUDA, and RenderScript) for the LLVM project. Both a GCC-compatible
compiler driver (clang) and an MSVC-compatible compiler driver (clang-
cl.exe) are provided.
Some Characteristics:
● Fast compiles and low memory use

● Expressive diagnostics

● GCC compatibility

● Support diverse clients (refactoring, static analysis, code generation, etc.)

● Allow tight integration with IDEs

● A real-world, production quality compiler

● A simple and hackable code base

● A single unified parser for C, Objective C, C++, and Objective C++

● Conformance with C/C++/ObjC and their variants

E) Study and explain :LLVM


LLVM is a compiler and a toolkit for building compilers, which are programs
that convert instructions into a form that can be read and executed by a
computer. The LLVM project is a collection of modular and reusable compiler
and toolchain technologies. LLVM helps build new computer languages and
improve existing languages.
LLVM is a framework to generate object code from any kind of source code.
On the front end, the LLVM compiler infrastructure uses clang — a compiler
for programming languages C, C++ and CUDA — to turn source code into an
interim format. Then the LLVM clang code generator on the back end turns the
interim format into final machine code.

F) Study and explain : Flex


FLEX (fast lexical analyzer generator) is a tool/computer program for
generating lexical analyzers (scanners or lexers) written by Vern Paxson in C
around 1987. It is used together with Berkeley Yacc parser generator or GNU
Bison parser generator.
A Flex lexical analyzer usually has time complexity of O(n) in the length of the
input. That is, it performs a constant number of operations for each input
symbol.

G) Study and explain : Bison.


Bison is a general-purpose parser generator that converts an annotated context-
free grammar into a deterministic LR or generalized LR (GLR) parser
employing LALR(1) parser tables.

Flex, an automatic lexical analyser, is often used with Bison, to tokenise input
data and provide Bison with tokens.
Reentrancy is a feature which has been added to Bison and does not exist in
Yacc. Normally, Bison generates a parser which is not re-entrant. In order to
achieve reentrancy the declaration %define api.pure must be used.
Bison can generate code for C, C++, D and Java. For using the Bison-generated
parser from other languages a language binding tool such as SWIG can be used.

You might also like