0% found this document useful (0 votes)
10 views100 pages

PPL Units (1,2&3)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views100 pages

PPL Units (1,2&3)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

Principles of Programming Language

UNIT-I
Preliminaries

Topics
1. ReasonsforStudyingConceptsofProgrammingLanguages
2. ProgrammingDomains
3. Language EvaluationCriteria
4. Influences on LanguageDesign
5. Language Categories
6. Language DesignTrade-Offs
7. ImplementationMethods
8. ProgrammingEnvironments
`
Background

 ―Frankly, we didn‘t have the vaguest idea how the thing [FORTRAN language
andcompiler]wouldworkoutindetail.…Westruckoutsimplytooptimizetheobject
program,therunningtime,becausemostpeopleatthattimebelievedyoucouldn‘tdo that kind of thing. They
believed that machined-coded programs would be so
inefficientthatitwouldbeimpracticalformanyapplications.‖
-John Backus

 Unexpected successes are common – the browser is another example of an unexpectedsuccess

Reasons for Studying Concepts of ProgrammingLanguages

 Increased ability to expressideas


 Improved background for choosing appropriatelanguages
 Increased ability to learn newlanguages
 Better understanding of significance ofimplementation
 Overall advancement ofcomputing

ProgrammingDomains

• Scientificapplications
– Large number of floating pointcomputations
– Fortran
• Businessapplications
– Produce reports, use decimal numbers andcharacters
– COBOL
• Artificialintelligence
– Symbols rather than numbersmanipulated
– LISP
• Systemsprogramming
– Need efficiency because of continuoususe
– C
• WebSoftware
– Eclecticcollectionoflanguages:markup(e.g.,XHTML),scripting(e.g., PHP), general-
purpose (e.g.,Java)

Language EvaluationCriteria

• Readability:theeasewithwhichprogramscanbereadandunderstood
• Writability:theeasewithwhichalanguagecanbeusedtocreateprograms
• Reliability:conformancetospecifications(i.e.,performstoitsspecifications)
• Cost: the ultimate totalcost
Evaluation Criteria: Readability
• Overallsimplicity
– A manageable set of features andconstructs
– Fewfeaturemultiplicity(meansofdoingthesameoperation)
– Minimal operatoroverloading
• Orthogonality
– Arelativelysmallsetofprimitiveconstructscanbecombinedina relatively small
number ofways
– Every possible combination islegal
• Controlstatements
– Thepresenceofwell-knowncontrolstructures(e.g.,whilestatement)
• Data types andstructures
– Thepresenceofadequatefacilitiesfordefiningdatastructures
• Syntaxconsiderations
– Identifier forms: flexiblecomposition
– Specialwordsandmethodsofformingcompoundstatements
– Formandmeaning:self-descriptiveconstructs,meaningfulkeywords
Evaluation Criteria: Writability
• Simplicity andorthogonality
– Fewconstructs,asmallnumberofprimitives,asmallsetofrulesfor combiningthem
• Support forabstraction
– Theabilitytodefineandusecomplexstructuresoroperationsinways that allow details to
beignored
• Expressivity
– Asetofrelativelyconvenientwaysofspecifyingoperations
– Example:theinclusionofforstatementinmanymodernlanguages
Evaluation Criteria: Reliability
• Typechecking
– Testing for typeerrors
• Exceptionhandling
– Intercept run-time errors and take correctivemeasures
• Aliasing
– Presenceoftwoormoredistinctreferencingmethodsforthesame memorylocation
• Readability andwritability
– A language that does not support ―natural‖ ways of expressing an
algorithmwillnecessarilyuse―unnatural‖approaches,andhencereducedreliability
Evaluation Criteria: Cost
• Training programmers to uselanguage
• Writing programs (closeness to particularapplications)
• Compilingprograms
• Executingprograms
• Languageimplementationsystem:availabilityoffreecompilers
• Reliability: poor reliability leads to highcosts
• Maintainingprograms
Evaluation Criteria: Others
• Portability
• Neural Networks - an OverviewTheeasewithwhichprogramscanbemovedfromoneimplementationto
another
• Generality
– The applicability to a wide range ofapplications
• Well-definedness
– Thecompletenessandprecisionofthelanguage‘sofficialdefinition
Influences on LanguageDesign

• ComputerArchitecture
– Languages are developed around the prevalent computer architecture, known as the von
Neumannarchitecture
• ProgrammingMethodologies
– New software development methodologies (e.g., object-oriented software development) led
to new programming paradigms and by extension, new programming languages
Computer Architecture Influence
• Well-known computer architecture: VonNeumann
• Imperativelanguages,mostdominant,becauseofvonNeumanncomputers
– Data and programs stored inmemory
– Memory is separate fromCPU
– Instructions and data are piped from memory toCPU
– Basis for imperativelanguages
• Variables model memorycells
• Assignment statements modelpiping
• Iteration isefficient
The von NeumannArchitecture
Programming Methodologies Influences
• 1950sandearly1960s:Simpleapplications;worryaboutmachineefficiency
• Late1960s:Peopleefficiencybecameimportant;readability,bettercontrol structures
– structuredprogramming
– top-down design and step-wiserefinement
• Late 1970s: Process-oriented todata-oriented
– dataabstraction
• Middle 1980s: Object-orientedprogramming
– Data abstraction + inheritance +polymorphism
LanguageCategories

• Imperative
– Centralfeaturesarevariables,assignmentstatements,anditeration
– Examples: C,Pascal
• Functional
– Mainmeansofmakingcomputationsisbyapplyingfunctionstogiven parameters
– Examples: LISP,Scheme
• Logic
– Rule-based(rulesarespecifiedinnoparticularorder)
– Example:Prolog
• Object-oriented
– Data abstraction, inheritance, latebinding
– Examples: Java,C++
• Markup
– New;notaprogrammingperse,butusedtospecifythelayoutof information in
Webdocuments
– Examples: XHTML,XML

Language DesignTrade-Offs

• Reliability vs. cost ofexecution


– Conflictingcriteria
– Example:Javademandsallreferencestoarrayelementsbecheckedfor
properindexingbutthatleadstoincreasedexecutioncosts
• Readability vs.writability
– Another conflictingcriteria
– Example:APLprovidesmanypowerfuloperators(andalargenumberof
newsymbols),allowingcomplexcomputationstobewritteninacompactprogrambut at the cost of
poorreadability
• Writability (flexibility) vs.reliability
– Another conflictingcriteria
– Example:C++pointersarepowerfulandveryflexiblebutnotreliably used

ImplementationMethods

• Compilation
– Programs are translated into machinelanguage
• Pure Interpretation
– Programsareinterpretedbyanotherprogramknownasaninterpreter
• Hybrid ImplementationSystems
– A compromise between compilers and pureinterpreters

Layered View ofComputer

The operating system and language implementation are layered over Machine interface of a computer

Compilation
• Translate high-level program (source language) into machine code (machine language)
• Slow translation, fastexecution
• Compilation process has severalphases:
– lexical analysis: converts characters in the source program into lexical units
– syntaxanalysis:transformslexicalunitsintoparse treeswhichrepresent the syntactic
structure ofprogram
– Semantics analysis: generate intermediatecode
– code generation: machine code isgenerated

The Compilation Process


Additional Compilation Terminologies

• Loadmodule(executableimage):theuserandsystemcodetogether
• Linkingandloading:theprocessofcollectingsystemprogramandlinkingthem to userprogram

Execution of Machine Code


• Fetch-execute-cycle (on a von Neumann architecture) initialize the
programcounter
repeat forever
fetchtheinstructionpointedbythecounter increment
thecounter
decode the instruction execute the
instruction
end repeat
Von Neumann Bottleneck

• Connectionspeedbetweenacomputer‘smemoryanditsprocessordetermines the speed of acomputer


• Program instructions often can be executed a lot faster than the above connection speed; the
connection speed thus results in abottleneck
• Known as von Neumann bottleneck; it is the primary limiting factor in the speed ofcomputers
Pure Interpretation
• Notranslation
• Easierimplementationofprograms(run-timeerrorscaneasilyandimmediately displayed)
• Slowerexecution(10to100timesslowerthancompiledprograms)
• Often requires morespace
• Becoming rare on high-levellanguages
Significant comeback with some Web scripting languages (e.g., JavaScript)
Hybrid Implementation Systems
• A compromise between compilers and pureinterpreters
• Ahigh-levellanguageprogramistranslatedtoanintermediatelanguagethat allows easyinterpretation
• Faster than pureinterpretation
• Examples
– Perlprogramsarepartiallycompiledtodetecterrorsbeforeinterpretation
– InitialimplementationsofJavawerehybrid;theintermediateform,byte
code,providesportabilitytoanymachinethathasabytecodeinterpreterandarun- time system (together, these
are called Java VirtualMachine)
Just-in-Time Implementation Systems
• Initially translate programs to an intermediatelanguage
• Then compile intermediate language into machinecode
• Machine code version is kept for subsequentcalls
• JIT systems are widely used for Javaprograms
• .NET languages are implemented with a JITsystem

Preprocessors
• Preprocessormacros(instructions)arecommonlyusedtospecifythatcodefrom another file is to
beincluded
• A preprocessor processes a program immediately before the programis compiled to
expand embedded preprocessormacros
• A well-known example: Cpreprocessor
– expands #include, #define, and similarmacros
UNIT-2
Syntax and Semantics

Topics
Introduction
TheGeneralProblemofDescribingSyntax Formal
Methods of Describing Syntax AttributeGrammars
Describing the Meanings of Programs: Dynamic Semantics

Introduction
Syntax:theformorstructureoftheexpressions,statements,andprogram units
Semantics:themeaningoftheexpressions,statements,andprogramunits Syntax and
semantics provide a language‘sdefinition
o Users of a languagedefinition
 Other languagedesigners
 Implementers
 Programmers (the users of thelanguage)

TheGeneralProblemofDescribingSyntax:Terminology
Asentenceisastringofcharactersoversomealphabet A language is
a set ofsentences
Alexemeisthelowestlevelsyntacticunitofalanguage(e.g.,*,sum,begin) A token is a category
of lexemes (e.g.,identifier)

Formal Definition of Languages


Recognizers
o Arecognitiondevicereadsinputstringsofthelanguageanddecides
whethertheinputstringsbelongtothelanguage
o Example: syntax analysis part of acompiler
o Detailed discussion in Chapter4
Generators
o A device that generates sentences of alanguage
o Onecandetermineifthesyntaxofaparticularsentenceiscorrectby comparing it to the
structure of thegenerator

Formal Methods of DescribingSyntax


Backus-Naur Form and Context-Free Grammars
o Most widely known method for describing programming language syntax ExtendedBNF
o Improves readability and writability ofBNF Grammars
andRecognizers
BNF and Context-FreeGrammars
o Context-FreeGrammars
o Developed by Noam Chomsky in themid-1950s
o Languagegenerators,meanttodescribethesyntaxofnaturallanguages
o Defineaclassoflanguagescalledcontext-freelanguages

Backus-Naur Form (BNF)


Backus-Naur Form (1959)
o Invented by John Backus to describe Algol58
o BNF is equivalent to context-freegrammars
o BNF is ametalanguageused to describe anotherlanguage
o In BNF, abstractions are used to represent classes of syntactic structures--
theyactlikesyntacticvariables(alsocallednonterminal symbols)

BNF Fundamentals

Non-terminals:BNFabstractions
Terminals:lexemesandtokens
Grammar: a collection of rules
o Examples of BNFrules:
<ident_list> → identifier | identifer, <ident_list>
<if_stmt> → if <logic_expr>then <stmt>

BNF Rules
A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of
terminal and nonterminal symbols
A grammar is a finite nonempty set of rules
Anabstraction(ornonterminalsymbol)canhavemorethanoneRHS
<stmt> <single_stmt>
|begin <stmt_list>end
Describing Lists
Syntactic lists are described using recursion
<ident_list> ident
| ident, <ident_list>
Aderivationisarepeatedapplicationofrules,startingwiththestartsymbol and ending with a
sentence (all terminalsymbols)

An Example Grammar
<program> <stmts>
<stmts> <stmt>|<stmt> ;<stmts>
<stmt> <var> =<expr>
<var> a|b |c|d
<expr> <term>+<term>|<term>-<term>
<term> <var>|const

An example derivation
<program> =><stmts> =><stmt>
<var> = <expr> => a =<expr>
a = <term> + <term>
a = <var> + <term>
a = b + <term>
a = b + const

Derivation
Every string of symbols in the derivation is a sentential form
Asentenceisasententialformthathasonlyterminalsymbols
A leftmost derivation is one in which the leftmost nonterminal ineach
sentential form is the one that is expanded
A derivation may be neither leftmost nor rightmost

Parse Tree
A hierarchical representation of a derivation

Ambiguity in Grammars
Agrammarisambiguousiffitgeneratesasententialformthathastwoormore distinct parsetrees

An Ambiguous Expression Grammar


An Unambiguous ExpressionGrammar
Ifweusetheparsetreetoindicateprecedencelevelsoftheoperators,wecannot haveambiguity

<expr> <expr> - <term>|<term>


<term> <term>/ const|const

Associativity of Operators
Operator associativity can also be indicated by a grammar

<expr> -><expr> + <expr>| const (ambiguous)


<expr> -><expr> + const | const (unambiguous)
Extended BNF
Optional parts are placed in brackets ([ ])
<proc_call> -> ident [(<expr_list>)]
AlternativepartsofRHSsareplacedinsideparenthesesandseparatedvia verticalbars
<term> → <term> (+|-) const
Repetitions (0 or more) are placed inside braces ({ })
<ident> → letter {letter|digit}
BNF and EBNF
BNF
<expr> <expr> +<term>
| <expr>-<term>
| <term>
<term> <term> *<factor>
| <term>/ <factor>
| <factor>
EBNF
<expr> <term>{(+ |-) <term>}
<term> <factor> {(* |/) <factor>}

AttributeGrammars
Context-freegrammars(CFGs)cannotdescribeallofthesyntaxofprogramming languages
AdditionstoCFGstocarrysomesemanticinfoalongparsetrees Primary value of
attribute grammars(AGs):
o Static semanticsspecification
o Compiler design (static semanticschecking)
Attribute Grammars : Definition
An attribute grammar is a context-free grammar G = (S, N, T, P) with the
following additions:
o ForeachgrammarsymbolxthereisasetA(x)ofattributevalues
o Eachrulehasasetoffunctionsthatdefinecertainattributesofthe nonterminals in therule
o Eachrulehasa(possiblyempty)setofpredicatestocheckforattribute consistency
o Let X0 X1 ... Xn be arule
o FunctionsoftheformS(X0)=f(A(X1),...,A(Xn))definesynthesized attributes
o FunctionsoftheformI(Xj)=f(A(X0),...,A(Xn)),fori<=j<=n,define
inherited attributes
o Initially, there are intrinsic attributes on theleaves
Attribute Grammars: Example
Syntax
<assign> -><var> = <expr>
<expr> -><var> + <var>| <var>
<var> A | B |C
actual_type:synthesizedfor<var>and<expr>expected_type
: inherited for<expr>

Syntaxrule:<expr> <var>[1]+<var>[2]

Semantic rules:
<expr>.actual_type <var>[1].actual_type
Predicate:
<var>[1].actual_type == <var>[2].actual_type
<expr>.expected_type == <expr>.actual_type

Syntaxrule:<var> id
Semantic rule:
<var>.actual_type lookup(<var>.string)

How are attribute values computed?


o Ifallattributeswereinherited,thetreecouldbedecoratedintop-down order.
o Ifallattributesweresynthesized,thetreecouldbedecoratedinbottom- uporder.
o Inmanycases,bothkindsofattributesareused,anditissome combinationoftop-
downandbottom-upthatmustbeused.

<expr>.expected_type inherited fromparent

<var>[1].actual_type lookup(A)
<var>[2].actual_type lookup(B)
<var>[1].actual_type =? <var>[2].actual_type

<expr>.actual_type <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type

Semantics
Thereisnosinglewidelyacceptablenotationorformalismfordescribing semantics
OperationalSemantics
o Describethemeaningofaprogrambyexecutingitsstatementsona machine, either
simulated or actual. The change in the state of the machine (memory, registers, etc.)
defines the meaning of thestatement
Touseoperationalsemanticsforahigh-levellanguage,avirtualmachineis needed
Ahardwarepureinterpreterwouldbetooexpensive
Asoftwarepureinterpreteralsohasproblems:
o Thedetailedcharacteristicsoftheparticularcomputerwouldmake actions difficult
tounderstand
o Such a semantic definition would be machine-dependent
Operational Semantics
A better alternative: A completecomputersimulation The process:
o Buildatranslator(translatessourcecodetothemachinecodeofan idealizedcomputer)
o Buildasimulatorfortheidealizedcomputer Evaluation
of operationalsemantics:
o Good if used informally (language manuals,etc.)
o Extremelycomplexifusedformally(e.g.,VDL),itwasusedfordescribing semantics ofPL/I.

Axiomatic Semantics
o Based on formal logic (predicatecalculus)
o Original purpose: formal programverification
o Approach:Defineaxiomsorinferencerulesforeachstatementtypein the language (to
allow transformations of expressions to other expressions)
o The expressions are calledassertions

Axiomatic Semantics
Anassertionbeforeastatement(aprecondition)statestherelationshipsand
constraintsamongvariablesthataretrueatthatpointinexecution
An assertion following a statement is a postcondition
Aweakestpreconditionistheleastrestrictivepreconditionthatwillguarantee thepostcondition

Pre-postform:{P}statement{Q}
An example: a = b + 1 {a > 1}

Onepossibleprecondition:{b>10} Weakest
precondition: {b>0}

Programproofprocess:Thepostconditionforthewholeprogramisthedesired result. Work back


through the program to the first statement. If the precondition on the first statement is the same
as the program spec, the program iscorrect.

Anaxiomforassignmentstatements (x =E):
{Qx->E }x=E{Q}

An inference rule for sequences


o For a sequenceS1;S2:
o {P1} S1{P2}
o {P2} S2{P3}

Aninferenceruleforlogicalpretestloops For the


loopconstruct:
{P}whileBdoSend{Q}

Characteristicsoftheloopinvariant
Imustmeetthefollowingconditions:
o P=>I (the loop invariant must be trueinitially)
o {I}B{I} (evaluation of the Boolean mustnot
change the validity of I)
o {IandB}S{I} (I is not changed by executingthe
body of the loop)
o (Iand(notB))=>Q (if I is true and B is false,Q
is implied)
o Theloopterminates (this can be difficult toprove)
TheloopinvariantIisaweakenedversionofthelooppostcondition,anditis also aprecondition.
Imustbeweakenoughtobesatisfiedpriortothebeginningoftheloop,but
whencombinedwiththeloopexitcondition,itmustbestrongenoughtoforce the truth of
thepostcondition

Evaluation of axiomaticsemantics:
o Developingaxiomsorinferencerulesforallofthestatementsina language isdifficult
o Itisagoodtoolforcorrectnessproofs,andanexcellentframeworkfor
reasoningaboutprograms,butitisnotasusefulforlanguageusersand compilerwriters
o Itsusefulnessindescribingthemeaningofaprogramminglanguageis limited for language
users or compilerwriters
Denotational Semantics
o Based on recursive functiontheory
o The most abstract semantics descriptionmethod
o Originally developed by Scott and Strachey(1970)
o Theprocessofbuildingadenotationalspecforalanguage(not necessarilyeasy):
 Defineamathematicalobjectforeachlanguageentity
 Defineafunctionthatmapsinstancesofthelanguageentities
ontoinstancesofthecorrespondingmathematicalobjects
o Themeaningoflanguageconstructsaredefinedbyonlythevaluesofthe program'svariables
o Thedifferencebetweendenotationalandoperationalsemantics:In operational
semantics, the state changes are defined by coded
algorithms;indenotationalsemantics,theyaredefinedbyrigorous
mathematicalfunctions
o Thestateofaprogramisthevaluesofallitscurrentvariables

s={<i1,v1>,<i2,v2>,…,<in,vn>}

o LetVARMAPbeafunctionthat,whengivenavariablenameandastate, returns the current


value of thevariable
VARMAP(ij, s) = vj
Decimal Numbers
o The following denotational semantics description maps decimal numbers as strings of
symbols into numericvalues

<dec_num> 0|1|2|3|4|5|6|7|8|9
| <dec_num> (0 | 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9)

Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9


Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>)
Mdec(<dec_num>'1’)=10*Mdec(<dec_num>)+1

Mdec(<dec_num>'9')=10*Mdec(<dec_num>)+9

Expressions
MapexpressionsontoZ {error}
Weassumeexpressionsaredecimalnumbers,variables,orbinaryexpressions
havingonearithmeticoperatorandtwooperands,eachofwhichcanbean expression

Me(<expr>,s) =
case <expr>of
<dec_num> => Mdec(<dec_num>, s)
<var> =>
if VARMAP(<var>, s) == undefthen
error
else VARMAP(<var>, s)
<binary_expr> =>
if (Me(<binary_expr>.<left_expr>, s) == undef
OR Me(<binary_expr>.<right_expr>, s) =
undef)then
error
else
if(<binary_expr>.<operator>==‗+‘then
Me(<binary_expr>.<left_expr>, s)+
Me(<binary_expr>.<right_expr>, s) else
Me(<binary_expr>.<left_expr>, s) *
Me(<binary_expr>.<right_expr>,s)
...

Assignment Statements
o Maps state sets to statesets

Ma(x :=E,s) =
ifMe(E,s)==error
thenerror
else s‘ = {<i1‘,v1‘>,<i2‘,v2‘>,...,<in‘,vn‘>}, where
for j = 1, 2, ..., n,
vj‘ = VARMAP(ij, s) if ij <> x
=Me(E,s)ifij==x
Logical PretestLoops
o Maps state sets to statesets

Ml(whileBdoL,s) =
if Mb(B, s) ==
undefthenerror
elseifMb(B,s)==false thens
elseifMsl(L,s)==error
thenerror
else Ml(while B do L, Msl(L, s))

The meaning of the loop is the value of the program variables after the
statementsintheloophavebeenexecutedtheprescribednumberoftimes, assuming there have
been noerrors
Inessence,theloophasbeenconvertedfromiterationtorecursion,wherethe
recursivecontrolismathematicallydefinedbyotherrecursivestatemapping functions
Recursion,whencomparedtoiteration,iseasiertodescribewithmathematical rigor
Evaluation of denotational semantics
o Can be used to prove the correctness ofprograms
o Provides a rigorous way to think aboutprograms
o Can be an aid to languagedesign
o Has been used in compiler generationsystems
o Becauseofitscomplexity,theyareoflittleusetolanguageusers

Summary
BNF and context-free grammars are equivalent meta-languages
o Well-suitedfordescribingthesyntaxofprogramminglanguages
Anattributegrammarisadescriptiveformalismthatcandescribeboththe syntax and the
semantics of alanguage
Three primary methods of semantics description
o Operation, axiomatic,denotational
UNIT-III
Data types

Topics
• Introduction
• Primitive DataTypes
• Character StringTypes
• User-Defined OrdinalTypes
• ArrayTypes
• AssociativeArrays
• RecordTypes
• UnionTypes
• Pointer and ReferenceTypes
• Names
• Variables
• The Concept ofBinding
• TypeChecking
• StrongTyping
• TypeCompatibility
• Scope
• Scope andLifetime
• ReferencingEnvironments
• NamedConstants

Introduction
• A data type defines a collection of data objects and a set of predefined operations on
thoseobjects
• A descriptor is the collection of the attributes of avariable
• Anobjectrepresentsaninstanceofauser-defined(abstractdata)type
• Onedesignissueforalldatatypes:Whatoperationsaredefinedandhoware theyspecified?
Primitive Data Types
• Almostallprogramminglanguagesprovideasetofprimitivedatatypes
• Primitivedatatypes:Thosenotdefinedintermsofotherdatatypes
• Someprimitivedatatypesaremerelyreflectionsofthehardware
• Othersrequireonlyalittlenon-hardwaresupportfortheirimplementation
Primitive Data Types:Integer
• Almostalwaysanexactreflectionofthehardwaresothemappingistrivial
• Theremaybeasmanyaseightdifferentintegertypesinalanguage
• Java‘s signed integer sizes: byte, short, int,long
Primitive Data Types: Floating Point
• Model real numbers, but only asapproximations
• Languages for scientific use support at least two floating-point types (e.g., float and double;
sometimesmore
• Usually exactly like the hardware, but notalways
• IEEE Floating-Point
Standard754

Primitive Data Types:Complex


• Somelanguagessupportacomplextype,e.g.,FortranandPython
• Eachvalueconsistsoftwofloats,therealpartandtheimaginarypart
• Literal form (inPython):
(7 + 3j), where 7 is the real part and 3 is the imaginary part

Primitive Data Types:Decimal


• For business applications(money)
–Essential to COBOL
–C# offers a decimal data type
• Storeafixednumberofdecimaldigits,incodedform(BCD)
• Advantage: accuracy
• Disadvantages: limited range, wastesmemory
Primitive Data Types:Boolean
• Simplest ofall
• Rangeofvalues:twoelements,onefor―true‖andonefor―false‖
• Could be implemented as bits, but often asbytes
–Advantage: readability
Primitive Data Types:Character
• Stored as numericcodings
• Most commonly used coding:ASCII
• An alternative, 16-bit coding:Unicode
–Includes characters from most natural languages
–Originally used in Java
–C# and JavaScript also support Unicode
Character String Types
• Values are sequences ofcharacters
• Designissues:
–Is it a primitive type or just a special kind of array?
–Should the length of strings be static ordynamic?
Character String TypesOperations
• Typicaloperations:
–Assignment and copying
–Comparison (=, >, etc.)
–Catenation
–Substring reference
–Pattern matching
Character String Type in Certain Languages
• C andC++
–Not primitive
–Use char arrays and a library of functions that provide operations
• SNOBOL4 (a string manipulationlanguage)
–Primitive
–Many operations, including elaborate pattern matching
• Fortran andPython
–Primitive type with assignment and several operations
• Java
–Primitive via the String class
• Perl, JavaScript, Ruby, andPHP
-Providebuilt-inpatternmatching,usingregular expressions

Character String LengthOptions


• Static:COBOL,Java‘sStringclass
• Limited Dynamic Length: C andC++
–thanIntheselanguages,aspecialcharacterisusedtoindicatetheendofa
maintaining thelength
string‘s characters, rather

• Dynamic (no maximum): SNOBOL4, Perl,JavaScript


• Ada supports all three string lengthoptions
Character String TypeEvaluation
• Aid towritability
• As a primitive type with static length, they are inexpensive toprovide--why not havethem?
• Dynamic length is nice, but is it worth theexpense?
Character String Implementation
• Static length: compile-timedescriptor
• Limiteddynamiclength:mayneedarun-timedescriptorforlength(butnot in C andC++)
• Dynamic length: need run-time descriptor; allocation/de-allocation is the biggest
implementationproblem
Compile- and Run-TimeDescriptors

User-Defined Ordinal Types


• An ordinal type is one in which the range of possible values can be easily associated with the
set of positiveintegers
• Examples of primitive ordinal types inJava
–integer
–char
–boolean
Enumeration Types
• Allpossiblevalues,whicharenamedconstants,areprovidedinthedefinition
• C#example
enum days {mon, tue, wed, thu, fri, sat, sun};
• Designissues
–so,Ishow
an enumeration constant allowed to appear in more than one type definition, and if
is the type of an occurrence of that constant checked?
–Are enumeration values coerced to integer?
–Any other type coerced to an enumeration type?
Evaluation of EnumeratedType
• Aidtoreadability,e.g.,noneedtocodeacolorasanumber
• Aid to reliability, e.g., compiler cancheck:
–operations (don‘t allow colors to be added)
–No enumeration variable can be assigned a value outside its defined range
Ada, C#, and Java 5.0 provide better support for enumeration than C++ because

enumeration type variables in these languages are not coerced into integer types

Subrange Types
• An ordered contiguous subsequence of an ordinaltype
–Example: 12..18 is a subrange of integer type
• Ada‘sdesign
typeDaysis(mon,tue,wed,thu,fri,sat,sun); subtype
Weekdays is Days range mon..fri;
subtypeIndexisIntegerrange1..100;

Day1: Days; Day2:


Weekday; Day2 :=
Day1;

Subrange Evaluation
• Aid toreadability
–Makeitcleartothereadersthatvariablesofsubrangecanstoreonly certain range ofvalues
• Reliability
–as Assigning
anerror
a value to a subrange variable that is outside thespecified range is detected

Implementation of User-Defined Ordinal Types


• Enumeration types are implemented asintegers
• Subrangetypesareimplementedliketheparenttypeswithcodeinserted(by
thecompiler)torestrictassignmentstosubrangevariables

Array Types
• An array is an aggregate of homogeneous data elements in which an
individualelementisidentifiedbyitspositionintheaggregate,relativetothe firstelement.

Array Design Issues


• What types are legal forsubscripts?
• Aresubscriptingexpressionsinelementreferencesrangechecked?
• When are subscript rangesbound?
• When does allocation takeplace?
• What is the maximum number ofsubscripts?
• Can array objects beinitialized?
• Are any kind of slicessupported?
Array Indexing
• Indexing(orsubscripting)isamappingfromindicestoelements
array_name(index_value_list) anelement
• IndexSyntax
–FORTRAN, PL/I, Ada useparentheses
• Adaexplicitlyusesparenthesestoshowuniformitybetweenarrayreferences and function calls
because both aremappings

–Most other languages use brackets


Arrays Index (Subscript)Types

• FORTRAN, C: integeronly
• Ada: integer or enumeration (includes Boolean andchar)
• Java: integer typesonly
• Index rangechecking
- C,C++,Perl,andFortrandonotspecify
rangechecking
- Java, ML, C# specify rangechecking
- InAda,thedefaultistorequirerange
checking,butitcanbeturnedoff

Subscript Binding and Array Categories

• Static:subscriptrangesarestaticallyboundandstorageallocationisstatic (beforerun-time)

–Advantage: efficiency (no dynamic allocation)


• Fixedstack-dynamic:subscriptrangesarestaticallybound,buttheallocation is done at
declarationtime

–Advantage: space efficiency


• Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is
dynamic (done atrun-time)

–Advantage:flexibility(thesizeofanarrayneednotbeknownuntilthe array is to beused)


• Fixed heap-dynamic: similar to fixed stack-dynamic: storage binding is dynamic but fixed
after allocation (i.e., binding is done when requested and storage is allocated from heap,
notstack)
Subscript Binding and Array Categories (continued)
• Heap-dynamic:bindingofsubscriptrangesandstorageallocationisdynamic and can change any
number oftimes

–Advantage: flexibility (arrays can grow or shrink during program execution)


• CandC++arraysthatincludestaticmodifierarestatic
• CandC++arrayswithoutstaticmodifierarefixedstack-dynamic
• C and C++ provide fixed heap-dynamicarrays
• C#includesasecondarrayclassArrayListthatprovidesfixedheap-dynamic
• Perl, JavaScript, Python, and Ruby support heap-dynamicarrays
Array Initialization
• Somelanguageallowinitializationatthetimeofstorageallocation
–C, C++, Java, C#example int list
[] = {4, 5, 7, 83}

–CharacterstringsinCandC++
charname[]=―freddie‖;

–Arrays of strings in C and C++


char*names[]={―Bob‖,―Jake‖,―Joe‖];

–Java initialization of String objects String[]


names = {―Bob‖, ―Jake‖,―Joe‖};

Heterogeneous Arrays
• A heterogeneous array is one in which the elements need not be of the same type
• Supported by Perl, Python, JavaScript, andRuby
Arrays Operations

•APLprovidesthemostpowerfularrayprocessingoperationsforvectorsand
matrixesaswellasunaryoperators(forexample,toreversecolumnelements)

• Ada allows array assignment but alsocatenation


• Python‘s array assignments, but they are only reference changes. Python also supports array
catenation and element membership operations

• Ruby also provides arraycatenation


•Fortran provides elemental operations because they are between pairs of array elements
–Forexample,+operatorbetweentwoarraysresultsinanarrayofthesumsof the element pairs of the
twoarrays

Rectangular and JaggedArrays


•A rectangular array is a multi-dimensioned array in which all of the rows
havethesamenumberofelementsandallcolumnshavethesamenumberof elements
• Ajaggedmatrixhasrowswithvaryingnumberofelements
–Possiblewhenmulti-dimensionedarraysactuallyappearasarraysofarrays
• C, C++, and Java support jaggedarrays
• Fortran, Ada, and C# support rectangular arrays (C# also supportsjagged arrays)
Slices
• A slice is some substructure of an array; nothing more than a referencing mechanism
• Slicesareonlyusefulinlanguagesthathavearrayoperations

Implementation of Arrays
• Accessfunctionmapssubscriptexpressionstoanaddressinthearray
Slice Examples
• Fortran95
Integer, Dimension (10) ::Vector
Integer,Dimension(3,3)::Mat
Integer,Dimension(3,3)::Cube

Vector(3:6)isafourelementarray

Slices Examples in Fortran 95


• Accessfunctionforsingle-dimensionedarrays:
address(list[k])=address(list[lower_bound])
+ ((k-lower_bound) * element_size)

Accessing Multi-dimensioned Arrays


• Two commonways:
–Row major order (by rows) – used in most languages
–column major order (by columns) – used in Fortran
Locating an Element in a Multi-dimensioned Array

Compile-Time Descriptors
Associative Arrays
• An associative array is an unordered collection of data elements that are indexed by an
equal number of values calledkeys

–User-defined keys must be stored


• Designissues:
- What is the form of references toelements?
- Is the size static ordynamic?

Associative Arrays inPerl


• Names begin with %; literals are delimited byparentheses
%hi_temps=("Mon"=>77,"Tue"=>79,―Wed‖=>65,…);
• Subscripting is done using braces andkeys
$hi_temps{"Wed"} = 83;

–Elementscanberemovedwithdelete
delete$hi_temps{"Tue"};

Record Types
• Arecordisapossiblyheterogeneousaggregateofdataelementsinwhichthe individual elements are
identified bynames
• Designissues:
–What is the syntactic form of references to the field?
–Are elliptical references allowed
Definition of Records in COBOL
• COBOL uses level numbers to show nested records; others use recursive definition
01 EMP-REC.
02 EMP-NAME.
05 FIRST PIC X(20).
05MID PICX(10).
05 LAST PIC X(20).
02 HOURLY-RATE PIC 99V99.

Definition of Records in Ada


• Record structures are indicated in an orthogonalway
type Emp_Rec_Type is record First:
String (1..20);
Mid: String (1..10);
Last: String (1..20);
Hourly_Rate: Float;
end record;
Emp_Rec: Emp_Rec_Type;

References to Records

• Record fieldreferences
1. COBOL
field_name OF record_name_1 OF ... OF record_name_n
2. Others (dotnotation)
record_name_1.record_name_2. ... record_name_n.field_name

• Fully qualified references must include all recordnames


• Ellipticalreferencesallowleavingoutrecordnamesaslongasthereference is unambiguous, for
example inCOBOL
FIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the
employee‘s first name

Operations on Records
• Assignment is very common if the types areidentical
• Ada allows recordcomparison
• Ada records can be initialized with aggregateliterals
• COBOL provides MOVECORRESPONDING
–Evaluation
Copies a field of the source record to the corresponding field in the target record
and Comparison to Arrays

• Recordsareusedwhencollectionofdatavaluesisheterogeneous
• Accesstoarrayelementsismuchslowerthanaccesstorecordfields,because subscripts are dynamic
(field names arestatic)
• Dynamic subscripts could be used with record field access, but it would disallow type
checking and it would be muchslower

Implementation of RecordType
Unions Types
• Aunionisatypewhosevariablesareallowedtostoredifferenttypevaluesat different times
duringexecution
• Designissues
–Should type checking be required?
–Should unions be embedded in records?
Discriminated vs. FreeUnions
• Fortran,C,andC++provideunionconstructsinwhichthereisnolanguage
supportfortypechecking;theunionintheselanguagesiscalledfreeunion
• Type checking of unions require that each union include a type indicator called
adiscriminant

–Supported by Ada
Ada Union Types
type Shape is (Circle, Triangle, Rectangle); type
Colors is (Red, Green,Blue);
type Figure (Form: Shape) isrecord Filled:
Boolean;
Color: Colors;
case Form is
whenCircle=>Diameter:Float; when
Triangle=>
Leftside, Rightside: Integer; Angle:
Float;
when Rectangle => Side1, Side2: Integer;
end
case;end record;

Ada Union TypeIllustrated

• A pointer type variable has a range of values that consists of memory addresses and a special
value,nil
• Provide the power of indirectaddressing
• Provideawaytomanagedynamicmemory
• A pointer can be used to access a location in the area where storage is dynamically created
(usually called aheap)

Design Issues ofPointers


• Whatarethescopeofandlifetimeofapointervariable?
• What is the lifetime of a heap-dynamicvariable?
• Arepointersrestrictedastothetypeofvaluetowhichtheycanpoint?
• Arepointersusedfordynamicstoragemanagement,indirectaddressing,or both?
• Shouldthelanguagesupportpointertypes,referencetypes,orboth?
Pointer Operations
• Two fundamental operations: assignment anddereferencing
• Assignmentisusedtosetapointervariable‘svaluetosomeusefuladdress
• Dereferencing yields the value stored at the location represented by the pointer‘svalue
–Dereferencing can be explicit or implicit C++ uses an
–explicit operation via *
j = *ptr
sets j to the value located at ptr

Pointer Assignment Illustrated

The assignment operation j = *ptr

Problems with Pointers

• Dangling pointers(dangerous)
–A pointer points to a heap-dynamic variable that has been deallocated
• Lost heap-dynamicvariable
–An allocated heap-dynamic variable that is no longer accessible to the user
program (often called garbage)
• Pointerp1issettopointtoanewlycreatedheap-dynamicvariable
• Pointer p1 is later set to point to another newly created heap-dynamic variable
• Theprocessoflosingheap-dynamicvariablesiscalledmemoryleakage
Pointers in Ada
• Some dangling pointers are disallowed because dynamic objects can be
automaticallydeallocatedattheendofpointer'stypescope
• The lost heap-dynamic variable problem is not eliminated by Ada (possible
withUNCHECKED_DEALLOCATION)

Pointers in C and C++

• Extremely flexible but must be used withcare


•Pointers can point at any variable regardless of when or where it was allocated
• Used for dynamic storage management andaddressing
• Pointer arithmetic ispossible
• Explicit dereferencing and address-of operators Domain
•type need not be fixed(void *)
void*canpointtoanytypeandcanbetype checked
(cannot bede-referenced)

Pointer Arithmetic in C and C++


float stuff[100];
float *p;
p = stuff;

*(p+5) is equivalent to stuff[5] and p[5]


*(p+i) is equivalent to stuff[i] and p[i]

Reference Types

• C++ includes a special kind of pointer type called a reference type that is used primarily for
formalparameters
–Advantages of both pass-by-reference and pass-by-value
• JavaextendsC++‘sreferencevariablesandallowsthemtoreplacepointers entirely
–References are references to objects, rather than being addresses
• C#includesboththereferencesofJavaandthepointersofC++
Evaluation of Pointers
• Danglingpointersanddanglingobjectsareproblemsasisheapmanagement
• Pointersarelikegoto's--theywidentherangeofcellsthatcanbeaccessedby avariable
• Pointersorreferencesarenecessaryfordynamicdatastructures--sowecan't design a language
withoutthem

Representations of Pointers
• Large computers use singlevalues
• Intel microprocessors use segment andoffset
Dangling Pointer Problem

• Tombstone:extraheapcellthatisapointertotheheap-dynamicvariable
–The actual pointer variable points only at tombstones
–When heap-dynamic variable de-allocated, tombstone remains but set to nil
–Costly in time and space
. Locks-and-keys: Pointer values are represented as (key, address) pairs

–Heap-dynamicvariablesarerepresentedasvariablepluscellforintegerlock value
–When heap-dynamic variable allocated, lock value is created and placed in lock cell and key
cell of pointer

Heap Management
• A very complex run-timeprocess
• Single-size cells vs. variable-sizecells
• Two approaches to reclaimgarbage
–Reference counters (eager approach): reclamation is gradual
–Mark-sweep (lazy approach): reclamation occurs when the list of variable space
becomesempty

Reference Counter
• Referencecounters:maintainacounterineverycellthatstorethenumberof pointers currently
pointing at thecell

–circularly
Disadvantages: space required, execution time required, complications for cells connected

–Advantage: it is intrinsically incremental, so significant delays in the application execution are


avoided

Mark-Sweep

•The run-time system allocates storage cells as requested and disconnects pointers from cells as
necessary; mark-sweep thenbegins

–Every heap cell has an extra bit used by collection algorithm


–All cells initially set to garbage
–All pointers traced into heap, and reachable cells marked as not garbage
–All garbage cells returned to list of availablecells
–Disadvantages:initsoriginalform,itwasdonetooinfrequently.Whendone, itcaused significant
delays in application execution. Contemporary mark-
sweepalgorithmsavoidthisbydoingitmoreoften—calledincrementalmark- sweep

Marking Algorithm
Length
If too short, they cannot beconnotative
Languageexamples:
FORTRAN I: maximum6
COBOL: maximum30
FORTRAN 90 and ANSI C: maximum31
Ada and Java: no limit, and all aresignificant
C++: no limit, but implementors often imposeone
Connectors
Pascal, Modula-2, and FORTRAN 77 don'tallow
Othersdo
Casesensitivity
Disadvantage: readability (names that look alike aredifferent)
worse in C++ and Java because predefined names are mixed case(e.g.
IndexOutOfBoundsException)
C, C++, and Java names are casesensitive
The names in other languages arenot
Specialwords
An aid to readability; used to delimit or separate statementclauses
Def: A keyword is a word that is special only in certain contexts i.e. in Fortran:
Real VarName (Real is data type followed with a name, therefore Real is a keyword)
Real = 3.4 (Real is avariable)
Disadvantage: poorreadability
Def: A reserved word is a special word that cannot be used as a user-defined
name

Variables
A variable is an abstraction of a memorycell
Variables can be characterized as a sextuple ofattributes:
(name, address, value, type, lifetime, andscope)
Name - not all variables have them(anonymous)
Address - the memory address with which it is associated (also calledl-value)
A variable may have different addresses at different times duringexecution
A variable may have different addresses at different places in aprogram
If two variable names can be used to access the same memory location, they are
calledaliases
Aliases are harmful to readability (program readers must remember all ofthem)
How aliases can becreated:
Pointers, reference variables, C and C++ unions, (and through parameters-
discussed in Chapter9)
Some of the original justifications for aliases are no longer valid; e.g. memory
reuse inFORTRAN
Replace them with dynamicallocation
Type - determines the range of values of variables and the set of operations that are
defined for values of that type; in the case of floating point, type also determines the
precision
Value - the contents of the location with which the variable isassociated
Abstract memory cell - the physical cell or collection of cells associated with a
variable

The Concept of Binding


The l-value of a variable is itsaddress
The r-value of a variable is itsvalue
Def: A binding is an association, such as between an attribute and an entity, or
between an operation and asymbol
Def: Binding time is the time at which a binding takesplace.
Possible bindingtimes:
Language design time--e.g., bind operator symbols tooperations
Language implementation time--e.g., bind floating point type to a
representation
Compile time--e.g., bind a variable to a type in C orJava
Load time--e.g., bind a FORTRAN 77 variable to a memory cell (or a Cstatic
variable)
Runtime--e.g., bind a nonstatic local variable to a memorycell
Def: A binding is static if it first occurs before run time and remains unchanged
throughout programexecution.
Def: A binding is dynamic if it first occurs during execution or can change during
execution of theprogram.
TypeBindings
How is a typespecified?
When does the binding takeplace?
If static, the type may be specified by either an explicit or an implicitdeclaration
Def: An explicit declaration is a program statement used for declaring the types of
variables
Def: An implicit declaration is a default mechanism for specifying types of variables
(the first appearance of the variable in theprogram)
FORTRAN, PL/I, BASIC, and Perl provide implicitdeclarations
Advantage:writability
Disadvantage: reliability (less trouble withPerl)
Dynamic Type Binding (JavaScript andPHP)
Specified through anassignmentstatement e.g.,JavaScript
list = [2, 4.33, 6, 8];
list = 17.3;
Advantage: flexibility (generic programunits)
Disadvantages:
High cost (dynamic type checking andinterpretation)
Type error detection by the compiler isdifficult
Type Inferencing (ML, Miranda, andHaskell)
Rather than by assignment statement, types are determined from the contextof
thereference
Storage Bindings &Lifetime
Allocation - getting a cell from some pool of availablecells
Deallocation - putting a cell back into thepool
Def: The lifetime of a variable is the time during which it is bound to a particular
memorycell
Categories of variables bylifetimes
Static--bound to memory cells before execution begins and remains bound to
the same memory cell throughoutexecution.
e.g. all FORTRAN 77 variables, C static variables
Advantages: efficiency (direct addressing), history-sensitive subprogram
support
Disadvantage: lack of flexibility (norecursion)
Categories of variables bylifetimes
Stack-dynamic--Storage bindings are created for variables when their
declaration statements areelaborated.
If scalar, all attributes except address are staticallybound
e.g. local variables in C subprograms and Javamethods
Advantage: allows recursion; conservesstorage
Disadvantages:
Overhead of allocation anddeallocation
Subprograms cannot be historysensitive
Inefficient references (indirectaddressing)
Categories of variables bylifetimes
Explicit heap-dynamic--Allocated and deallocated by explicit directives,
specified by the programmer, which take effect duringexecution
Referenced only through pointers orreferences
e.g. dynamic objects in C++ (via new and delete)
all objects in Java
Advantage: provides for dynamic storagemanagement
Disadvantage: inefficient andunreliable
Categories of variables bylifetimes
Implicit heap-dynamic--Allocation and deallocation caused by assignment
statements
e.g. all variables in APL; all strings and arrays in Perl and JavaScript
Advantage:flexibility
Disadvantages:
Inefficient, because all attributes aredynamic
Loss of errordetection

Type Checking
Generalize the concept of operands and operators to include subprograms and
assignments
Type checking is the activity of ensuring that the operands of an operator are of
compatibletypes
A compatible type is one that is either legal for the operator, or is allowed under
language rules to be implicitly converted, by compiler- generated code, to a legal
type. This automatic conversion is called acoercion.
A type error is the application of an operator to an operand of aninappropriate
type
If all type bindings are static, nearly all type checking can bestatic
If type bindings are dynamic, type checking must bedynamic
Def:Aprogramminglanguageisstronglytypediftypeerrorsarealwaysdetected

Strong Typing
Advantage of strong typing: allows the detection of the misuses of variables that
result in typeerrors
Languageexamples:
FORTRAN 77 is not: parameters,EQUIVALENCE
Pascal is not: variantrecords
C and C++ are not: parameter type checking can be avoided; unions are not
typechecked
Ada is, almost (UNCHECKED CONVERSION is loophole)
(Java is similar)
Coercion rules strongly affect strong typing--they can weaken it considerably (C++
versusAda)
Although Java has just half the assignment coercions of C++, its strong typing is
still far less effective than that ofAda

Type Compatibility
Our concern is primarily for structuredtypes
Def: Name type compatibility means the two variables have compatible types if
they are in either the same declaration or in declarations that use the same type
name
Easy to implement but highlyrestrictive:
Subranges of integer types are not compatible with integertypes
Formal parameters must be the same type as their corresponding actual
parameters (Pascal)
Structure type compatibility means that two variables have compatible types if their
types have identicalstructures
More flexible, but harder toimplement
Consider the problem of two structuredtypes:
Are two record types compatible if they are structurally the same but use
different fieldnames?
Are two array types compatible if they are the same except that the subscripts
aredifferent?
(e.g. [1..10] and [0..9])
Are two enumeration types compatible if their components are spelled
differently?
With structural type compatibility, you cannot differentiate between types of
thesamestructure (e.g. different units of speed, bothfloat)
Languageexamples:
Pascal: usually structure, but in some cases name is used (formalparameters)
C: structure, except forrecords
Ada: restricted form ofname
Derived types allow types with the same structure to bedifferent
Anonymous types are all unique, evenin:
A, B : array (1..10) of INTEGER:
Scope
The scope of a variable is the range of statements over which it isvisible
The nonlocal variables of a program unit are those that are visible but not declared
there
The scope rules of a language determine how references to names are associated
withvariables
Staticscope
Based on programtext
To connect a name reference to a variable, you (or the compiler) must findthe
declaration
Search process: search declarations, first locally, then in increasinglylarger
enclosing scopes, until one is found for the givenname
Enclosingstaticscopes(toaspecificscope)arecalleditsstaticancestors;the
nearest static ancestor is called a staticparent
Variables can be hidden from a unit by having a "closer" variable with the same
name
C++ and Ada allow access to these "hidden"variables
In Ada:unit.name
In C++:class_name::name
Blocks
A method of creating static scopes inside program units--from ALGOL60
Examples:
C and C++: for (...)
{
int index;
...
}
Ada: declare LCL : FLOAT;
begin
...
end
Evaluation of StaticScoping
Consider theexample:
Assume MAIN calls A and B
A calls C and D
B calls A and E
Static Scope Example
Static Scope Example
Static Scope
Suppose the spec is changed so that D must now access some data inB
Solutions:
Put D in B (but then C can no longer call it and D cannot access A'svariables)
Move the data from B that D needs to MAIN (but then all procedures can access
them)
Same problem for procedureaccess
Overall: static scoping often encourages manyglobals
DynamicScope
Based on calling sequences of program units, not their textual layout (temporal
versus spatial)
References to variables are connected to declarations by searching back through
the chain of subprogram calls that forced execution to thispoint
Scope Example
MAIN
- declaration of x
SUB1
- declaration of x -
...
call SUB2
...

SUB2
...
- reference to x -
...

...
call SUB1

Scope Example
Staticscoping
Reference to x is to MAIN'sx
Dynamic scoping
Reference to x is to SUB1'sx
Evaluation of DynamicScoping:
Advantage:convenience
Disadvantage: poorreadability

Scope and Lifetime


Scope and lifetime are sometimes closely related, but are differentconcepts
Consider a static variable in a C or C++function
Referencing Environments
Def:Thereferencingenvironmentofastatementisthecollectionofallnamesthat are
visible in thestatement
In a static-scoped language, it is the local variables plus all of the visible variables in
all of the enclosingscopes
A subprogram is active if its execution has begun but has not yetterminated
In a dynamic-scoped language, the referencing environment is the local variables
plus all visible variables in all activesubprograms

Named Constants
Def: A named constant is a variable that is bound to a value only when it is bound
tostorage
Advantages: readability andmodifiability
Used to parameterizeprograms
The binding of values to named constants can be either static (calledmanifest
constants) ordynamic
Languages:
Pascal: literalsonly
FORTRAN 90: constant-valuedexpressions
Ada, C++, and Java: expressions of any kind
VariableInitialization
Def: The binding of a variable to a value at the time it is bound to storage is called
initialization
Initialization is often done on the declarationstatement
e.g.,Java
int sum = 0;

Summary

• The data types of a language are a large part of what determines that language‘s style and
usefulness

• Theprimitivedatatypesofmostimperativelanguagesincludenumeric, character, and


Booleantypes

• The user-defined enumeration and subrange types are convenient and add to the readability
and reliability of programs

• Arrays and records are included in mostlanguages

• Pointers are used for addressing flexibility and to control dynamic storage management

Case sensitivity and the relationship of names to special words representdesign


issues of names

Variables are characterized by the sextuples: name, address, value, type, lifetime,
scope

Binding is the association of attributes with programentities

Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic,
implicit heapdynamic
UNIT-IV
Expressions and Statements

• Introduction
• ArithmeticExpressions
• OverloadedOperators
• TypeConversions
• Relational and BooleanExpressions
• Short-CircuitEvaluation
• AssignmentStatements
• Mixed-ModeAssignment
• ControlStructures
• Introduction
• SelectionStatements
• IterativeStatements
• UnconditionalBranching
• GuardedCommands
• Conclusions
Introduction
• Expressions are the fundamental means of specifying computations in a programminglanguage
• Tounderstandexpressionevaluation,needtobefamiliarwiththeordersof operator and
operandevaluation
• Essenceofimperativelanguagesisdominantroleofassignmentstatements
ArithmeticExpressions
• Arithmeticevaluationwasoneofthemotivationsforthedevelopmentofthe first
programminglanguages
• Arithmetic expressions consist of operators, operands, parentheses, and functioncalls
Arithmetic Expressions: DesignIssues
• Designissuesforarithmeticexpressions
–Operator precedence rules?
–Operator associativity rules?
–Order of operand evaluation?
–Operand evaluation side effects?
–Operator overloading?
–Type mixing in expressions?
Arithmetic Expressions: Operators
• A unary operator has oneoperand
• A binary operator has twooperands
• A ternary operator has threeoperands
Arithmetic Expressions: Operator Precedence Rules
• The operator precedence rules for expression evaluation define the order in
which―adjacent‖operatorsofdifferentprecedencelevelsareevaluated
• Typical precedencelevels
– parentheses
– unaryoperators
– ** (if the language supportsit)
–*, /
– +,-
Arithmetic Expressions: Operator Associativity Rule

• The operator associativity rules for expression evaluation define the order in
whichadjacentoperatorswiththesameprecedencelevelareevaluated
• Typical associativityrules
–Left to right, except **, which is right to left
–Sometimes unary operators associate right to left (e.g., in FORTRAN)
• APL is different; all operators have equal precedence and all operators associate right toleft
• Precedence and associativity rules can be overriden with parentheses Arithmetic
Expressions: ConditionalExpressions
• ConditionalExpressions
–C-basedlanguages(e.g.,C,C++)
–Anexample:
average = (count == 0)? 0 : sum / count

–Evaluates as if written like


if (count ==0)
average =0
else
average = sum/count

Arithmetic Expressions: Operand Evaluation Order


• Operand evaluationorder
• Variables: fetch the value frommemory
• Constants:sometimesafetchfrommemory;sometimestheconstantisinthe machine
languageinstruction
• Parenthesizedexpressions:evaluatealloperandsandoperatorsfirst
• Themostinterestingcaseiswhenanoperandisafunctioncall Arithmetic
Expressions: Potentials for SideEffects

• Functional side effects: when a function changes a two-way parameter or a non-


localvariable
• Problem with functional sideeffects:
–When a function referenced in an expression alters another operand of the expression; e.g., for
a parameter change:
a = 10;
/* assume that fun changes its parameter */ b = a +
fun(a);

Functional Side Effects


• Two possible solutions to theproblem
• Writethelanguagedefinitiontodisallowfunctionalsideeffects
• Notwo-wayparametersinfunctions
• Nonon-localreferencesinfunctions
• Advantage: itworks!
• Disadvantage: inflexibility of one-way parameters and lack of non-local references
• Write the language definition to demand that operand evaluation order be fixed
• Disadvantage: limits some compileroptimizations
• Javarequiresthatoperandsappeartobeevaluatedinleft-to-rightorder OverloadedOperators
• Useofanoperatorformorethanonepurposeiscalledoperatoroverloading
• Some are common (e.g., + for int andfloat)
• Some are potential trouble (e.g., * in C andC++)
–Loss of compiler error detection (omission of an operand should be a detectable error)
–Some loss of readability
–Canbeavoidedbyintroductionofnewsymbols(e.g.,Pascal‘sdivforinteger division)
Overloaded Operators (continued)
• C++,Ada,Fortran95,andC#allowuser-definedoverloadedoperators
• Potentialproblems:
–Users can define nonsense operations
–Readability may suffer, even when the operators make sense
Type Conversions
• A narrowing conversion is one that converts an object to a type that cannot
includeallofthevaluesoftheoriginaltypee.g.,floattoint
• A widening conversion is one in which an object is converted to a type that can include at
least approximations to all of the values of the original type e.g., int tofloat
Type Conversions: Mixed Mode
• A mixed-mode expression is one that has operands of differenttypes
• A coercion is an implicit typeconversion
• Disadvantage ofcoercions:
–They decrease in the type error detection ability of the compiler
• In most languages, all numeric types are coerced in expressions, using wideningconversions
• InAda,therearevirtuallynocoercionsinexpressions Explicit
TypeConversions

• Called casting in C-basedlanguages


• Examples
–C: (int)angle
–Ada: Float (Sum)
Note that Ada’s syntax is similar to that of function calls
Type Conversions: Errors in Expressions
• Causes
–Inherentlimitationsofarithmetic e.g., division byzero

–Limitationsofcomputerarithmetic e.g.overflow
• Often ignored by the run-timesystem
Relational and BooleanExpressions
• RelationalExpressions
–Use relational operators and operands of varioustypes
–Evaluate to some Boolean representation
–Operatorsymbolsusedvarysomewhatamonglanguages(!=,/=,.NE.,<>,#)
• JavaScriptandPHPhavetwoadditionalrelationaloperator,===and!==
- Similar to their cousins, == and !=, except that they do not coerce their operands

• BooleanExpressions
–Operands are Boolean and the result is Boolean
–Example operators
FORTRAN77 FORTRAN 90 C Ada
.AND. and && and
.OR. or || or
.NOT. not ! not
xor
Relational and Boolean Expressions: No Boolean Type in C
• C89 has no Boolean type--it uses int type with 0 for false and nonzero for true
• One odd characteristic of C‘sexpressions: a < b <c is a legal
expression, but the result is not what you mightexpect:

–Left operator is evaluated, producing 0 or 1


–Theevaluationresultisthencomparedwiththethirdoperand(i.e.,c) Short
CircuitEvaluation
• Anexpressioninwhichtheresultisdeterminedwithoutevaluatingallofthe operands and/oroperators
• Example:(13*a)*(b/13–1)
If a is zero, there is no need to evaluate (b/13-1)
• Problem with non-short-circuitevaluation
index = 1;
while(index<=length)&&(LIST[index]!=value)
index++;

–Whenindex=length,LIST[index]willcauseanindexingproblem(assuming LIST has length -


1elements)
Short Circuit Evaluation (continued)
• C, C++, and Java: use short-circuit evaluation for the usual Boolean
operators(&&and||),butalsoprovidebitwiseBooleanoperatorsthatarenot shortcircuit(&and|)

• Ada:programmercanspecifyeither(short-circuitisspecifiedwithandthen and orelse)


• Short-circuit evaluation exposes the potential problem of side effects in expressions
e.g. (a > b) || (b++ / 3)
Assignment Statements1
• The generalsyntax
<target_var><assign_operator><expression>
• The assignmentoperator
= FORTRAN, BASIC, the C-basedlanguages
:= ALGOLs, Pascal, Ada
•= canbebadwhenitisoverloadedfortherelationaloperatorforequality (that‘s why the C-based
languages use == as the relational operator) Assignment Statements: ConditionalTargets
• Conditional targets (Perl)
($flag ? $total : $subtotal) =0

Whichisequivalentto

if($flag){
$total = 0
} else {
$subtotal = 0
}

Assignment Statements: CompoundOperators


• Ashorthandmethodofspecifyingacommonlyneededformofassignment
• Introduced in ALGOL; adopted byC
• Example
a=a+b

is writtenas a

+= b

Assignment Statements: Unary Assignment Operators


• Unary assignment operators in C-based languages combine increment and decrement
operations withassignment
• Examples
sum=++count(countincremented,addedtosum)
sum=count++(countincremented,addedtosum) count++
(countincremented)
-count++ (count incremented then negated) Assignment
as an Expression
• InC,C++,andJava,theassignmentstatementproducesaresultandcanbe used asoperands
• Anexample:
while((ch=getchar())!=EOF){…}

ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for
the while statement
List Assignments
• PerlandRubysupportlistassignments e.g.,
($first, $second, $third) = (20, 30, 40);

Mixed-Mode Assignment
• Assignmentstatementscanalsobemixed-mode,forexample int a,b;
float c;
c = a / b;
• In Fortran, C, and C++, any numeric type value can be assigned to any numeric typevariable
• In Java, only widening assignment coercions aredone
• In Ada, there is no assignmentcoercion

Levels of ControlFlow

–Withinexpressions
–Amongprogramunits
–Amongprogramstatements
Control Statements: Evolution
• FORTRANIcontrolstatementswerebaseddirectlyonIBM704hardware
• Much research and argument in the 1960s about theissue
–flowchartscanbecodedwithonlytwo-wayselectionandpretestlogicalloops
One important result: It was proven that all algorithms represented by

Control Structure
• Acontrolstructureisacontrolstatementandthestatementswhoseexecution itcontrols
• Designquestion
–Should a control structure have multiple entries?
Selection Statements
• A selection statement provides the means of choosing between two or more paths
ofexecution
• Two generalcategories:
–Two-way selectors
–Multiple-way selectors
Two-Way Selection Statements
• Generalform:
if control_expressionthen
clause else clause
• DesignIssues:
–What is the form and type of the controlexpression?
–How are the then and else clauses specified?
–How should the meaning of nested selectors be specified?
The Control Expression
• If the then reserved word or some other syntactic marker is not used to
introducethethenclause,thecontrolexpressionisplacedinparentheses
• InC89,C99,Python,andC++,thecontrolexpressioncanbearithmetic
• In languages such as Ada, Java, Ruby, and C#, the control expressionmust beBoolean
Clause Form

• In many contemporary languages, the then and else clauses can be single statements or
compoundstatements
• InPerl,allclausesmustbedelimitedbybraces(theymustbecompound)
• InFortran95,Ada,andRuby,clausesarestatementsequences
• Pythonusesindentationtodefineclauses if x >y:
x=y
print "case 1"

Nesting Selectors
• Javaexample
if(sum==0)
if(count==0)
result=0;
elseresult=1;
• Which if gets theelse?
• Java'sstaticsemanticsrule:elsematcheswiththenearestif Nesting
Selectors(continued)
• Toforceanalternativesemantics,compoundstatementsmaybeused: if (sum == 0){
if(count==0)
result =0;
}
else result = 1;
• The above solution is used in C, C++, andC#
• Perlrequiresthatallthenandelseclausestobecompound
• Statementsequencesasclauses:Ruby if sum ==
0then
ifcount==0then result
=0
else
result=1
end
end
• Python
if sum == 0 :
ifcount==0:
result =0
else :
result=1

Multiple-Way Selection Statements


• Allowtheselectionofoneofanynumberofstatementsorstatementgroups
• DesignIssues:
• What is the form and type of the controlexpression?
• How are the selectable segmentsspecified?
• Is execution flow through the structure restricted to include just a single selectablesegment?
• How are case valuesspecified?
• What is done about unrepresented expressionvalues?
Multiple-Way Selection: Examples
• C, C++, andJava
switch (expression) {
case const_expr_1: stmt_1;

caseconst_expr_n:stmt_n;[defaul
t:stmt_n+1]
}

• Design choices for C‘s switchstatement


• Control expression can be only an integertype
• Selectable segments can be statement sequences, blocks, or compound statements
• Anynumberofsegmentscanbeexecutedinoneexecutionoftheconstruct
(thereisnoimplicitbranchattheendofselectablesegments)
• default clause is for unrepresented values (if there is no default, thewhole statement
doesnothing)
Multiple-Way Selection: Examples
• C#
- DiffersfromCinthatithasastatic semantics rule that disallows the implicit
execution of more than onesegment

- Each selectable segment must end with an unconditional branch (gotoor break)
• Ada
case expression is
when choice list =>stmt_sequence;

when choice list =>stmt_sequence; when
others =>stmt_sequence;]
end case;
• MorereliablethanC‘sswitch(onceastmt_sequenceexecutioniscompleted,
controlispassedtothefirststatementafterthecasestatement

• Ada designchoices:
1. Expression can be any ordinaltype
2. Segments can be single orcompound
3. Onlyonesegmentcanbeexecutedperexecutionoftheconstruct
4. Unrepresented values are notallowed

• Constant ListForms:
1. A list ofconstants
2. Caninclude:
- Subranges
- BooleanORoperators(|)

Multiple-Way Selection Usingif


• MultipleSelectorscanappearasdirectextensionstotwo-wayselectors,using else-if clauses, for
example inPython:
ifcount<10:
bag1=True
elsifcount<100: bag2
=True
elifcount<1000: bag3
=True

Iterative Statements
• The repeated execution of a statement or compound statement is
accomplished either by iteration orrecursion
• General design issues for iteration controlstatements:
1. How is iterationcontrolled?
2. Where is the control mechanism in theloop?

Counter-Controlled Loops
• Acountingiterativestatementhasaloopvariable,andameansofspecifying the initial and
terminal, and stepsizevalues
• DesignIssues:
• What are the type and scope of the loopvariable?
• Whatisthevalueoftheloopvariableatlooptermination?
• Shoulditbelegalfortheloopvariableorloopparameterstobechangedin
theloopbody,andifso,doesthechangeaffectloopcontrol?

• Should the loop parameters be evaluated only once, or once for every iteration?
Iterative Statements: Examples

• FORTRAN 95syntax
DO label var = start, finish [, stepsize]
• Stepsize can be any value butzero
• Parameters can beexpressions
• Designchoices:
1. Loop variable must beINTEGER
2. Loop variable always has its lastvalue
3. The loop variable cannot be changed in the loop, but the parameters can;
becausetheyareevaluatedonlyonce,itdoesnotaffectloopcontrol
4. Loop parameters are evaluated onlyonce
• FORTRAN 95 : a secondform:
[name:] Do variable = initial, terminal [,stepsize]
… End
Do [name]

- Cannot branch into either of Fortran‘s Do statements

• Ada
forvarin[reverse]discrete_rangeloop ...
endloop
• Designchoices:
- Typeoftheloopvariableisthatofthediscreterange(Adiscreterangeisa sub-range of an integer or
enumerationtype).
- Loop variable does not exist outside theloop
- Theloopvariablecannotbechangedintheloop,butthediscreterangecan; it does not affect
loopcontrol
- The discrete range is evaluated justonce
• Cannot branch into the loopbody
• C-basedlanguages
for ([expr_1] ; [expr_2] ; [expr_3]) statement
- Theexpressionscanbewholestatements,orevenstatementsequences, with the statements
separated bycommas
–The value of a multiple-statement expression is the value of the last statement in the expression
–If the second expression is absent, it is an infinite loop
• Designchoices:
- There is no explicit loopvariable
- Everything can be changed in theloop
- Thefirstexpressionisevaluatedonce,buttheothertwoareevaluatedwith eachiteration

• C++ differs from C in twoways:


• The control expression can also beBoolean
• The initial expression can include variable definitions (scope is from the definition to the end
of the loopbody)
• Java andC#
–Statements:
DiffersfromC++inthatthecontrolexpressionmustbeBoolean Iterative
Logically-ControlledLoops
• Repetition control is based on a Booleanexpression
• Designissues:
–Pretest or posttest?
–Should the logically controlled loop be a special case of the counting loop statement or a
separate statement?

Iterative Statements: Logically-Controlled Loops: Examples


• C and C++ have both pretest and posttest forms, in which the control expression can
bearithmetic:
while(ctrl_expr) do
loopbody loop body
while(ctrl_expr)
• JavaislikeCandC++,exceptthecontrolexpressionmustbeBoolean(and
thebodycanonlybeenteredatthebeginning--Javahasnogoto
Iterative Statements: Logically-Controlled Loops: Examples
• Ada has a pretest version, but noposttest
• FORTRAN 95 hasneither
• Perl and Ruby have two pretest logical loops, while and until. Perl also has two posttestloops
Iterative Statements: User-Located Loop Control Mechanisms
• Sometimesitisconvenientfortheprogrammerstodecidealocationforloop control (other than top or
bottom of theloop)
• Simple design for single loops (e.g.,break)
• Design issues for nestedloops
• Should the conditional be part of theexit?
• Shouldcontrolbetransferableoutofmorethanoneloop?
Iterative Statements: User-Located Loop Control Mechanisms break and
continue
• C,C++,Python,Ruby,andC#haveunconditionalunlabeledexits(break)
• JavaandPerlhaveunconditionallabeledexits(breakinJava,lastinPerl)
• C, C++, and Python have an unlabeled control statement, continue, that
skipstheremainderofthecurrentiteration,butdoesnotexittheloop
• Java and Perl have labeled versions ofcontinue
Iterative Statements: Iteration Based on Data Structures
• Numberofelementsofinadatastructurecontrolloopiteration
• Control mechanism is a call to an iterator function that returns the next
elementinsomechosenorder,ifthereisone;elseloopisterminate
• C'sforcanbeusedtobuildauser-definediterator:
for(p=root;p==NULL;traverse(p)){
}

• C#‘s foreach statement iterates on the elements of arrays and other collections:
Strings[] = strList = {"Bob", "Carol", "Ted"}; foreach
(Strings name in strList)
Console.WriteLine ("Name: {0}", name);

- The notation {0} indicates the position in the string to be displayed


• Perl has a built-in iterator for arrays and hashes,foreach
Unconditional Branching
• Transfersexecutioncontroltoaspecifiedplaceintheprogram
• Representedoneofthemostheateddebatesin1960‘sand1970‘s
• Well-known mechanism: gotostatement
• Major concern:Readability
• Somelanguagesdonotsupportgotostatement(e.g.,Java)
• C#offersgotostatement(canbeusedinswitchstatements)
• Loop exit statements are restricted and somewhat
camouflagedgoto‘sGuardedCommands
• Designed byDijkstra
• Purpose: to support a new programming methodology that supported
verification (correctness) duringdevelopment
• Basisfortwolinguisticmechanismsforconcurrentprogramming(inCSPand Ada)
• Basic Idea: if the order of evaluation is not important, the program should not specifyone
Selection Guarded Command
• Form
if <Boolean exp> -><statement> []
<Boolean exp> -><statement>
...
[] <Boolean exp> -><statement>fi
• Semantics: when construct isreached,
–Evaluate all Boolean expressions
–Ifmorethanonearetrue,chooseonenon-deterministically If none are true,
–it is a runtimeerror
Selection Guarded Command: Illustrated Loop Guarded
Command
• Form
do <Boolean> -><statement>[]
<Boolean> -><statement>
...
[] <Boolean> -><statement>od
• Semantics: for eachiteration
–Evaluate all Boolean expressions
–Ifmorethanonearetrue,chooseonenon-deterministically;thenstartloop again
–If none are true, exit loop
Guarded Commands: Rationale
• Connectionbetweencontrolstatementsandprogramverificationisintimate
• Verification is impossible with gotostatements
• Verificationispossiblewithonlyselectionandlogicalpretestloops
• Verificationisrelativelysimplewithonlyguardedcommands
Conclusion
• Expressions
• Operator precedence andassociativity
• Operatoroverloading
• Mixed-typeexpressions
• Various forms ofassignment
• Variety of statement-levelstructures
• Choiceofcontrolstatementsbeyondselectionandlogicalpretestloopsisa trade-off between
language size andwritability
• Functional and logic programming languages are quite different control structures
UNIT-V
Subprograms and Blocks

Topics
• Introduction
• Fundamentals ofSubprograms
• Design Issues forSubprograms
• Local ReferencingEnvironments
• Parameter-PassingMethods
• Parameters That Are SubprogramNames
• OverloadedSubprograms
• GenericSubprograms
• Design Issues forFunctions
• User-Defined OverloadedOperators
• Coroutines
Introduction
• Two fundamental abstractionfacilities
–Process abstraction
• Emphasized from earlydays
–Data abstraction
• Emphasized inthe1980s
Fundamentals of Subprograms
• Each subprogram has a single entrypoint
• Thecallingprogramissuspendedduringexecutionofthecalledsubprogram
• Controlalwaysreturnstothecallerwhenthecalledsubprogram‘sexecution terminates
Basic Definitions
• Asubprogramdefinition describestheinterfacetoandtheactionsofthe
subprogramabstraction
• Asubprogramcallisanexplicitrequestthatthesubprogrambeexecuted
• Asubprogramheaderisthefirstpartofthedefinition,includingthename, the kind of
subprogram, and the formalparameters

• Theparameterprofile(akasignature)ofasubprogramisthenumber,order, and types of


itsparameters

• Theprotocolisasubprogram‘sparameterprofileand,ifitisafunction,its returntype
• FunctiondeclarationsinCandC++areoftencalledprototypes
• Asubprogramdeclarationprovidestheprotocol,butnotthebody,ofthe subprogram
• Aformalparameter isadummyvariablelistedinthesubprogramheaderand used in
thesubprogram

• Anactualparameterrepresentsavalueoraddressusedinthesubprogram callstatement
Actual/Formal Parameter Correspondence
• Positional
–firstactualparameterisboundtothefirstformalparameterandsoforth
Thebindingofactualparameterstoformalparametersisbyposition:the

–Safe and effective


• Keyword
–actualparameter
Thenameoftheformalparametertowhichanactualparameteristobe bound is specified with the

–Parameters can appear in any order


Formal Parameter DefaultValues
• Incertainlanguages(e.g.,C++,Ada),formalparameterscanhavedefault values (if not actual
parameter ispassed)

–positionallyassociated
InC++,defaultparametersmustappearlastbecauseparametersare
• C#methodscanacceptavariablenumberofparametersaslongastheyare of the sametype
Procedures and Functions
• There are two categories ofsubprograms
–Proceduresarecollectionofstatementsthatdefineparameterized computations
Functionsstructurallyresembleproceduresbutaresemanticallymodeledon

mathematicalfunctions

• They are expected to produce no sideeffects


• In practice, program functions have sideeffects
Design Issues forSubprograms
• What parameter passing methods areprovided?
• Are parameter typeschecked?
• Are local variables static ordynamic?
• Cansubprogramdefinitionsappearinothersubprogramdefinitions?
• Can subprograms beoverloaded?
• Can subprogram begeneric?
Local Referencing Environments
• Local variables can be stack-dynamic (bound tostorage)
–Advantages
• Support forrecursion
• Storage for locals is shared among somesubprograms
–Disadvantages
• Allocation/de-allocation, initializationtime
• Indirectaddressing
• Subprograms cannot be historysensitive
• Local variables can bestatic
–More efficient (no indirection)
–No run-time overhead
–Cannot support recursion
Parameter Passing Methods
• Waysinwhichparametersaretransmittedtoand/orfromcalled subprograms
–Pass-by-value
–Pass-by-result
–Pass-by-value-result
–Pass-by-reference
–Pass-by-name
Models of ParameterPassing

Pass-by-Value (In Mode)


• Thevalueoftheactualparameterisusedtoinitializethecorresponding formalparameter
–Normally implemented by copying
–Canbeimplementedbytransmittinganaccesspathbutnotrecommended (enforcing write
protection is noteasy)

–When copies are used, additional storage is required


–Storage and copy operations can be costly
Pass-by-Result (Out Mode)
• When a parameter is passed by result, no value is transmitted to the
subprogram;thecorrespondingformalparameteractsasalocalvariable;its
valueistransmittedtocaller‘sactualparameterwhencontrolisreturnedtothe caller

–Require extra storage location and copy operation


• Potentialproblem:sub(p1,p1);whicheverformalparameteriscopiedback will represent the
current value ofp1

Pass-by-Value-Result (inout Mode)

• A combination of pass-by-value andpass-by-result

• Sometimes calledpass-by-copy

• Formal parameters have localstorage

• Disadvantages:

–Thoseofpass-by-result
–Thoseofpass-by-value
Pass-by-Reference (Inout Mode)

• Pass an accesspath
• Also calledpass-by-sharing

• Passingprocessisefficient(nocopyingandnoduplicatedstorage)

• Disadvantages

–Sloweraccesses(comparedtopass-by-value)toformalparameters
–Potentialsforun-wantedsideeffects
–Un-wantedaliases(accessbroadened)
Pass-by-Name (Inout Mode)
• By textualsubstitution
• Formalsareboundtoanaccessmethodatthetimeofthecall,butactual binding to a value or
address takes place at the time of a reference or assignment
• Allows flexibility in latebinding
Implementing Parameter-Passing Methods
• Inmostlanguageparametercommunicationtakesplacethrutherun-time stack
• Pass-by-referencearethesimplesttoimplement;onlyanaddressisplacedin thestack
• Asubtlebutfatalerrorcanoccurwithpass-by-referenceandpass-by-value- result: a formal
parameter corresponding to a constant can mistakenly be changed

Parameter Passing Methods of Major Languages


• Fortran
–Always used the inout semantics model
–Before Fortran 77: pass-by-reference
–Fortran 77 and later: scalar variables are often passed by value-result
•C
–Pass-by-value
–Pass-by-reference is achieved by using pointers as parameters
• C++
–A special pointer type called reference type for pass-by-reference
• Java
–All parameters are passed are passed by value
–Object parameters are passed by reference

• Ada
–Threesemanticsmodesofparametertransmission:in,out,inout;inisthe defaultmode
–Formalparametersdeclaredoutcanbeassignedbutnotreferenced;those
declaredincanbereferencedbutnotassigned;inoutparameterscanbe referenced andassigned
• C#
–Default method: pass-by-value
–Pass-by-referenceisspecifiedbyprecedingbothaformalparameterandits actual parameter
withref
• PHP: very similar toC#
• Perl:allactualparametersareimplicitlyplacedinapredefinedarraynamed @_
Type Checking Parameters
• Considered very important forreliability
• FORTRAN 77 and original C:none
• Pascal,FORTRAN90,Java,andAda:itisalwaysrequired
• ANSI C and C++: choice is made by theuser
–Prototypes
• RelativelynewlanguagesPerl,JavaScript,andPHPdonotrequiretype checking
Multidimensional Arrays asParameters
• Ifamultidimensionalarrayispassedtoasubprogramandthesubprogram
isseparatelycompiled,thecompilerneedstoknowthedeclaredsizeofthat array to build the storage
mappingfunction

Multidimensional Arrays as Parameters: C and C++


• Programmerisrequiredtoincludethedeclaredsizesofallbutthefirst subscript in the
actualparameter
• Disallows writing flexiblesubprograms
• Solution:passapointertothearrayandthesizesofthedimensionsasother
parameters;theusermustincludethestoragemappingfunctionintermsof the sizeparameters

Multidimensional Arrays as Parameters: Pascal and Ada


• Pascal
–Not a problem; declared size is part of the array‘s type
• Ada
–Constrained arrays - like Pascal
–Unconstrained arrays - declared size is part of the object declaration
Multidimensional Arrays as Parameters: Fortran

• Formalparameterthatarearrayshaveadeclarationaftertheheader

–Forsingle-dimensionarrays,thesubscriptisirrelevant
–Formulti-dimensionalarrays,thesubscriptsallowthestorage-mapping function
Multidimensional Arrays as Parameters: Java and C#
• Similar toAda
• Arraysareobjects;theyareallsingle-dimensioned,buttheelementscanbe arrays
• Eacharrayinheritsanamedconstant(lengthinJava,LengthinC#)thatis
settothelengthofthearraywhenthearrayobjectiscreated Design
Considerations for ParameterPassing

• Two importantconsiderations
–Efficiency
–One-way or two-way data transfer
• But the above considerations are inconflict
–wheneverpossible
Goodprogrammingsuggestlimitedaccesstovariables,whichmeansone- way

–But pass-by-reference is more efficient to pass structures of significant size


Parameters that are Subprogram Names
• Itissometimesconvenienttopasssubprogramnamesasparameters
• Issues:
• Are parameter typeschecked?
• Whatisthecorrectreferencingenvironmentforasubprogramthatwassent as aparameter?
Parameters that are Subprogram Names: Parameter Type Checking
• CandC++:functionscannotbepassedasparametersbutpointersto
functionscanbepassed;parameterscanbetypechecked
• FORTRAN 95 typechecks
• Later versions of Pascaland
• Adadoesnotallowsubprogramparameters;asimilaralternativeisprovided via Ada‘s
genericfacility

Parameters that are Subprogram Names: Referencing Environment


• Shallowbinding:Theenvironmentofthecallstatementthatenactsthe passedsubprogram
• Deepbinding:Theenvironmentofthedefinitionofthepassedsubprogram
• Adhocbinding:Theenvironmentofthecallstatementthatpassedthe subprogram
Overloaded Subprograms
• An overloaded subprogram is one that has the same name asanother subprogram
in the same referencingenvironment

–Every version of an overloaded subprogram has a unique protocol


• C++,Java,C#,andAdaincludepredefinedoverloadedsubprograms
• In Ada, the return type of an overloaded function can be used to
disambiguatecalls(thustwooverloadedfunctionscanhavethesame parameters)

• Ada,Java,C++,andC#allowuserstowritemultipleversionsof subprograms with


the samename

Generic Subprograms
• Agenericorpolymorphicsubprogramtakesparametersofdifferenttypeson
differentactivations
• Overloaded subprograms provide ad hocpolymorphism
• Asubprogramthattakesagenericparameterthatisusedinatype
expressionthatdescribesthetypeoftheparametersofthesubprogram provides
parametricpolymorphism

Examples of parametric polymorphism: C++


template <class Type>
Type max(Type first, Type second) {
returnfirst>second?first:second;
}

• Theabovetemplatecanbeinstantiatedforanytypeforwhichoperator>is defined
int max (int first, int second) {
return first > second? first : second;
}

Design Issues forFunctions


• Are side effectsallowed?
–Parameters should always be in-mode to reduce side effect (like Ada)
• What types of return values areallowed?
–Most imperative languages restrict the return types
–C allows any type except arrays and functions
–C++ is like C but also allows user-defined types
–Ada allows any type
–Java and C# do not have functions but methods can have any type
User-Defined Overloaded Operators
• Operators can be overloaded in Ada andC++
• An Adaexample
Function―*‖(A,B:inVec_Type):returnIntegeris Sum:
Integer :=0;
begin
for Index in A‘range loop
Sum:=Sum+A(Index)*B(Index) endloop
return sum; end
―*‖;

c = a * b; -- a, b, and c are of type Vec_Type

Coroutines

• Acoroutineisasubprogramthathasmultipleentriesandcontrolsthem itself
• Alsocalledsymmetriccontrol:callerandcalledcoroutinesareonamore equalbasis
• A coroutine call is named aresume
• Thefirstresumeofacoroutineistoitsbeginning,butsubsequentcallsenter
atthepointjustafterthelastexecutedstatementinthecoroutine
• Coroutinesrepeatedlyresumeeachother,possiblyforever
• Coroutinesprovidequasi-concurrentexecutionofprogramunits(the
coroutines);theirexecutionisinterleaved,butnotoverlapped

Coroutines Illustrated: Possible Execution Control


Coroutines Illustrated: PosSHAPE \* MERGEFORMAT
sible Execution Controls
with Loops
Summary

• Asubprogramdefinitiondescribestheactionsrepresentedbythe subprogram
• Subprograms can be either functions orprocedures
• Localvariablesinsubprogramscanbestack-dynamicorstatic
• Threemodelsofparameterpassing:inmode,outmode,andinoutmode
• Some languages allow operatoroverloading
• Subprograms can begeneric
• A coroutine is a special subprogram with multipleentries
UNIT-VI
Abstract Data types
Topics

• The Concept ofAbstraction

• Introduction to DataAbstraction

• Design Issues for Abstract DataTypes

• LanguageExamples

• Parameterized Abstract DataTypes

• EncapsulationConstructs

• NamingEncapsulations

• Object-OrientedProgramming

• Design Issues for Object-OrientedLanguages

• Support for Object-Oriented Programming inSmalltalk

• Support for Object-Oriented Programming inC++

• Support for Object-Oriented Programming inJava

• Support for Object-Oriented Programming inC#

• Support for Object-Oriented Programming in Ada95

• Implementation of Object-OrientedConstructs

• ConcurrencyIntroduction

• Introduction to Subprogram-LevelConcurrency

• Semaphores

• Monitors

• MessagePassing
• Ada Support forConcurrency

• JavaThreads

• C#Threads

• Statement-LevelConcurrency

The Concept ofAbstraction


• Anabstractionisavieworrepresentationofanentitythatincludesonlythe most
significantattributes
• Theconceptofabstractionisfundamentalinprogramming(andcomputer science)
• Nearlyallprogramminglanguagessupportprocessabstractionwith subprograms
• Nearlyallprogramminglanguagesdesignedsince1980supportdata abstraction
Introduction to DataAbstraction
• Anabstractdatatypeisauser-defineddatatypethatsatisfiesthefollowing twoconditions:
Therepresentationof,andoperationson,objectsofthetypearedefinedina single syntacticunit

The representation of objects of the type is hidden from the program units

thatusetheseobjects,sotheonlyoperationspossiblearethoseprovidedinthe type'sdefinition

Advantages of DataAbstraction
• Advantage of the firstcondition
–together),
Program organization, modifiability (everything associated with a data structure is
and separate compilation
• Advantage the secondcondition
–Reliability--by hiding the data representations, user code cannot directly
accessobjectsofthetypeordependontherepresentation,allowingthe
representationtobechangedwithoutaffectingusercode
Language Requirements for ADTs
• Asyntacticunitinwhichtoencapsulatethetypedefinition
• Amethodofmakingtypenamesandsubprogramheadersvisibletoclients, while hiding
actualdefinitions
• Someprimitiveoperationsmustbebuiltintothelanguageprocessor DesignIssues
• Can abstract types beparameterized?
• What access controls areprovided?

Language Examples: Ada

• The encapsulation construct is called apackage


–Specification package (the interface)
–Body package (implementation of the entities named in the specification)
• InformationHiding
–The spec package has two parts, public and private
–Thenameoftheabstracttypeappearsinthepublicpartofthespecification
package.Thispartmayalsoincluderepresentationsofunhiddentypes
–Therepresentationoftheabstracttypeappearsinapartofthespecification called the privatepart
• More restricted form with limited privatetypes
Privatetypeshavebuilt-inoperationsforassignmentandcomparison Limited private
types have NO built-inoperations
• Reasons for the public/private specpackage:
1. Thecompilermustbeabletoseetherepresentationafterseeingonlythe spec package (it cannot
see the privatepart)
2. Clientsmustseethetypename,butnottherepresentation(theyalsocannot see the privatepart)

• Havingpartoftheimplementationdetails(therepresentation)inthespec
packageandpart(themethodbodies)inthebodypackageisnotgood

One solution: make all ADTs pointers


Problems with this:

1. Difficulties withpointers
2. Objectcomparisons
3. Control of object allocation islost

An Example in Ada package


Stack_Pack is
typestack_typeislimitedprivate;
max_size: constant :=100;
function empty(stk: in stack_type) return Boolean; procedure push(stk:
in out stack_type; elem:in Integer); procedure pop(stk: in out stack_type);
function top(stk: in stack_type) return Integer;

private -- hidden from clients


type list_type is array (1..max_size) of Integer; type
stack_type is record
list: list_type;
topsub: Integer range 0..max_size) :=0; end
record;
end Stack_Pack

Language Examples: C++


• Based on C struct type and Simula 67 classes
• The class is the encapsulationdevice
• Alloftheclassinstancesofaclassshareasinglecopyofthemember functions
• Eachinstanceofaclasshasitsowncopyoftheclassdatamembers
• Instancescanbestatic,stackdynamic,orheapdynamic Language
Examples: C++(continued)
• InformationHiding
–Private clause for hidden entities
–Public clause for interface entities
–Protectedclauseforinheritance(Chapter12) Language
Examples: C++(continued)
• Constructors:
–Functionstoinitializethedatamembersofinstances(theydonotcreatethe objects)
–May also allocate storage if part of the object is heap-dynamic
–Can include parameters to provide parameterization of the objects
–Implicitly called when an instance is created
–Can be explicitly called
–Nameisthesameastheclassname Language
Examples: C++(continued)
• Destructors
–Functionstocleanupafteraninstanceisdestroyed;usuallyjusttoreclaim heapstorage
–Implicitly called when the object‘s lifetime ends
–Can be explicitly called
–Nameistheclassname,precededbyatilde(~) An Example
inC++
class stack {
private:
int*stackPtr,maxLen,topPtr; public:
stack() { // a constructor
stackPtr=newint[100];
maxLen =99;
topPtr = -1;
};
~stack () {delete [] stackPtr;}; void
push (int num) {…}; void pop ()
{…};
int top () {…};
int empty () {…};
}

Evaluation of ADTs in C++ and Ada


• C++supportforADTsissimilartoexpressivepowerofAda
• Bothprovideeffectivemechanismsforencapsulationandinformationhiding
• Adapackagesaremoregeneralencapsulations;classesaretypes Language
Examples: C++(continued)
• Friendfunctionsorclasses-toprovideaccesstoprivatememberstosome unrelated units
orfunctions

–Necessary in C++
Language Examples:Java
• SimilartoC++,except:
–All user-defined types are classes
–Allobjectsareallocatedfromtheheapandaccessedthroughreference variables
Individualentitiesinclasseshaveaccesscontrolmodifiers(privateorpublic), rather thanclauses

Javahasasecondscopingmechanism,packagescope,whichcanbeusedin place offriends
–• Allentitiesinallclassesinapackagethatdonothaveaccesscontrol modifiers are visible
throughout thepackage
An Example in Java
class StackClass{
private:
private int [] *stackRef;
private int [] maxLen, topIndex;
publicStackClass(){//aconstructor
stackRef=new int[100];
maxLen = 99;
topPtr = -1;
};
public void push (int num) {…}; public
void pop () {…};
public int top () {…};
public boolean empty () {…};
}

Language Examples: C#
• Based on C++ andJava
• Adds two access modifiers, internal and protectedinternal
• All class instances are heapdynamic
• Default constructors are available for allclasses
• Garbagecollectionisusedformostheapobjects,sodestructorsarerarely used
• structsarelightweightclassesthatdonotsupportinheritance

Language Examples: C# (continued)


• Commonsolutiontoneedforaccesstodatamembers:accessormethods (getter andsetter)
• C#providespropertiesasawayofimplementinggettersandsetterswithout requiring explicit
methodcalls

C# Property Example
public class Weather {
publicintDegreeDays{//**DegreeDaysisaproperty get
{returndegreeDays;}
set {
if(value<0||value>30)
Console.WriteLine(
"Valueisoutofrange:{0}",value); else
degreeDays =value;}
}
private int degreeDays;
...
}
...
Weather w = new Weather();
int degreeDaysToday, oldDegreeDays;
...
w.DegreeDays = degreeDaysToday;
...
oldDegreeDays = w.DegreeDays;

Parameterized Abstract DataTypes

• ParameterizedADTsallowdesigninganADTthatcanstoreanytypeelements (among otherthings)


• Also known as genericclasses
• C++,Ada,Java5.0,andC#2005providesupportforparameterizedADTs Parameterized ADTs
inAda
• Ada GenericPackages
–Makethestacktypemoreflexiblebymakingtheelementtypeandthesizeof the stackgeneric
generic
Max_Size: Positive;
type Elem_Type is private;
package Generic_Stack is
Type Stack_Type is limited private;
function Top(Stk: in out StackType) return Elem_type;

end Generic_Stack;

Package Integer_Stack is new Generic_Stack(100,Integer); Package


Float_Stack is new Generic_Stack(100,Float);

Parameterized ADTs in C++

• Classes can be somewhat generic by writing parameterized constructor functions


class stack {

stack (int size) {
stk_ptr=newint[size];
max_len = size -1;
top = -1;
};

}

stack stk(100);
Parameterized ADTs in C++ (continued)

• Thestackelementtypecanbeparameterizedbymakingtheclassa templatedclass
template<classType> class
stack{
private:
Type *stackPtr; const
int maxLen; int topPtr;
public:
stack() {
stackPtr = new Type[100];
maxLen = 99;
topPtr = -1;
}

}

Parameterized Classes in Java 5.0


• Generic parameters must beclasses
• Mostcommongenerictypesarethecollectiontypes,suchasLinkedListand ArrayList
• Eliminate the need to cast objects that areremoved
• Eliminatetheproblemofhavingmultipletypesinastructure Parameterized
Classes in C#2005
• Similar to those of Java5.0
• Elementsofparameterizedstructurescanbeaccessedthroughindexing Summary
TheconceptofADTsandtheiruseinprogramdesignwasamilestoneinthe development oflanguages

TwoprimaryfeaturesofADTsarethepackagingofdatawiththeir associated

operations and informationhiding

• Ada provides packages that simulateADTs


• C++ data abstraction is provided byclasses
• Java‘s data abstraction is similar toC++
• Ada,C++,Java5.0,andC#2005supportparameterizedADTs
Object-Oriented Programming
• Abstract datatypes
• Inheritance
–Inheritance is the central theme in OOP and languages that support it
• Polymorphism
Inheritance
• Productivity increases can come fromreuse
–ADTs are difficult to reuse—always need changes
–All ADTs are independent and at the same level
• Inheritanceallowsnewclassesdefinedintermsofexistingones,i.e.,by allowing them to
inherit commonparts
• Inheritanceaddressesbothoftheaboveconcerns--reuseADTsafterminor changes and define
classes in ahierarchy
Object-Oriented Concepts
• ADTs are usually calledclasses
• Class instances are calledobjects
• A class that inherits is a derived class or a subclass
• Theclassfromwhichanotherclassinheritsisaparentclassorsuperclass
• Subprogramsthatdefineoperationsonobjectsarecalledmethods
Object-Oriented Concepts(continued)
• Calls to methods are calledmessages
• Theentirecollectionofmethodsofanobjectiscalleditsmessageprotocolor
message interface
• Messageshavetwoparts--amethodnameandthedestinationobject
• Inthesimplestcase,aclassinheritsalloftheentitiesofitsparent Object-Oriented
Concepts(continued)
• Inheritancecanbecomplicatedbyaccesscontrolstoencapsulatedentities
–A class can hide entities from its subclasses
–A class can hide entities from its clients
–Aclasscanalsohideentitiesforitsclientswhileallowingitssubclassesto seethem
• Besidesinheritingmethodsasis,aclasscanmodifyaninheritedmethod
–Thenewoneoverridestheinheritedone
–Themethodintheparentisoverriden
Object-Oriented Concepts (continued)
• There are two kinds of variables in aclass:
–Class variables - one/class
–Instance variables - one/object
• There are two kinds of methods in aclass:
–Class methods – accept messages to the class
–Instance methods – accept messages to objects
• Single vs. MultipleInheritance
• One disadvantage of inheritance forreuse:
–Creates interdependencies among classes that complicate maintenance Dynamic Binding
• Apolymorphicvariable canbedefinedinaclassthatisabletoreference(or
pointto)objectsoftheclassandobjectsofanyofitsdescendants
• When a class hierarchy includes classes that override methods and such
methodsarecalledthroughapolymorphicvariable,thebindingtothecorrect method will bedynamic
• Allowssoftwaresystemstobemoreeasilyextendedduringbothdevelopment andmaintenance
Dynamic Binding Concepts
• Anabstractmethodisonethatdoesnotincludeadefinition(itonlydefinesa protocol)
• An abstract class is one that includes at least one virtualmethod
• Anabstractclasscannotbeinstantiated Design
Issues for OOPLanguages
• The Exclusivity ofObjects
• Are SubclassesSubtypes
• Type Checking andPolymorphism
• Single and MultipleInheritance
• Object Allocation andDeAllocation
• Dynamic and StaticBinding
• NestedClasses
The Exclusivity ofObjects

• Everything is anobject
–Advantage - elegance and purity
–Disadvantage - slow operations on simple objects
• Add objects to a complete typingsystem
–Advantage - fast operations on simple objects
–Disadvantage - results in a confusing type system (two kinds of entities)
•Includeanimperative-styletypingsystemforprimitivesbutmakeeverything elseobjects
–Advantage-fastoperationsonsimpleobjectsandarelativelysmalltyping system
–Disadvantage - still some confusion because of the two type systems
Are Subclasses Subtypes?
• Doesan―is-a‖relationshipholdbetweenaparentclassobjectandanobject of thesubclass?
Ifaderivedclassis-aparentclass,thenobjectsofthederivedclassmust behave the same as the parent

classobject
• Aderivedclassisasubtypeifithasanis-arelationshipwithitsparentclass
–―compatible‖ways
Subclasscanonlyaddvariablesandmethodsandoverrideinherited methods in

Type Checking and Polymorphism


• Polymorphismmayrequiredynamictypecheckingofparametersandthe returnvalue
–Dynamic type checking is costly and delays error detection
• Ifoverridingmethodsarerestrictedtohavingthesameparametertypesand
returntype,thecheckingcanbestatic Single and
MultipleInheritance
• Multipleinheritanceallowsanewclasstoinheritfromtwoormoreclasses
• Disadvantages of multipleinheritance:
–Language and implementation complexity (in part due to name collisions)
–Potentialinefficiency-dynamicbindingcostsmorewithmultipleinheritance (but notmuch)
• Advantage:
–andSometimesitisquiteconvenientandvaluable
DeAllocation ofObjects
Allocation

• From where are objectsallocated?


–If they behave line the ADTs, they can be allocated from anywhere
• Allocated from the run-timestack
• Explicitly create on the heap (vianew)
–Iftheyareallheap-dynamic,referencescanbeuniformthruapointeror referencevariable
• Simplifies assignment - dereferencing can beimplicit
–If objects are stack dynamic, there is a problem with regard to subtypes
• Isdeallocationexplicitorimplicit?
Dynamic and StaticBinding
• Should all binding of messages to methods bedynamic?
–If none are, you lose the advantages of dynamic binding
–If all are, it is inefficient
• Allow the user tospecify
Nested Classes
• Ifanewclassisneededbyonlyoneclass,thereisnoreasontodefinesoit can be seen by
otherclasses

–Can the new class be nested inside the class that uses it?
–Insomecases,thenewclassisnestedinsideasubprogramratherthan directly in anotherclass
• Otherissues:
–Whichfacilitiesofthenestingclassshouldbevisibletothenestedclassand viceversa
Support for OOP in Smalltalk
• Smalltalk is a pure OOPlanguage
–Everything is an object
–All objects have local memory
–All computation is through objects sending messages to objects
–None of the appearances of imperative languages
–Allobjectedareallocatedfromtheheap All
–deallocation isimplicit
Support for OOP in Smalltalk (continued)
• Type Checking andPolymorphism
–All binding of messages to methods is dynamic
• The process is to search the object to which the message is sent for the
method;ifnotfound,searchthesuperclass,etc.uptothesystemclasswhich has nosuperclass

TheonlytypecheckinginSmalltalkisdynamicandtheonlytypeerror

occurswhenamessageissenttoanobjectthathasnomatchingmethod

Support for OOP in Smalltalk (continued)


• Inheritance
–itssuperclass
ASmalltalksubclassinheritsalloftheinstancevariables,instancemethods, and class methods of

–All subclasses are subtypes (nothing can be hidden)


–All inheritance is implementation inheritance
–No multiple inheritance
Support for OOP in Smalltalk (continued)
• Evaluation ofSmalltalk
–The syntax of the language is simple and regular
–Good example of power provided by a small language
–Slow compared with conventional compiled imperative languages
–Dynamic binding allows type errors to go undetected until run time
–Introduced the graphical userinterface
–Greatestimpact:advancementofOOP
Support for OOP in C++
• GeneralCharacteristics:
–Evolved from C and SIMULA 67
–Among the most widely used OOP languages
–Mixed typing system
–Constructors and destructors
–Elaborateaccesscontrolstoclassentities
SupportforOOPinC++(continued)
• Inheritance
–A class need not be the subclass of any class
–Access controls for members are
–Private(visibleonlyintheclassandfriends)(disallowssubclassesfrombeing subtypes)
–Public (visible in subclasses and clients)
–Protected(visibleintheclassandinsubclasses,butnotclients) Support for OOP in
C++(continued)
• Inaddition,thesubclassingprocesscanbedeclaredwithaccesscontrols
(private or public), which define potential changes in access by subclasses

–Privatederivation-inheritedpublicandprotectedmembersareprivateinthe subclasses
Publicderivationpublicandprotectedmembersarealsopublicand protected insubclasses

Inheritance Example in C++ class
base_class{
private:
int a;
floatx;
protected: int
b; floaty;
public:
int c;
floatz;
};

class subclass_1 : public base_class{ … };


// Inthisone,bandyareprotectedand
// c and z arepublic

class subclass_2 : private base_class{ … };


// Inthisone,b,y,c,andzareprivate,
// andnoderivedclasshasaccesstoany
// member of
base_classReexportation inC++
• Amemberthatisnotaccessibleinasubclass(becauseofprivatederivation)
canbedeclaredtobevisiblethereusingthescoperesolutionoperator(::),e.g.,

classsubclass_3:privatebase_class{ base_class
::c;

}
Reexportation (continued)
• One motivation for using privatederivation
–publicmembers;aderivedclassaddssomenewmembers,butdoesnotwant
A class provides members that must be visible, so they are defined to be

itsclientstoseethemembersoftheparentclass,eventhoughtheyhadtobe public in the parent


classdefinition
SupportforOOPinC++(continued)
• Multiple inheritance issupported
–scope
Iftherearetwoinheritedmemberswiththesamename,theycanbothbe referenced using the
resolutionoperator
Support for OOP in C++ (continued)
• DynamicBinding
–throughpolymorphicvariablesanddynamicallyboundtomessages
Amethodcanbedefinedtobevirtual,whichmeansthattheycanbecalled

–A pure virtual function has no definition at all


–A class that has at least one pure virtual function is an abstract class
Support for OOP in C++ (continued)
• Evaluation
–C++ provides extensive access controls (unlike Smalltalk)
–C++ provides multiple inheritance
–InC++,theprogrammermustdecideatdesigntimewhichmethodswillbe statically bound and
which must be dynamicallybound

• Static binding isfaster!


–Smalltalk type checking is dynamic (flexible, but somewhat unsafe)
–Becauseofinterpretationanddynamicbinding,Smalltalkis~10times slower thanC++
Support for OOP in Java

•BecauseofitscloserelationshiptoC++,focusisonthedifferencesfromthat language
• GeneralCharacteristics
–All data are objects except the primitive types
–All primitive types have wrapper classes that store one data value
–All objects are heap-dynamic, are referenced through reference variables, and most are allocated
with new

–Afinalizemethodisimplicitlycalledwhenthegarbagecollectorisaboutto reclaim the storage


occupied by theobject
Support for OOP in Java (continued)
• Inheritance
–thatprovidessomeofthebenefitsofmultipleinheritance(interface)
Singleinheritancesupportedonly,butthereisanabstractclasscategory

–Aninterfacecanincludeonlymethoddeclarationsandnamedconstants, e.g.,
public interface Comparable {
public int comparedTo (Object b);
}

–Methodscanbefinal(cannotbeoverriden) Support
for OOP in Java(continued)
• DynamicBinding
–overriden,
InJava,allmessagesaredynamicallyboundtomethods,unlessthemethod is final (i.e., it cannot be
therefore dynamic binding serves no purpose)

Staticbindingisalsousedifthemethodsisstaticorprivatebothofwhich disallowoverriding

Support for OOP in Java (continued)
• Several varieties of nestedclasses
• Allarehiddenfromallclassesintheirpackage,exceptforthenestingclass
• Nested classes can beanonymous
• Alocalnestedclassisdefinedinamethodofitsnestingclass
–No access specifier is used
SupportforOOPinJava(continued)
• Evaluation
–Design decisions to support OOP are similar to C++
–No support for procedural programming
–No parentless classes
–Dynamicbindingisusedas―normal‖waytobindmethodcallstomethod definitions
–Usesinterfacestoprovideasimpleformofsupportformultipleinheritance Support for OOP inC#
• Generalcharacteristics
–Support for OOP similar to Java
–Includes both classes and structs
–Classes are similar to Java‘s classes
–structsarelesspowerfulstack-dynamicconstructs(e.g.,noinheritance) Support for OOP in
C#(continued)
• Inheritance
–Uses the syntax of C++ for defining classes
–Amethodinheritedfromparentclasscanbereplacedinthederivedclassby marking its definition
withnew

–Theparentclassversioncanstillbecalledexplicitlywiththeprefixbase: base.Draw()
Support for OOP in C#
• Dynamicbinding
–To allow dynamic binding of method calls tomethods:
• The base class method is markedvirtual
• Thecorrespondingmethodsinderivedclassesaremarkedoverride
–Abstractmethodsaremarkedabstractandmustbeimplementedinall subclasses
AllC#classesareultimatelyderivedfromasinglerootclass,Object Support for OOP in

C#(continued)
• NestedClasses
–AC#classthatisdirectlynestedinanestingclassbehaveslikeaJavastatic nestedclass
–C#doesnotsupportnestedclassesthatbehavelikethenon-staticclassesof
Java
Support for OOP in C#
• Evaluation
–C# is the most recently designed C-based OO language
–ThedifferencesbetweenC#‘sandJava‘ssupportforOOParerelativelyminor Support for OOP in
Ada95
• GeneralCharacteristics
–OOP was one of the most important extensions to Ada 83
–Encapsulation container is a package that defines a tagged type
–Ataggedtypeisoneinwhicheveryobjectincludesatagtoindicateduring execution its type (the
tags areinternal)

–Taggedtypescanbeeitherprivatetypesorrecords
–Noconstructorsordestructorsareimplicitlycalled
Support for OOP in Ada 95 (continued)
• Inheritance
–Subclasses can be derived from tagged types
–Newentitiesareaddedtotheinheritedentitiesbyplacingtheminarecord definition
–All subclasses are subtypes
–No support for multiple inheritance
• Acomparableeffectcanbeachievedusinggenericclasses Example of a
TaggedType
Package Person_Pkg is
type Person is tagged private;
procedureDisplay(P:inoutPerson); private
type Person is tagged record
Name : String(1..30); Address
: String(1..30); Age : Integer;
end record;
end Person_Pkg;
with Person_Pkg; use Person_Pkg;
package Student_Pkg is
typeStudentisnewPersonwith record
Grade_Point_Average : Float;
Grade_Level : Integer;
end record;
procedureDisplay(St:inStudent);
endStudent_Pkg;

//Note:DisplayisbeingoverriddenfromPerson_Pkg Support for


OOP in Ada 95(continued)
• DynamicBinding
–Dynamicbindingisdoneusingpolymorphicvariablescalledclasswidetypes
• ForthetaggedtypePrtdon,theclasswidetypeisPerson‘class
–Other bindings are static
–Any method may be dynamically bound
–PurelyabstractbasetypescanbedefinedinAda95byincludingthe reserved wordabstract
Support for OOP in Ada 95 (continued)
• Evaluation
–Ada offers complete support for OOP
–C++ offers better form of inheritance thanAda
–Ada includes no initialization of objects (e.g., constructors)
–DynamicbindinginC-basedOOPlanguagesisrestrictedtopointersand/or
referencestoobjects;Adahasnosuchrestrictionandisthusmoreorthogonal Implementing
OOConstructs
• Two interesting and challengingparts
–Storagestructuresforinstancevariables
–Dynamicbindingofmessagestomethods
Instance Data Storage
• Classinstancerecords(CIRs)storethestateofanobject
–Static (built at compile time)
• Ifaclasshasaparent,thesubclassinstancevariablesareaddedtothe parentCIR
• BecauseCIRisstatic,accesstoallinstancevariablesisdoneasitisin records
–Efficient
Dynamic Binding of Methods Calls
• MethodsinaclassthatarestaticallyboundneednotbeinvolvedintheCIR;
methodsthatwillbedynamicallyboundmusthaveentriesintheCIR

–theCIR
Callstodynamicallyboundmethodscanbeconnectedtothecorresponding code thru a pointer in

–The storage structure is sometimes called virtual method tables (vtable)


–Methodcallscanberepresentedasoffsetsfromthebeginningofthevtable
Summary

• OO programming involves three fundamental concepts: ADTs, inheritance, dynamicbinding


• Major design issues: exclusivity of objects, subclasses and subtypes, type
checkingandpolymorphism,singleandmultipleinheritance,dynamicbinding, explicitandimplicitde-
allocationofobjects,andnestedclasses
• Smalltalk is a pureOOL
• C++ has two distinct type system(hybrid)
• JavaisnotahybridlanguagelikeC++;itsupportsonlyOOprogramming
• C# is based on C++ andJava
• Implementing OOP involves some new datastructures

You might also like