+ All Categories
Home > Documents > Overview of Compilation

Overview of Compilation

Date post: 05-Jan-2016
Category:
Upload: hamal
View: 17 times
Download: 0 times
Share this document with a friend
Description:
Overview of Compilation. Programming Language Concepts Lecture 2. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. Target. Source. Translator. Overview of Translation. - PowerPoint PPT Presentation
28
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 2
Transcript
Page 1: Overview of Compilation

Overview of Compilation

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 2

Page 2: Overview of Compilation

Overview of Translation

• Definition: A translator is an algorithm that converts source programs into equivalent target programs.

• Definition: A compiler is a translator whose target language is at a “lower” level than its source language.

TranslatorSource Target

Page 3: Overview of Compilation

Overview of Translation (cont’d)

• When is one language’s level “lower” than another’s?

• Definition: An interpreter is an algorithm that simulates the execution of programs written in a given source language.

InterpreterSource

input

output

Page 4: Overview of Compilation

Overview of Translation (cont’d)

• Definition: An implementation of a programming language consists of a translator (or compiler) for that language, and an interpreter for the corresponding target language.

InterpreterTarget

input

output

CompilerSource

Page 5: Overview of Compilation

Translation

• A source program may be translated an arbitrary number of times before the target program is generated.

Translator1

Source

Translator2

TranslatorN

Target

...

Page 6: Overview of Compilation

Translation (cont’d)

• Each of these translations is called a phase, not to be confused with a pass, i.e., a disk dump.

Q: How should a compiler be divided into phases?

A: So that each phase can be easily described by some formal model of computation, and so the phase can be carried out efficiently.

Page 7: Overview of Compilation

Translation (cont’d)

Q: How is a compiler usually divided?A: Two major phases, with many possibilities

for subdivision.• Phase 1: Analysis (determine correctness)• Phase 2: Synthesis (produce target code)

• Another criterion:• Phase 1: Syntax (form).• Phase 2: Semantics (meaning).

Page 8: Overview of Compilation

Typical Compiler Breakdown

• Scanning (Lexical analysis).• Goal: Group sequences of characters

that occur on the source, into logical atomic units called tokens.

• Examples of tokens: Identifiers, keywords, integers, strings, punctuation marks, “white spaces”, end-of-line characters, comments, etc., …

Scanner (Lexical analysis)

Source

Sequence of Tokens

Page 9: Overview of Compilation

Lexical Analysis

• Must deal with end-of-line and end-of-file characters.

• A preliminary classification of tokens is made. For example, both ‘program’ and ‘Ex’ are classified as Identifier.

• Someone must give unambiguous rules for forming tokens.

Page 10: Overview of Compilation
Page 11: Overview of Compilation

Screening

• Goals:• Remove unwanted tokens.• Classify keywords.• Merge/simplify tokens.

Screener

Sequence of Tokens

Sequence of Tokens

Page 12: Overview of Compilation

Screening

• Keywords recognized.• White spaces (and comments) discarded.• The screener acts as an interface between

the scanner and the next phase, the parser.

Page 13: Overview of Compilation
Page 14: Overview of Compilation

Parsing (Syntax Analysis)

• Goals• To group together the tokens, into

the correct syntactic structures, if possible.

• To determine whether the tokens appear in patterns that syntactically correct.

Page 15: Overview of Compilation

Parsing (Syntax Analysis)

• Syntactic structures:• Expressions• Statements• Procedures• Functions• Modules

• Methodology:• Use “re-write” rules (a.k.a. BNF).

Page 16: Overview of Compilation

String-To-Tree Transduction

• Goal: To build a “syntax tree” from the sequence of rewrite rules. The tree will be the functional representation of the source.

• Method: Build tree “bottom-up,” as the rewrite rules are emitted. Use a stack of trees.

Page 17: Overview of Compilation
Page 18: Overview of Compilation

Contextual Constraint Analysis

• Goal: To analyze static semantics, e.g.,• Are variables declared before they are used?• Is there assignment compatibility?

e.g., a:=3• Is there operator type compatibility?

e.g., a+3• Do actual and formal parameter types match?

e.g. int f(int n, char c) {…} ...

f('x', 3); • Enforcement of scope rules.

Page 19: Overview of Compilation

Contextual Constraint Analysis

• Method: Traverse the tree recursively, deducing type information at the bottom, and passing it up.

– Make use of a DECLARATION TABLE, to record information about names.

– “Decorate” tree with reference information.

Page 20: Overview of Compilation
Page 21: Overview of Compilation

Example

Chronologically,1. Enter x into the DCLN table, with its type.2. Check type compatibility for x=5.3. X2 not declared!4. Verify type of ’>’ is boolean.5. Check type compatibility for ‘+’.6. Check type compatibility between x and

int, for assignment.

Page 22: Overview of Compilation

Code Generation

• Goal: Convert syntax tree to target code.

Target code could be:• Machine language.• Assembly language.• Quadruples for a fictional machine:

• label• opcode• operands (1 or 2)

Page 23: Overview of Compilation

Code Generation

• Example: • “pc” on UNIX generates assembly code• “pi” on UNIX generates code for the “p”

machine, which is interpreted by… an interpreter.

• pc: slow compilation, fast running code.• pi: fast compilation, slow running code.

• Method: Traverse the tree again.

Page 24: Overview of Compilation

Code (for a stack machine)

LOAD 5STORE X

LOAD XLOAD 10BGTCOND L1 L2

L1 LOAD XLOAD 1BADDSTORE XGOTO L3

L2 . . .L3

Page 25: Overview of Compilation

Code Optimization

• Goals:• Reduce the size of the target program.• Decrease the running time of the target.

• Note: “Optimization” is a misnomer. Code improvement would be better.

• Two types of optimization:• Peephole optimization (local).• Global optimization (improve loops, etc.).

Page 26: Overview of Compilation

Code Optimization (cont’d)

• Example (from previous slide):

LOAD 5 can be LOAD 5

STORE X replaced STND X

LOAD X with

Store non-destructively, i.e., store in X, but do not destroy value on top of stack.

Page 27: Overview of Compilation

Summary

Parser

Source

Constrainer

Code Generator

Code (for an abstract machine)

Interpreter

Screener

Scanner

Input Output

Table Routines

Error Routines

Tokens

Tokens

Tree

Tree

Page 28: Overview of Compilation

Overview of Compilation

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 2


Recommended