Introduction to Compiling Chapter 1 1 Compiler Construction - - PowerPoint PPT Presentation

introduction to compiling
SMART_READER_LITE
LIVE PREVIEW

Introduction to Compiling Chapter 1 1 Compiler Construction - - PowerPoint PPT Presentation

Introduction to Compiling Chapter 1 1 Compiler Construction Introduction to Compiling To Do Read Chapter 1 of the Dragon book Begin work on Assignment 1 2 Compiler Construction Introduction to Compiling What is a compiler? A


slide-1
SLIDE 1

Introduction to Compiling

Chapter 1

Compiler Construction Introduction to Compiling

1

slide-2
SLIDE 2

To Do

  • Read Chapter 1 of the Dragon book
  • Begin work on Assignment 1

Compiler Construction Introduction to Compiling

2

slide-3
SLIDE 3

What is a compiler?

  • A program that reads a program written in one language (source) and

translates it into an equivalent program in another language (target)

  • Error messages are generated if necessary

Source program Error messages Target program Compiler

Compiler Construction Introduction to Compiling

3

slide-4
SLIDE 4

Analysis-Synthesis Model

Compilation has two parts

  • Analysis:

source code −

→ intermediate representation

  • Synthesis:

intermediate representation −

→ target code

position = initial + rate * 60; = position + initial * 60 rate

Compiler Construction Introduction to Compiling

4

slide-5
SLIDE 5

Analysis

  • The intermediate representation

is usually in the form of a tree

  • This tree can be traversed
  • Analysis of various types can be

performed during this traversal Type checking

position + initial * 60 rate =

Identifier Offset Type

position int initial

4

float rate

8

int

Compiler Construction Introduction to Compiling

5

slide-6
SLIDE 6

Other Tools that Perform Analysis

  • Structure editors
  • Pretty printers
  • Static checkers
  • Interpreters

Compiler Construction Introduction to Compiling

6

slide-7
SLIDE 7

Compiler Context

The compiler interacts with other programs in a complete language processing system

Compiler Assembler Loader/Linker Preprocessor Absolute machine code Library and relocatable

  • bject files

Relocatable machine code Target assembly program Embellished source code Source code

Compiler Construction Introduction to Compiling

7

slide-8
SLIDE 8

Types of Analysis

  • Lexical analysis
  • Syntax analysis
  • Semantic analysis

Compiler Construction Introduction to Compiling

8

slide-9
SLIDE 9

Lexical Analysis

  • Also called scanning
  • Input stream:

position = initial + rate * 60;

  • Token stream:
  • 1. Identifier token position
  • 2. Assignment token =
  • 3. Identifier token initial
  • 4. Plus token +
  • 5. Identifier token rate
  • 6. Multiplication token *
  • 7. Number token 60
  • 8. Semicolon token ;

Compiler Construction Introduction to Compiling

9

slide-10
SLIDE 10

Syntax Analysis

  • Also called parsing
  • Constructs a parse tree

rate 60 number identifier * expression initial identifier expression + expression expression = position = initial + rate * 60; position identifier statement assignment expression ;

Compiler Construction Introduction to Compiling

10

slide-11
SLIDE 11

Rules

1. assignment statement − → identifier = expression ; 2. expression − → expression + expression 3. expression − → expression * expression 4. expression − → identifier 5. expression − → number 6. identifier − → letter letter* 7. letter − →

a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y |z

8. number − → digit digit* 9. digit − →

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

rate 60 number identifier * expression initial identifier expression + expression expression = position = initial + rate * 60; position identifier statement assignment expression ;

Compiler Construction Introduction to Compiling

11

slide-12
SLIDE 12

Rules

  • Observe that the rules are recursive
  • Lexical constructs do not require recursion

– Regular expressions – Identifying identifiers

  • Syntactic constructs often do require recursion

– Context-free grammars – Expressions

Compiler Construction Introduction to Compiling

12

slide-13
SLIDE 13

Semantic Analysis

  • Type checking
  • Elements of the parse tree are “decorated” with additional information
  • A syntactically legal statement may be semantically illegal
  • What is the meaning of position?

position = initial + rate * 60;

Compiler Construction Introduction to Compiling

13

slide-14
SLIDE 14

Phases of Compiling

Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Symbol table manager Error handler Code generator Code optimizer Source program Target program

Tokens Abstract syntax tree Abstract syntax tree Three−address code Three−address code

Compiler Construction Introduction to Compiling

14

slide-15
SLIDE 15

Front End vs. Back End

Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Symbol table manager Error handler Code generator Code optimizer Source program Target program

Tokens Abstract syntax tree Abstract syntax tree Three−address code Three−address code

Back end Front end

Compiler Construction Introduction to Compiling

15

slide-16
SLIDE 16

Front End vs. Back End

  • Front end

– Target language independent – Machine-independent – Can be used for multiple platforms

  • Back end

– Source language independent – Machine dependent – Can be used for multiple languages

Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Symbol table manager Error handler Code generator Code optimizer Source program Target program

Tokens Abstract syntax tree Abstract syntax tree Three−address code Three−address code

Back end Front end

Compiler Construction Introduction to Compiling

16