Lecture 2: Compilation and Interpretation (Sections 1.4-1.6) - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 2: Compilation and Interpretation (Sections 1.4-1.6)

Description:

... Preprocessing Linking Portability Programming Environments Overview of Compilation Phases of Compilation Example Lexical Analysis Syntax Analysis: ... – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 17
Provided by: csUncaEd7
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 2: Compilation and Interpretation (Sections 1.4-1.6)


1
Lecture 2 Compilation and Interpretation(Sectio
ns 1.4-1.6)
  • CSCI 431 Programming Languages
  • Fall 2002

A modification of slides developed by Felix
Hernandez-Campos at UNC Chapel Hill
2
From Source Code to Executable Code
program gcd(input, output) var i, j
integer begin read(i, j) while i ltgt j
do if i gt j then i i j else j j
i writeln(i) end.
3
Compilation and Interpretation
  • A compiler is a program that translates
    high-level source programs into target program

4
Mixing Compilation and Interpretation
  • Fuzzy difference
  • A language is interpreted when the initial
    translation is simple
  • A language is compiled when the translation
    process is complicated

5
Preprocessing
  • Macros
  • define ltmacrogt ltreplacement namegt
  • define FALSE 0
  • define max(A,B) ( (A) gt (B) ? (A)(B))

6
Linking
  • Libraries of subroutines

7
Portability
  • Assembly language instead of machine language
  • Intermediate source code

8
Programming Environments
  • Much more than compilers and interpreters
  • Assemblers, debuggers, preprocessors and linkers
  • Editors
  • Pretty printers
  • Style Checkers
  • Version management
  • Profilers
  • Integrated environments
  • Beyond a simple bus error
  • Emacs

9
Overview of Compilation
program gcd(input, output) var i, j
integer begin read(i, j) while i ltgt j
do if i gt j then i i j else j j
i writeln(i) end.
10
Phases of Compilation
11
Example
  • From Scotts class notes
  • Desk calculator language
  • Example program
  • read A
  • read B
  • sum A B
  • write sum
  • write sum / 2

12
Lexical Analysis
  • Tokens
  • id letter ( letter digit ) except
    "read" and "write"
  • literal digit digit
  • "", "", "-", "", "/", "(", ")
  • end of file

13
Syntax Analysis The Grammar
  • Grammar in EBNF
  • ltpgmgt -gt ltstatement listgt
  • ltstmt listgt -gt ltstmt listgt ltstmtgt E
  • ltstmtgt -gt id ltexprgt read ltidgt write
    ltexprgt
  • ltexprgt -gt lttermgt ltexprgt ltadd opgt lttermgt
  • lttermgt -gt ltfactor lttermgt ltmult opgt
    ltfactor
  • ltfactorgt -gt ( ltexprgt ) id literal
  • ltadd opgt -gt -
  • ltmult opgt -gt /

14
Syntactic and Semantic Analysis
  • Derive parse tree in class
  • Derive abstract syntax tree in class

15
Code Generation
  • Intermediate code
  • read
  • pop A
  • read
  • pop B
  • push A
  • push B
  • add
  • pop sum
  • push sum
  • write
  • push sum
  • push 2
  • div
  • write

16
Code Generation
  • Target code
  • .data
  • A .long 0
  • B .long 0
  • sum .long 0
  • .text
  • main jsr read
  • movl d0,d1
  • movl d1,A
  • jsr read
  • movl d0,d1
  • movl d1,B
  • movl A,d1

movl B,d2 addl
d1,d2 movl d1,sum movl
sum,d1 movl d1,d0
jsr write movl sum,d1
movl 2,d2 divsl d1,d2
movl d1,d0 jsr write
Write a Comment
User Comments (0)
About PowerShow.com