You are on page 1of 23

Compiler Design

Outline
• Syllabus & Text books
• History of compilers
• Interpreters
• What are compilers?
• Types of compilers
• Properties of the compiler
• Parts of compilation
• Analysis of the source program
Syllabus
PRINCIPLES OF COMPILER DESIGN LTPMC
3 1 0 100 4
UNIT I INTRODUCTION TO COMPILERS 9+3
Compilers, Analysis of the Source Program, The Phases of a Compiler,
Cousins of the Compiler, The Grouping of Phases, Compiler-Construction
Tools, Translators-Compilation and Interpretation, A simple one-pass
compiler

UNIT II LEXICAL ANALYSIS 9+3


Need and role of lexical analyzer-Lexical errors, Input Buffering -
Specification ofTokens, Recognition of Tokens, A Language for Specifying
Lexical Analyzers, Finite Automata, From a Regular Expression to an NFA,
Design of a Lexical Analyzer Generator

UNIT III SYNTAX ANALYSIS 9+3


Need and role of the parser- Context Free Grammars-Top Down parsing –
Recursive Descent Parser - Predictive Parser - LL(1) Parser -Shift Reduce
Parser - LR Parser - LR(0) item - Construction of SLR Parsing table
-Introduction to LALR Parser, YACC Design of a syntax analyzer for a
sample language
Syllabus (contd)
UNIT IV SYNTAX DIRECTED TRANSLATION AND TYPE
CHECKING 9+3
Syntax-Directed Definitions, Construction of Syntax Trees, Bottom-Up
Evaluation of S Attributed Definitions, L-Attributed Definitions, Top Down
Translation, Bottom-Up Evaluation of Inherited Attributes, Forms of
intermediate code -Translation of Assignment, Boolean Expression and
Control statements - Back patching type systems- Specification of a simple
type checker - equivalence of type expressions – type conversions

UNIT V RUN-TIME ENVIRONMENT AND ERROR HANDLING 9+3


Source language issues-Storage organization-Storage allocation-parameter
passing-Symbol tables-Dynamic storage allocation-Storage allocation in
FORTRAN, Error handling and recovery in different phases. Principal
sources of Optimization – DAG -Optimization of basic blocks-Global data
flow analysis - Efficient data flow algorithms -Issues in design of a code
generator-a simple code generator algorithm
TOTAL : 60
Text Books
1. Alfred V.Aho, Ravi Sethi and Jeffrey
D.Ullman, "Compilers – Principles,
Techniques and Tools", second edition,
Pearson Education, New Delhi, 2006.

2. Raghavan V, “Principles of Compiler


Design”, Tata Mc-Graw Hill Education
Pvt.Ltd., New Delhi, 2009
Why Study Compilers?
• Build a large, ambitious software system.
• See theory come to life.
• Learn how to build programming languages.
• Learn how programming languages work.
High-Level Languages

Rear Admiral Grace


Hopper, inventor of
A-0, COBOL, and the
term “compiler.”

John Backus,
team lead on
FORTRAN.
History of Compilers
• First complete compiler (1957) for
FORTRAN – John Backus
• Early compilers – Assembly language
• Self Hosting compiler
– LISP (1962)
– PASCAL / C
– Bootstrapping
Interpreter
• An interpreter reads the source code one instruction or
line at a time, converts this line into machine code and
executes it.

• The machine code is then discarded and the next line is


read.

• Can interrupt; change the program and either continue or


start again.

Disadvantages
• Examples of interpreters are BASIC, script interpreters
such as JavaScript, and languages such as Lisp.
Interpreter
Intermediate
code

Front end Back end


(Analysis) (Synthesis)
Source code Executable
code
• Instead of producing an intermediate
form the code is executed directly
– Interpreting backend
Interpreter
• Interpreters
– Used in educational / software development
scenarios
– Easier to write than back ends
– Interpreters are normally written in a HLL
– Allows better error checking and reporting
Compiler
A program which translates a program
written in one language to an equivalent
program in other language.

Source Target
Compiler Program
Program

Error
Messages
Types of Compilers
• One pass
– Quick ; PASCAL
• Multi-pass
– Step by step
– High performance; slower; lesser memory
• Source to Source Compiler
Types of Compilers
• Stage Compiler
– Byte code compiler
• JIT Compiler
– Byte code is compiled to native machine code
prior to execution
• Decompiler
Types of Compilers
• Transcompilers / Cascaders
• Cross Compilers
– Embedded Systems
• Narrow Vs Broad Compilers
Properties of a good compiler
• Reliability
• Faithfulness
• Compilation Speed
• Diagnostics
• Error handling
• Implementation and Maintenance
• Good human interface
Properties of generated code
• Reliable
• Secure
• Faithful
• Small
• Fast
Parts of the compilation
• Analysis
• Synthesis

Analysis: Breaks up the source program into constituent


pieces and creates IR

Tools: Structure Editors, Pretty Printers, Static Checker,


Interpreter

Synthesis: Construct the desired target program from IR.


Analysis of the Source Program
• Linear Analysis: Lexical Analysis (or) Scanning
It is a process of reading the characters from left to right
and grouped into tokens having a collective meaning.

• Hierarchical Analysis: Syntax Analysis (or) Parsing


It involves the grouping of tokens of the source program
into grammatical phases that are used by the compiler to
synthesize output.

• Semantic Analysis: Checks the source program for


semantic errors and gathers type information for the
subsequent code generation phase.
Phases of the Compiler
• Lexical analysis (Scanning): Identify logical pieces of the
description.
● Syntax analysis (Parsing): Identify how those pieces relate
to each other.
● Semantic analysis: Identify the meaning of the
overall structure.
● IR Generation: Design one possible structure.
● IR Optimization: Simplify the intended structure.
● Code Generation: Fabricate the structure.
● Code Optimization: Improve the resulting structure.
The Structure of a Modern Compiler

Lexical Analysis
Source Code
Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Code Optimization Machine


Code
Front End
Back End

You might also like