2. Compilers
• Programming languages are notations for describing computation to
people and to machines
• All the software (including Operating Systems, Embedded Software, Special
Purpose Software, General Software applications) running on the
computers was written in some programming language.
• Types of languages:
• High level language (C++, Java, C-sharp etc.)
• Low level language (one-to-one interaction with Machine e.g., Assembly)
• Machine level language (Interact directly with Machine’s Hardware in the form of
101010….)
• There must be a translation mechanism that tells machine what to do against each set
of instruction
3. Compilers
• A program must be translated into a form in which it can be executed
by a computer
• The software systems that do this translation are called “COMPILERS”
Program in Program in
Source Language Target
(High level) Language
(Machine Level)
Errors
Compiler
4. Objective
• The purpose of this course is to become familiar with the
functionality of different Phases in the construction of a compiler
front end and to gain insight of how these phases are related to each
other.
• Process of construction of compiler
• Lexical analysis
• Syntax Analysis
• Semantic Analysis
• Syntax Directed Translation
• Intermediate Code Representation
5. Language Processors
• Simply stated, a compiler is a program that can read a program in one
language – the source language – and translate into an equivalent
program in another language – the target language.
Source Program
• Important Role
• Report errors, if any, found in source program
Target Program
Compiler
6. Language Processors
• If the target program is an executable machine-language program, it
can then be called by the user to process inputs and produce outputs.
input output
Target Program
7. Interpreter
• Interpreters are the common kind of language processor.
• An interpreter appears to directly execute the program and provide
output.
Source Program
input output
Errors
Interpreter
8. Compiler Vs Interpreter
Compiler Interpreter
Pros Pros
Less space Easy debugging
Fast Execution Fast Development
Cons Cons
Slow Processing Not for large project
(Partly solved; Requires more space
Separate compilation) Slower Execution
Debugging (Interpreter in memory
(Improves through IDEs) all the time)
9. Language Processors
• Example
Java language processors combine compilation and interpretation
source Program
Intermediate Program
output
input
A Hybrid Compiler
Virtual
Machine
Translator
10. Language Processors
• 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 use just-in-time compilers
• It translate the bytecodes into machine language immediately before they run the
intermediate program to process the input.
• In addition to a compiler, several other programs may be required to create
an executable target program.
11. Language Processors
• A source program may be divided into modules stored in separate files.
• The task of collecting the source program is sometimes entrusted to a
separate program, called a preprocessor.
• The preprocessor may also expand “shorthands” called macros into source
language statements.
• The modified source program is then fed to a compiler.
• The compiler may produce an assembly-language program as its output,
because assembly language is easier to produce as output and is easier to
debug.
• The assembly language is then processed by a program called an assembler
that produces machine code as its output.
12. Language Processors
• Large programs are often compiled in pieces, so the machine code
may have to be linked together with other object files and library files
into the code that actually runs on the machine.
• The linker resolves external memory addresses, where the code in
one file may refer to a location in another file.
• The loader then puts together all of the executable objects files into
memory of execution.
17. Analysis
• Operations performed
• Breaks up the source program in to pieces
• Imposes a grammatical structure on these pieces
• Then an intermediate representation (known as Tree Structure = Syntax Tree, in
which we got to know all of the insight operations performed and their hierarchy) is
created using this structure
• Display an error messages (if any)
• It also collects information about the source program and stores in a data structure
called symbol table (Symbol table is used in all compilation phases whether directly
or in any of its updated form)
• Analysis phase is also known as “Front end of a compiler”
18. Synthesis
• Operations performed
• It takes the intermediate representation and information from the symbol
tree as input.
• Construct the desired target program
• Back End of a Compiler
19. Compilation Phases
• A typical decomposition of a
compiler can be done into
several phases: