Compilers Structure of a Compiler Alex Aiken Intro to Compilers - - PowerPoint PPT Presentation

compilers
SMART_READER_LITE
LIVE PREVIEW

Compilers Structure of a Compiler Alex Aiken Intro to Compilers - - PowerPoint PPT Presentation

Compilers Structure of a Compiler Alex Aiken Intro to Compilers 1. Lexical Analysis 2. Parsing 3. Semantic Analysis 4. Optimization 5. Code Generation Alex Aiken Intro to Compilers First step: recognize words. Smallest unit above


slide-1
SLIDE 1

Alex Aiken

Compilers

Structure of a Compiler

slide-2
SLIDE 2

Alex Aiken

Intro to Compilers

  • 1. Lexical Analysis
  • 2. Parsing
  • 3. Semantic Analysis
  • 4. Optimization
  • 5. Code Generation
slide-3
SLIDE 3

Alex Aiken

Intro to Compilers

  • First step: recognize words.

– Smallest unit above letters This is a sentence.

slide-4
SLIDE 4

Alex Aiken

Intro to Compilers ist his ase nte nce

slide-5
SLIDE 5

Alex Aiken

Intro to Compilers

  • Lexical analysis divides program text into “words” or

“tokens” if x == y then z = 1; else z = 2;

slide-6
SLIDE 6

Alex Aiken

Intro to Compilers

  • Once words are understood, the next step is to understand

sentence structure

  • Parsing = Diagramming Sentences

– The diagram is a tree

slide-7
SLIDE 7

Alex Aiken

Intro to Compilers This line is a longer sentence verb article noun article adjective noun subject

  • bject

sentence

slide-8
SLIDE 8

Alex Aiken

Intro to Compilers if x == y then z = 1; else z = 2; if-then-else x y z 1 z 2 == assign relation assign predicate else-stmt then-stmt

slide-9
SLIDE 9

Alex Aiken

Intro to Compilers

  • Once sentence structure is understood, we can try to

understand “meaning” – This is hard!

  • Compilers perform limited semantic analysis to catch

inconsistencies

slide-10
SLIDE 10

Alex Aiken

Intro to Compilers

  • Example:

Jack said Jerry left his assignment at home.

  • Even worse:

Jack said Jack left his assignment at home?

slide-11
SLIDE 11

Alex Aiken

Intro to Compilers

  • Programming languages

define strict rules to avoid such ambiguities { int Jack = 3; { int Jack = 4; cout << Jack; } }

slide-12
SLIDE 12

Alex Aiken

Intro to Compilers

  • Compilers perform many semantic checks besides

variable bindings

  • Example:

Jack left her homework at home.

  • A “type mismatch” between her and Jack; we know

they are different people

slide-13
SLIDE 13

Alex Aiken

Intro to Compilers

  • Optimization has no strong counterpart in English

– But a little bit like editing

  • Automatically modify programs so that they

– Run faster – Use less memory

slide-14
SLIDE 14

Alex Aiken

Intro to Compilers X = Y * 0 is the same as X = 0

slide-15
SLIDE 15

Alex Aiken

Intro to Compilers

  • Produces assembly code (usually)
  • A translation into another language

– Analogous to human translation

slide-16
SLIDE 16

Alex Aiken

Intro to Compilers

  • The overall structure of almost every compiler

adheres to our outline

  • The proportions have changed since FORTRAN