Unit 1 (A)
Unit 1 (A)
Introduction to Compilers
Analp Pathak
Assistant Professor
Computer Science Engineering Department
SRM IST, NCR Campus
2
Textbook
• Compilers: Principles, Techniques, and Tools, 2/E.
– Alfred V. Aho, Columbia University
– Ravi Sethi, Avaya Labs
– Jeffrey D. Ullman, Stanford University
Commonly Known as
Dragon Book
Contents
• Overview and History
• What Do Compilers Do
• Compiler and Interpreters
• Phases of a compiler
• Analysis of source program / Translation
• Tex
• Features of Good Programming Language
• The Syntax and Semantics of Programming Language
• Computer Architecture and Compiler Design
• Compiler Construction Tools
5
What Do Compilers Do
• Compilers may generate three types of code:
– Pure Machine Code
• Machine instruction set without assuming the existence
of any operating system or library.
• Mostly being OS or embedded applications.
– Augmented Machine Code
• Code with OS routines and runtime support routines.
• More often
– Virtual Machine Code
• Virtual instructions, can be run on any architecture with
a virtual machine interpreter or a just-in-time compiler
• Ex. Java
8
Source Target
Compiler
Program Program
Compilers
Source
Program
Interpreter Output
Input
Error messages
12
Preprocessors, Compilers,
Assemblers, and Linkers
Skeletal Source Program
Preprocessor
Source Program
Try for example:
Compiler gcc -v myprog.c
Target Assembly Program
Assembler
Relocatable Object Code
Linker Libraries and
Relocatable Object Files
Absolute Machine Code
14
Phases of Compiler
16
Intermediate
Representation
Code
Generator
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Representation
Scanner
The scanner begins the analysis of the source program by
Symbol and Optimizer
reading the input, character by character, and grouping
Attribute
characters into individual words and symbols (tokens)
Tables
RE ( Regular expression )
(Used
NFA ( Non-deterministic Finite by all )
Automata
DFA ( Deterministic FinitePhases
Automataof
)
LEX The Compiler) Code
Generator
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Parser Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Semantic Routines Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Optimizer Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Code Generator Representation
Code
Generator
Target machine code
22
Symbol-table Management
• To record the identifiers in source program
– Identifier is detected by lexical analysis and then is
stored in symbol table
• To collect the attributes of identifiers
(not by lexical analysis)
– Storage allocation : memory address
– Types
– Scope (where it is valid, local or global)
– Arguments (in case of procedure names)
• Arguments numbers and types
• Call by reference or address
• Return types
23
Symbol-table Management
Translation of A Statement
26
Translation of A Statement
27
Translation of A Statement
28
Code Optimization
• It attempts to improve the intermediate code, to
produce more efficient code that will result a faster-
running machine code.
• Compiler Optimizations must meet the following
objectives:
– The optimization must be correct, that is, preserve the
meaning of the compiled program,
– The optimization must improve the performance of many
programs,
– The compilation time must be kept reasonable, and
– The engineering effort required must be manageable.
31
• Register Allocation
• Register Scheduling
• Code Selection
• Addressing Modes
• Instruction format
• Power of instructions
• Optimization at the machine code level
• Back patching
32
Compiler-Construction Tools
• Software development tools are available to
implement one or more compiler phases
– Scanner generators
– Parser generators
– Syntax-directed translation engines
– Automatic code generators
– Data-flow engines
– Compiler-construction toolkits