You are on page 1of 11

Compilers

Why Study Compilers?


 Compilers enable programming at a high
level language instead of machine
instructions.
 Portability,Modularity, Simplicity, Programmer
Productivity
 Also Efficiency and Performance
Compilers Construction touches
many topics in Computer Science
 Theory
 Finite State Automata, Grammars and Parsing, data-flow
 Algorithms
 Graph manipulation, dynamic programming
 Data structures
 Symbol tables, abstract syntax trees
 Systems
 Allocation and naming, multi-pass systems, compiler construction
Compilers Construction touches
many topics in Computer Science...
 Computer Architecture
 Memory hierarchy, instruction selection, interlocks and latencies
 Security
 Detection of and Protection against vulnerabilities
 Software Engineering
 Software development environments, debugging
 Artificial Intelligence
 Heuristic based search
How to instruct the computer
• Write a program using a programming language
– High-level Abstract Description

• Microprocessors talk in assembly language


– Low-level Implementation Details

Program
written
in a
Compiler Assembly
Language
Programming Translation
Languages
How to instruct the computer
 Input: High-level programming language
 Output: Low-level assembly instructions

 Compiler does the translation:


 Read and understand the program
 Precisely determine what actions it require
 Figure-out how to faithfully carry-out those
actions
 Instruct the computer to carry out those actions
Input to the Compiler
 Standard imperative language(Java,C,C++)
 State
 Variables,
 Structures,

 Arrays

 Computation
 Expressions (arithmetic, logical, etc.)
 Assignment statements

 Control flow (conditionals, loops)

 Procedures
Output of the Compiler
 State
 Registers
 Memory with Flat Address Space
 Machine code – load/store architecture
 Load, store instructions
 Arithmetic, logical operations on registers
 Branch instructions
Compilers
 A compiler is a program that reads a program in one
language, the source language and translates into an
equivalent program in another language, the target
language.
 The translation process should also report the presence
of errors in the source program.
Source Program→ Compiler→Target Program

Error Messages
 There are two parts of compilation.
 The analysis part breaks up the source program into
constant piece and creates an intermediate
representation of the source program.
 The synthesis part constructs the desired target program
from the intermediate representation.
Phases of Compiler
 The compiler has a number of phases plus symbol table manager and an error
handler.

Input Source Program



Lexical Analyzer

Syntax Analyzer

Symbol Table Manager Semantic Analyzer Error Handler

Intermediate Code Generator

Code Optimizer

Code Generator

Out Target Program
Compilers
 The cousins of the compiler are
1. Preprocessor.
2. Assembler.
3. Loader and Link-editor.

 Lexical analyzer takes the source program as an input


and produces a long string of tokens.
 Syntax Analyzer takes an out of lexical analyzer and
produces a large tree.
 Semantic analyzer takes the output of syntax analyzer
and produces another tree.
 Similarly, intermediate code generator takes a tree as an
input produced by semantic analyzer and produces
intermediate code.

You might also like