0% found this document useful (0 votes)
35 views15 pages

CD Greeshma

Uploaded by

Prasanna Latha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views15 pages

CD Greeshma

Uploaded by

Prasanna Latha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Compiler Design - Introduction

Prepared By
Prof.G.Greeshma
AP / CSE
JCT College of Engineering and Technology

1
Outlines
 1.1 Overview and History
 1.2 What Do Compilers Do?
 1.3 The Structure of a Compiler
 1.4 The Syntax and Semantics of Programming Languages
 1.5 Compiler Design and Programming Language Design
 1.7 Computer Architecture and Compiler Design
 1.8 Compiler Design Considerations

2
Overview and History (1)
 Cause
 Software for early computers was written in assembly language
 The benefits of reusing software on different CPUs started to become
significantly greater than the cost of writing a compiler

 The first real compiler


 FORTRAN compilers of the late 1950s
 18 person-years to build

3
Overview and History (2)

 Compiler technology
 is more broadly applicable and has been employed in rather
unexpected areas.
 Text-formatting languages,
like nroff and troff; preprocessor packages like eqn, tbl, pic
 Silicon compiler for the creation of VLSI circuits
 Command languages of OS
 Query languages of Database systems

4
What Do Compilers Do (1)

 A compiler acts as a translator,


transforming human-oriented programming languages
into computer-oriented machine languages.
 Ignore machine-dependent details for programmer

Programming Machine
Language Compiler Language
(Source) (Target)

5
What Do Compilers Do (2)
 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

6
What Do Compilers Do (3)
 Another way that compilers
differ from one another is in the format of the target
machine code they generate:
 Assembly or other source format
 Relocatable binary
 Relative address
 A linkage step is required
 Absolute binary
 Absolute address
 Can be executed directly

7
The Structure of a Compiler (1)

 Any compiler must perform two major tasks

Compiler

Analysis Synthesis

 Analysis of the source program


 Synthesis of a machine-language program

8
The Structure of a Compiler (2)

Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines

Intermediate
Representation

Symbol and Optimizer


Attribute
Tables

(Used by all Phases of The Compiler)

Code
Generator
9
Target machine code
The Structure of a Compiler (3)

Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines

Intermediate
Scanner Representation

 The scanner begins the analysis of the source program by


reading the input, character by character, and grouping
Symbol and Optimizer
characters into individual words and symbols (tokens)
Attribute
Tables
 RE ( Regular expression )
 NFA ( Non-deterministic Finite Automata )
 (Used by) all
DFA ( Deterministic Finite Automata
 LEX Phases of
The Compiler) Code
Generator
10
Target machine code
The Structure of a Compiler (4)

Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines

Intermediate
Parser Representation

 Given a formal syntax specification (typically as a context-free


grammar [CFG] ), the parse reads tokens and groups them into
Symbol and Optimizer
units as specified by the productions of the CFG being used.
Attribute
 As syntactic structure is recognized, the parser either calls
Tables
corresponding semantic routines directly or builds a syntax
tree.
(Used
 CFG ( Context-Free Grammar ) by all
 BNF ( Backus-Naur Form ) Phases of
 GAA ( Grammar Analysis Algorithms )
The Compiler) Code
 LL, LR, SLR, LALR Parsers Generator
 YACC
11
Target machine code
The Structure of a Compiler (5)

Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines

Intermediate
Semantic Routines Representation
 Perform two functions
 Check the static semantics of each construct
Symbol and
 Do the actual translation Optimizer
 The heart of a compilerAttribute
Tables
 Syntax Directed Translation
 Semantic Processing Techniques
(Used
by all
 IR (Intermediate Representation)
Phases of
The Compiler) Code
Generator
12
Target machine code
The Structure of a Compiler (6)

Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines

Intermediate
Optimizer Representation

 The IR code generated by the semantic routines is analyzed and


transformed into functionally equivalent but improved IR code
Symbol and Optimizer
 This phase can be very complex and slow
Attribute
 Peephole optimization
Tables
 loop optimization, register allocation, code scheduling
(Used by all
 Register and Temporary Management
 Peephole Optimization Phases of
The Compiler) Code
Generator
13
Target machine code
The Structure of a Compiler (7)

Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines

Intermediate
Code Generator Representation
 Interpretive Code Generation
 Generating Code from Tree/Dag
 Grammar-Based Code Generator
Optimizer

Code
Generator
14 Target machine code
The Structure of a Compiler (8)
Code Generator
[Intermediate Code Generator]

Non-optimized Intermediate Code


Scanner
[Lexical Analyzer]

Tokens

Code Optimizer
Parser
[Syntax Analyzer]
Optimized Intermediate Code
Parse tree

Code Optimizer
Semantic Process
[Semantic analyzer] Target machine code

Abstract Syntax Tree w/ Attributes

15

You might also like