compiler introduction 1 kavita patel. outlines 2 1.1 what do compilers do? 1.2 the structure of a...

17
Compiler Introduction 1 Kavita Patel

Upload: sherilyn-hancock

Post on 20-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Compiler Introduction

1

Kavita Patel

Page 2: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Outlines

2

1.1 What Do Compilers Do? 1.2 The Structure of a Compiler 1.3 Compilation Process 1.4 Phases of Compiler 1.5 Process of Compiling 1.6 The Grouping of Phases 1.7 Compiler-Construction Tools 1.8 Compiler vs. Interpreter

Page 3: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

What Do Compilers Do (1)

3

A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages.

Ignore machine-dependent details for programmer

Programming Language(Source)

CompilerMachineLanguage(Target)

Page 4: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

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

4

Page 5: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

5

Any compiler must perform two major tasks

Analysis of the source program Synthesis of a machine-language program

The Structure of a Compiler

Compiler

Analysis Synthesis

Page 6: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Compilation Process: Phases of Compiler

6

Lexical Analysis Syntax AnalysisSemanticAnalysis

CodeGenerator

Code Optimizer

SourceProgram Token

sSyntactic

Structure

Symbol andAttribute

Tables

(Used by all Phases of The Compiler)

(Character Stream)

IntermediateRepresentation

Target machine code

Page 7: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Phases of Compiler(2)

7

Scanner ParserSemanticRoutines

CodeGenerator

Optimizer

SourceProgram Tokens Syntactic

Structure

Symbol andAttribute

Tables

(Used by allPhases of The Compiler)

Lexical Analyzer(Scanner)

The scanner begins the analysis of the source program by reading the input, character by character, and grouping characters into individual words and symbols (tokens)

RE ( Regular expression ) NFA ( Non-deterministic Finite Automata ) DFA ( Deterministic Finite Automata ) LEX

(Character Stream)

IntermediateRepresentation

Target machine code

Page 8: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Phases of Compiler(3)

8

Scanner ParserSemanticRoutines

CodeGenerator

Optimizer

SourceProgram Tokens Syntactic

Structure

Symbol andAttribute

Tables

(Used by allPhases of The Compiler)

Syntax Analyzer(Parser)

Given a formal syntax specification (typically as a context-free grammar [CFG] ), the parse reads tokens and groups them into units as specified by the productions of the CFG being used.

As syntactic structure is recognized, the parser either calls corresponding semantic routines directly or builds a syntax tree.

CFG ( Context-Free Grammar ) GAA ( Grammar Analysis Algorithms ) LL, LR, SLR, LALR Parsers YACC

(Character Stream)

IntermediateRepresentation

Target machine code

Page 9: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Phases of Compiler(4)

9

Scanner ParserSemanticRoutines

CodeGenerator

Optimizer

SourceProgram

(Character Stream)

Tokens Syntactic

Structure

IntermediateRepresentation

Symbol andAttribute

Tables

(Used by allPhases of The Compiler)

Semantic Routines Perform two functions

Check the static semantics of each construct Do the actual translation

The heart of a compiler

Syntax Directed Translation Semantic Processing Techniques IR (Intermediate Representation)

Target machine code

Page 10: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Phases of Compiler(5)

10

Scanner ParserSemanticRoutines

CodeGenerator

Optimizer

SourceProgram Tokens Syntactic

Structure

Symbol andAttribute

Tables

(Used by allPhases of The Compiler)

Optimizer The IR code generated by the semantic routines is

analyzed and transformed into functionally equivalent but improved IR code

This phase can be very complex and slow loop optimization, register allocation, code

scheduling

Register and Temporary Management

(Character Stream)

IntermediateRepresentation

Target machine code

Page 11: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Phases of Compiler(6)

11

SourceProgram

(Character Stream)

ScannerTokens

ParserSyntactic

StructureSemanticRoutines

IntermediateRepresentation

Optimizer

CodeGenerator

Code Generator Interpretive Code Generation Generating Code from Tree/Dag Grammar-Based Code Generator

Target machine code

Page 12: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Phases of Compiler(7)

12

Scanner [Lexical Analyzer]

Parser [Syntax Analyzer]

Semantic Process [Semantic analyzer]

Code Generator[Intermediate Code Generator]

Code Optimizer

Tokens

Parse tree

Abstract Syntax Tree w/ Attributes

Non-optimized Intermediate Code

Optimized Intermediate Code

Code Optimizer

Target machine code

Page 13: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

13

Process of Compiling

scanner

parser

Semantic analyzer

Intermediate code generator

Code optimization

Code generator

Stream of characters

Stream of tokens

Parse/syntax tree

Annotated tree

Intermediate code

Intermediate code

Target code

Error Handler

Symbol Table Manager

Page 14: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

14

The Grouping of Phases

Compiler front and back ends: Front end:

Analysis steps + Intermediate code generation Depends primarily on the source language Machine independent

Back end: Code optimization and generation Independent of source language Machine dependent

Page 15: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

15

Compiler-Construction Tools Software development tools are available

to implement one or more compiler phases Scanner generators (LEX) Parser generators (YACC) Syntax-directed translation engines Automatic code generators Data-flow engines

Page 16: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

16

Page 17: Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases

Thank You

17