Nawroz University

CoIIege Computer Science
Department of Computer Science
Stage Second
Þrogrammlng Languages
0.9:70
Topic
·evlew
·CompllaLlon
·lnLerpreLaLlon
·Assembler
·CompllaLlon Phases
·MachlneŴdependenL and MachlneŴlndependenL
·Summary
evlew
CompllaLlon
A Compller ls a program LhaL reads a program wrlLLen
ln one language (Lhe source language) and LranslaLes lL
lnLo an equlvalenL program ln anoLher language (Lhe
LargeL language)Ŧ
[u = u + b ;j
Muchine code
[1000101001111j
lnLerpreLaLlon
lnLerpreLersť A program LhaL LranslaLes a
user's hlghŴlevel program (Source Þrogram)
lnLo machlne program ( 1argeL Þrogram) aL
Lhe same LlmeŦ
Assembler
A program LhaL converLs a program
wrlLLen ln assembly language lnLo
machlne code
Assembler
Assembly Language Low Level Language
Machlne Code
Preprocessor
Compiler
Assembler
Loader /
Linker-editor
Source Program
Modified Source Program
Target Assembly Program
Relocatable Machine Code
Absolute Machine Code
Library , Relocatable
Object Files
Language
Þrocesslng
SysLem
#lncludeƼlosLreamŦhƽ
vold maln()
žlnL aŤ clnƽƽaŤ couLƼƼaŤſ
Comp||at|on
Þhases
!ase 1- LexicaI AnaIysis:
· Ìn a compiler, linear analysis is called lexical
analysis or scanning. Example
position = initiaI + rate * 60
· would be grouped into the following tokens:
position = initiaI + rate * 60;
Lexeme Token
Position
The Ìdentifier position.
= The Assignment Symbol
initial The Ìdentifier Ìnitial.
+ The Plus Sign
rate The Ìdentifier Rate.
* The Multiplication Sign
60 The Number 60
; The Semi-colon Symbol
!ase 2- Syntax AnaIysis:
· Hierarchical analysis is called parsing or
syntax analysis, it involves grouping the
tokens of the source program into
grammatical phrases that are used by the
compiler to synthesize output.
· the grammatical phrases of the source
program are represented by a parse tree .
!arse tree for:
position =initiaI + rate*60;
Assignment
Statement
=
identifier
position
Expression
Expression
Expression
Expression
+
*
identifier
rate
identifier
initial
Number
60
Expression
!ase 3- Semantic AnaIysis:
· checks the source program for semantic errors and
gathers type information for the subsequent code-
generation phase.
· Ì uses the hierarchical structure determined by the
syntax-analysis phase to identify the operators and
operands of expressions and statements. An important
component of semantic analysis is type checking.
· Here the compiler checks that each operator has
operands that are permitted by the source language
specification.
For example, a real number is used to index
an or when a binary arithmetic operator is
applied to an integer and real.
!osition
+
initial
=
rate
*
60
!osition
+
initial
=
rate
*
60.0
inttoreal
Convert by semantic
anaIysis
!ase 4- Intermediate Code Generator:
· After syntax and semantic analysis, some
compilers generate an explicit intermediate
representation of the source program.
· We can think of this intermediate
representation as a program for an abstract
machine. This intermediate representation
should have two important properties;
· it should be easy to produce, and easy
to transIate into the target program.
temp1 = inttoreal(60)
temp2 = id3 * temp1
temp3 = id2 + temp2
id1 =temp3
!ase 5- Code Optimization :
· The code optimization phase attempts to
improve the intermediate code, so that
faster-running machine code will result.
temp1 = inttoreal(60)
temp2 = id3 * temp1 temp1 = id3 * 60.0
temp3 = id2 + temp2 id1 = id2+ temp1
id1 =temp3
After perform
Optimization
!ase 6- Code Generation:
· The final phase of the compiler is the
generation of target code, consisting
normally of relocatable machine code or
assembly code.
· Memory locations are selected for each of
the variables used by the program.
Then, intermediate instructions are
each translated into a sequence of
machine instructions that perform the
same task.
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
|t|ona| Comp||at|on 1erm|no|og|es
· Loa mo|e (execuLable lmage)ť Lhe user and
sysLem code LogeLher
· L|nk|ng an |oa|ngť Lhe process of collecLlng
sysLem program unlLs and llnklng Lhem Lo a
user program
,ach|neŴepenent
an
,ach|neŴ|nepenent
,ach|neŴepenent
· ls a Lerm for appllcaLlon sofLware
LhaL runs only on a parLlcular Lype of
compuLerŦ
mach|neŴ|nepenent
· A Lerm applled Lo sofLware LhaL ls noL
dependenL on Lhe properLles of a
parLlcular machlneţ and can Lherefore be
used on any machlneŦ
· Also called mocbloeŴloJepeoJeotţ or
ctossŴplotfotm ţ pottobleŦ
eclaraLlons
and 1ypes
aLa 1ypes
· CompuLers manlpulaLe sequences of blLs
· 8uL mosL programs manlpulaLe more general
daLa
÷ numbers
÷ SLrlng
÷ LlsLs
÷ ŧ
aLa 1ypes
· Þrogrammlng languages provlde daLa Lypes
LhaL ralse Lhe level of absLracLlon from blLs Lo
daLa
÷ 8uL compuLer hardware only knows abouL blLs!
JhaL ls a Lype?
÷o typeť ls o set of voloes
÷o typeť ls bolltŴlo type ot o
composlte type
ClasslflcaLlon of 1ypesť 8ullLŴln 1ypes
· 8ullLŴln
÷Ŧ–Ŧ booleanţ characLerţ lnLegerţ real
(floaL)
· 1helr lmplemenLaLlon varles across
languages
8ullLŴln 1ypesť numerlc 1ypes
· MosL languages supporL lnLegers and floaLs
· Some languages supporL oLher numerlc Lypes
÷ Complex numbers (eŦ–Ŧ orLran)
÷ Slgned and unslgned lnLegers (eŦ–Ŧ C)
· Some languages dlsLlngulsh numerlc Lypes
dependlng on Lhelr preclslon
÷ Slngle vsŦ double preclslon numbers
· C's int (4 byLes) and long (8 byLes)
ClasslflcaLlon of 1ypesť LnumeraLlons
· LnumeraLlons lmprove program readablllLy
and error checklng
· 1hey were flrsL lnLroduced ln Þascal
÷ type weekday = (sun,mon,tue,wed,thu,fri,sat);
÷ 1hey deflne an orderţ so Lhey can be used ln
enumeraLlonŴconLrolled loops
÷ 1he same feaLure ls avallable ln C
· enum weekday ,sun, mon, tue, wed, thu, fri, sat,;
ClasslflcaLlon of 1ypesťComposlLe 1ypes
· ComposlLe o f one or more slmpler Lypes
· Lxamples
÷ ecords
÷ varlanL ecords
÷ Arrays
÷ SeLs
÷ ÞolnLers
÷ LlsLs
÷ lles
varlables
· A varlable ls an absLracLlon of a memory cell
· varlables can be characLerlzed as a sexLuple of
aLLrlbuLesť
÷ name
÷ Address
÷ value
÷ 1ype
÷ LlfeLlme
÷ Scope
varlables ALLrlbuLes
· -ame Ŷ name of Lhe varlableŦ
· ress Ŵ Lhe memory address wlLh whlch lL ls
assoclaLed
÷A varlable may have dlfferenL addresses aL
dlfferenL Llmes durlng execuLlon
÷lf Lwo varlable names can be used Lo access
Lhe same memory locaLlonţ Lhey are called
allases
varlables ALLrlbuLes
· 1ype Ŵ deLermlnes Lhe range of values of
varlables and Lhe seL of operaLlons LhaL
are deflned for values of LhaL LypeŦ
· Ia|e Ŵ Lhe conLenLs of Lhe locaLlon wlLh
whlch Lhe varlable ls assoclaLed
varlables ALLrlbuLes
· ||fet|me of a varlable ls Lhe Llme durlng
whlch lL ls bound Lo a parLlcular memory
cell
· scope of a varlable ls Lhe range of
sLaLemenLs over whlch lL ls vlslble
blndlng
· A bloJlo– ls an assoclaLlonţ such as beLween an
aLLrlbuLe and an enLlLyţ or beLween an operaLlon
and a symbolŦ
· loJlo– ls Lhe assoclaLlon of aLLrlbuLes wlLh
program enLlLles
· loJlo– tlme ls Lhe Llme aL whlch a blndlng Lakes
placeŦ
vlslblllLy
· scope ls used Lo deflne Lhe exLenL of
lnformaLlon hldlngŸLhaL lsţ Lhe vlslblllLy or
accesslblllLy of varlables from dlfferenL parLs
of Lhe programŦ
· vlslblllLyť publlcţ prlvaLeţ proLecLedŦ
varlable lnlLlallzaLlon
· 1he blndlng of a varlable Lo a value aL Lhe Llme
lL ls bound Lo sLorage ls called loltlollzotloo
· lnlLlallzaLlon ls ofLen done on Lhe declaraLlon
sLaLemenLţ eŦgŦţ ln C#
int sum = 0;
1ype Checklng
· 1ype checkinq ls Lhe process of ensurlng LhaL
a program obeys Lhe language's Lype
compaLlblllLy rules
· 1ype checkinq ls Lhe acLlvlLy of ensurlng LhaL
Lhe operands of an operaLor are of
compaLlble Lypes
· A compotlble type ls legal for Lhe operaLor
arbage collection
· Every modern programming language allows
programmers to allocate new storage
dynamically
÷ New records, arrays, objects, etc.
· Every modern language needs facilities for
reclaiming and recycling the storage used by
programs
arbage collection
· Ìt's usually the most complex aspect of the run-
time system for any modern language (Java, C#,
ML, Lisp, Scheme, .)
· What is garbage?
· Ìs reclaiming and recycling the storage
used by programs.
÷ A value is garbage if it will not be used in any
subsequent computation by the program
end

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.