Compiler Construction
(CS F363)
BITS Pilani Dr. Raghunath
CS&IS Dept.
Hyderabad Campus
BITS Pilani, Hyderabad Campus
Lecture -1 Introduction
BITS Pilani
Hyderabad Campus
BITS Pilani, Hyderabad Campus
Books
BITS Pilani, Hyderabad Campus
Course team and Evaluation
CS F363 – L P U – 2 1 3
➢Course Team:
Faculty: Dr. Raghunath (I/C),
Dr. Jabez Christopher,
Dr. Sameera Muhamed Salam,
Ms. K Simran, Ms. Akella Amruta, and Mr. Sandeep Ravikanth
Weightage Nature of
Component Duration Date & Time
(%) Component
Assignments-1 Take Home 12% before mid-sem Open Book
Assignments-2 Take Home 18% after mid-sem Open Book
Mid-Sem 90 mins 28% 16 March 2024 Closed Book
4:00 PM – 5:30 PM
Comprehensive 180 mins 42% 20/05/2024 AN Closed Book
Examination
BITS Pilani, Hyderabad Campus
More about Assignments
➢ Group-based assignment
➢3 to 5 students per each group.
➢Each assignment has 2-3 sub-parts.
➢Important Note: Each submission will go through a plagiarism check,
and if a significant portion of the code is copied from other students or
the internet, serious actions will be taken.
BITS Pilani, Hyderabad Campus
What is a Compiler ?
➢ Compiler: A computer program (a complex program)
that translates a program in one language (the source
language) into an equivalent program in another
language (the target language).
The
“target” language is
Typical “source” usually the instruction
languages might be c, set of some processor.
c++, fortran, Java, etc.
Machine code as Target code: Examples
➢ Compilers that directly produce machine code as their target output are often
referred to as "native code compilers".
➢ Microsoft Visual C++ Compiler
➢ Intel C++ Compiler (ICC)
➢ GCC
➢ These compilers take source code written in high-level languages like C, C++, or
others and generate machine code, which consists of binary instructions that
the CPU can execute directly.
➢ The resulting executable files contain machine code that's specific to the
targeted hardware architecture and can be run on compatible systems without
further translation or interpretation.
Assembly code as Target code: Examples
➢ There are compilers that specifically generate assembly language as their
target output. Some examples include:
➢ GNU Compiler Collection (GCC):
➢ It can generate assembly language code for various processor
architectures, such as x86, ARM, MIPS, and others, using the -S flag.
➢ For instance, compiling a C program with GCC and specifying the -S option
(gcc -S) will generate assembly language output.
➢ Keil C51 Compiler: This compiler is designed for the 8051 microcontroller
architecture. It can compile C code and generate assembly language output
specific to the 8051 microcontroller family.
High-level language as Target code
➢ Some compilers produce a target program written in a human-oriented
programming language rather than the assembly language of some computer.
➢ CoffeeScript Compiler: CoffeeScript is a language that compiles into JavaScript.
➢ TypeScript Compiler (tsc): TypeScript is another language that compiles down
to JavaScript.
➢ Haxe Compiler: Haxe is a high-level programming language that can be
compiled into multiple target languages, such as JavaScript, C++, Java, C#, and
others.
Why compiler is a complex program ?
➢ A compiler is a tool that translates software written in one
language into another language.
➢ To translate text from one language to another, the tool
must understand both the form, or syntax, and content, or
meaning, of the input language.
➢ It needs to understand the rules that govern syntax and
meaning in the output language.
➢ Finally, it needs a scheme for mapping content from the
source language to the target language.
BITS Pilani, Hyderabad Campus
Isn't this a solved problem?
Changes in
architecture
influence changes
in compilers
Machines are constantly changing
BITS Pilani, Hyderabad Campus
The Fundamental Principles of Compilation
➢ The compiler must preserve the meaning of the program being
compiled.
➢ Correctness is a fundamental issue in programming. The
compiler must preserve correctness by faithfully
implementing the “meaning” of its input program.
➢ The compiler must improve/optimize the input program.
Abstract view
Source Code Target Code
Compiler
Errors
Functions:
• Recognize legal (and illegal) programs
• Generate correct code
• Manage storage of all variables
• Code agreement on format for object (or assembly) code
BITS Pilani, Hyderabad Campus
Structure of a compiler
➢ The compiler has a front end to deal with the source language.
➢ It has a back end to deal with the target language.
➢ Connecting the front end and the back end, it has a formal
structure for representing the program in an intermediate form
whose meaning is largely independent of either language.
BITS Pilani, Hyderabad Campus
Structure of a compiler
➢ To improve the translation, a compiler often includes an
optimizer that analyzes and rewrites that intermediate form.
BITS Pilani, Hyderabad Campus
Phases of a compiler
➢ If we divide the compiler on the basis of the way in which the
compiler compiles the program, then we can divide it into two
phases.
➢ Analysis Phase
➢ Synthesis Phase
BITS Pilani, Hyderabad Campus
Phases of a compiler
➢ The Front End / analysis part
➢ It 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
➢ The front end determines if the input code is well formed, in terms of
both syntax and semantics.
➢ If it finds that the code is valid, it creates a representation of the code in
the compiler’s intermediate representation;
➢ If not, it reports back to the user with diagnostic error messages to
identify the problems with the code.
BITS Pilani, Hyderabad Campus
Phases of a compiler
➢ The Back End
➢ The compiler’s back end traverses the IR form of the code and emits
code for the target machine.
➢ It selects target-machine operations to implement each IR operation.
➢ It chooses an order in which the operations will execute efficiently.
➢ It decides which values will reside in registers and which values will
reside in memory and inserts code to enforce those decisions.
BITS Pilani, Hyderabad Campus
Phases of a compiler
BITS Pilani, Hyderabad Campus
Phases of a compiler
BITS Pilani, Hyderabad Campus
Interesting points
➢ Compiler construction is a microcosm of computer science.
➢ Greedy algorithms (register allocation),
➢ heuristic search techniques (list scheduling),
➢ graph algorithms (dead-code elimination),
➢ dynamic programming (instruction selection),
➢ finite automata and push-down automata (scanning and
parsing), and
➢ fixed-point algorithms (data-flow analysis).
It deals with problems such as dynamic allocation, synchronization,
naming, locality, memory hierarchy management, and pipeline
scheduling.
BITS Pilani, Hyderabad Campus
Course Outline
➢ Lexical Analysis
➢ Syntax Analysis
➢ Semantic Analysis / Syntax Directed Translation
➢ Intermediate Code Generation
➢ Code Optimization
➢ Code Generation
BITS Pilani, Hyderabad Campus
Thank you
BITS Pilani, Hyderabad Campus