程序设计语言复习-矿大-程序设计语言原理_第1页
程序设计语言复习-矿大-程序设计语言原理_第2页
程序设计语言复习-矿大-程序设计语言原理_第3页
程序设计语言复习-矿大-程序设计语言原理_第4页
程序设计语言复习-矿大-程序设计语言原理_第5页
已阅读5页,还剩70页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.3LanguageEvaluationCriteriaReadabilityThemostimportantcriteriumFactors:OverallsimplicityToomanyfeaturesisbadMultiplicityoffeaturesisbadOperatoroverloadingOrthogonalityMakesthelanguageeasytolearnandreadMeaningiscontextindependentArelativelysmallsetofprimitiveconstructscanbecombinedinarelativelysmallnumberofwaysEverypossiblecombinationislegalLackoforthogonalityleadstoexceptionstorules正交性:指只用该语言的一组相对少量的根本结构,经过相对少的结合步骤,就可构成该语言的控制结构和数据结构。而且,它的根本结构的任何组合都是合法和有意义的。count=count+1count+=1count++++countCopyright©2006ShujuanJiang.Allrightsreserved.*1.3LanguageEvaluationCriteriaReadabilityfactors(continued)Controlstatements(goto)DefiningdatatypesandstructuresTimeout=1×Timeout=trueSyntaxconsiderationsIdentifierformsSpecialwordsFormandmeaning(C:static;UNIX:grep)Pascal:begin…..endC:{}Ada:if……endifloop……endloopCopyright©2006ShujuanJiang.Allrightsreserved.*1.3LanguageEvaluationCriteriaWritabilityFactors:SimplicityandorthogonalitySupportforabstraction(subprogram,binarytree)Expressivity(C:count++,Ada:andthen(布尔表达式的短路求值))ReliabilityFactors:TypecheckingExceptionhandlingAliasingReadabilityandwritabilityCopyright©2006ShujuanJiang.Allrightsreserved.*1.3LanguageEvaluationCriteriaCostCategoriesTrainingprogrammerstouselanguageWritingprogramsCompilingprogramsExecutingprograms(优化)LanguageimplementationsystemReliabilityMaintainingprogramsOthers:portability,generality,well-definedness

Copyright©2006ShujuanJiang.Allrightsreserved.*1.5LanguageCategoriesImperativeCentralfeaturesarevariables,assignmentstatements,anditerationC,PascalFunctionalMainmeansofmakingcomputationsisbyapplyingfunctionstogivenparametersLISP,SchemeCopyright©2006ShujuanJiang.Allrightsreserved.*1.5LanguageCategoriesLogicRule-basedRulesarespecifiedinnospecialorderPrologObject-orientedEncapsulatedataobjectswithprocessingInheritanceanddynamictypebindingGrewoutofimperativelanguagesC++,JavaCopyright©2006ShujuanJiang.Allrightsreserved.*1.7ImplementationMethodsCompilationTranslatehigh-levelprogramtomachinecodeCCOBOLAdaSlowtranslationFastexecutionBottleneck:thespeedoftheconnectionbetweenacomputer’smemoryanditsprocessor.Copyright©2006ShujuanJiang.Allrightsreserved.*Chapter2TopicsTheIBM704andFORTRANTheFirstStepTowardSophistication:ALGOL60TheBeginningsofDataAbstraction:SIMULA67Hisotry’sLargestDesignEffort:AdaObject-OrientedProgramming:SmalltalkCopyright©2006ShujuanJiang.Allrightsreserved.*5.2Names(cont.)SpecialwordsAnaidtoreadability;usedtodelimit(界定)orseparatestatementclausesDef:A

keywordisawordthatisspecialonlyincertaincontexts.i.e.inFortran:RealVarName(Realisdatatypefollowedwithaname,thereforeRealisakeyword)

Real=3.4(Realisavariable) Disadvantage:poorreadabilityDef:Areservedwordisaspecialwordthatcannotbeusedasauser-definednameCopyright©2006ShujuanJiang.Allrightsreserved.*5.2Names(cont.)SpecialwordsPredefinednames(预定义的名字)Betweenreservedwordanduser-definednamesTheyhavepredefinedmeaningbutcanberedefinedbytheuserForexample,printfscanfinCCopyright©2006ShujuanJiang.Allrightsreserved.*5.3VariablesAvariableisanabstractionofamemorycellVariablescanbecharacterizedasasextuple(六个局部)ofattributes:(name,address,value,type,lifetime,scope)Name-notallvariableshavethem(anonymous(匿名的))Copyright©2006ShujuanJiang.Allrightsreserved.*两个变量具有按名等价的类型当且仅当它们一起被说明,或者用相同的类型标识符说明,即对应的类型名相同。如:typeTisarray[1..100]ofINTEGER;(Ada)x,y:array[1..100]ofINTEGER;〔P198〕z:array[1..100]ofINTEGER;r,w:T;i,j:FLOAT;按名等价:r与w,i与jx与y、x与z按名不等价,z与r按名不等价.按名等价(Nametypecompatibility)Copyright©2006ShujuanJiang.Allrightsreserved.*两个变量具有按定义等价的类型当且仅当两个变量有完全相同的类型〔包括相同的域名〕。除类型名外的其他特征均相同者即视为按定义等价。如typecomplex=recordre:int;im:intend;Typerational=recordnominator:int;denominator:1..299999end;按定义等价(declarationequivalence)Copyright©2006ShujuanJiang.Allrightsreserved.*varx:complex;z:recordre:int;im:intend;u:rational;v:recordrepart:int;denominator:1..299999end;

x与z按名不等价,但按定义等价。

u与v按定义不等价按定义等价Copyright©2006ShujuanJiang.Allrightsreserved.*

两个变量具有按结构等价的类型当且仅当变量的所有分量有相同类型的值集。在上例中,u和v第二个分量的值集是相同的,所以按结构等价。x和u呢?按结构等价虽然第二个分量的值均为整数,但其类型不同,所以不是按结构等价的。Copyright©2006ShujuanJiang.Allrightsreserved.*programexample;vara,b:integer;……

proceduresubl;varx,y:integer;begin{subl}…..1end;{subl}

proceduresub2;varx:integer;……

proceduresub3;varx:integer;begin{sub3}……2end;{sub3}

begin{sub2}……..3

end;{sub2}begin{example}……4end.{example}Point

Referencing

Environmentxandyofsubl,aandb

ofexamplexofsub3,(xofsub2ishidden),aandbofexamplexofsub2,aandbofexample4aandbofexampleInastatic-scopedlanguageCopyright©2006ShujuanJiang.Allrightsreserved.*voidsubl(){inta,b;…….1}/*endofsubl*/voidsub2(){intb,c;……2

subl();}/*endofsub2*/voidmain(){intc,d;……3

sub2();}/*endofmain*/

Point

ReferencingEnvironment1aandbofsub1,cofsub2,dofmain,,(cofmainandbofsub2arehidden)2bandcofsub2,dofmain,(cofmainishidden)3canddofmainFunctioncall:Main--->sub2---->sub1Inadynamic-scopedlanguageCopyright©2006ShujuanJiang.Allrightsreserved.*6.10PointerTypesPointerProblemsDanglingPointers(悬挂指针):isapointerthatcontainstheaddressofaheap-dynamicvariablethathasbeendeallocated(解除分配).

Danglingpointersaredangerous

thelocationmayhavebeenreallocatedthevalueofthenewheap-dynamicvariablewillbedestroyedstoragemanagertofail

LostHeap-dynamicVariables:isanallocatedheap-dynamicvariablethatisnolongeraccessibletotheuserprogram.Suchvariablesarecalledgarbage(垃圾)Copyright©2006ShujuanJiang.Allrightsreserved.*OperandEvaluationOrder

Ifneitheroftheoperandsofanoperatorhassideeffects,thenoperandevaluationorderisirrelevant(不相关的)

Sideeffects

Def:

functional

side

effect,occurswhenthefunctionchangeseitheroneofitsparametersoraglobalvariable

Example

a=10;10+5=15(fromlefttoright)b=a+fun(a);20+5=25(fromrighttoleft)funreturnsthevalueofitsargumentdividedby2andchangesitsparametertohavethevalue207.2ArithmeticExpressions

Copyright©2006ShujuanJiang.Allrightsreserved.*7.2ArithmeticExpressions

Example

inta=5;

intfunI(){a=17;return3;}/*offun1*/

voidfun2(){a=a+funl();}/*offun2*/voidmain(){fun2();}/*ofmain*/Fromlefttoright:A=5,fun1=3,result=8Fromrighttoleft:Fun1=3,a=17,result=20Copyright©2006ShujuanJiang.Allrightsreserved.*在表达式中出现的函数调用可能出现“副作用〞,即可能存在同一个赋值语句中的同名量的值不相同,所以在串行流程中必须规定计算次序。计算次序如下:求位置求位置求位置(计算下标)(计算下标)(计算下标)左端1:=左端2:=……:=右端;赋值赋值赋值7.2ArithmeticExpressions

Copyright©2006ShujuanJiang.Allrightsreserved.*Examplei,j:integer;A,B:array[1..100]ofinteger;Functionf(x:integer):integer;begini:=i+1;j:=j+2;f:=x+1end7.2ArithmeticExpressions

Copyright©2006ShujuanJiang.Allrightsreserved.*例:i:=3;j:=0;A[i]:=B[f(j)+i]:=i+f(j)+i*j(1)求A[i]的位置,i=3;(2)求B[f(j)+i]的位置,j=0,f(0)=0+1=1,同时有,i=3+1=4,j=0+2=2(副作用),

即f(j)+i=5,B[f(j)+i]=B[5],(3)表达式的值为:4+f(2)+(4+1)*(2+2)=27,在求f(2)时i=4+1=5,j=2+2=4(副作用)

(4)将27赋给B[5](5)将27赋给A[3]A[i]:=B[i+f(j)]:=i+f(j)+i*j结果=?7.2ArithmeticExpressions

Copyright©2006ShujuanJiang.Allrightsreserved.*i:=3;j:=0;A[i]:=B[i+f(j)]:=i+f(j)+i*j(1)求A[i]的位置,i=3;(2)求B[i+f(j)]的位置,j=0,f(0)=0+1=1,即i+f(j)=3+1=4,B[i+f(j)]=B[4],同时有,i=3+1=4,j=0+2=2(副作用),

(3)表达式的值为:4+f(2)+(4+1)*(2+2)=27,在求f(2)时i=4+1=5,j=2+2=4(副作用)

(4)将27赋给B[4](5)将27赋给A[3]7.2ArithmeticExpressions

Copyright©2006ShujuanJiang.Allrightsreserved.*9.1IntroductionTwofundamentalabstractionfacilities(设施)canbeincludedinaprogramminglanguage:processabstractionanddataabstractionProcessabstractionhasbeenacentralconceptinallprogramminglanguages.Thefirstprogrammablecomputer,in1940,hadthecapability(能力)ofreusingcollectionsofinstructioncardsatseveraldifferentplacesinaprogramwhenthatwasconvenient(方便的)Thisreuseresultsinseveraldifferentkindsofsavings,frommemoryspacetocodingtime.Thisincreasesthereadabilityofaprogram.Copyright©2006ShujuanJiang.Allrightsreserved.*BasicDefinitions(con.)ForexampleFORTRAN:SUBROUTINEADDER(parameters)

Ada:procedureADDER(parameters)C:voidadder(parameters)Theparameter

profile(参数轮廓)

ofasubprogramisthenumber,order,andtypesofitsformalparameters(形式参数).Theprotocol(协议)

ofasubprogramisitsparameterprofileplus,ifitisafunction,itsreturntype.9.2FundamentalsofSubprogramsCopyright©2006ShujuanJiang.Allrightsreserved.*Parameters

Therearetwowaysthatasubprogramgains(得到)accesstothedata:Throughdirectaccesstononlocal(非局部的)variables

ThroughparameterpassingParameterpassingismoreflexible(灵活的)thandirectaccesstononlocalvariables.Extensive(大量)accesstononlocalscancausereducedreliability(可靠性)Theparametersinthesubprogramheaderarecalledformal

parameters.Theyareboundtostorageonlywhenthesubprogramiscalled.9.2FundamentalsofSubprogramsCopyright©2006ShujuanJiang.Allrightsreserved.*Parameters(Con.)

Subprogramcallstatementsmustincludethenameofthesubprogramandalistofparameterstobeboundtotheformalparametersofthesubprogram(actual

parameters.).positional

parameters:thecorrespondence(对应)betweenactualandformalparametersisdonebysimpleposition

keyword

parameters:thenameoftheformalparametertowhichanactualparameteristobeboundisspecifiedwiththeactualparameter.(Ada)9.2FundamentalsofSubprogramsCopyright©2006ShujuanJiang.Allrightsreserved.*Variablesthataredefinedinsidesubprogramsarecalledlocalvariables.(eitherstaticorstackdynamic)Def:Thenonlocal

variablesofasubprogramarethosethatarevisiblewithinthesubprogrambutarenotlocallydeclared.Def:

Global

variablesarethosethatarevisibleinallprogramunits.9.4LocalReferencingEnvironmentsCopyright©2006ShujuanJiang.Allrightsreserved.*9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ValueThevalueoftheactualparameterisusedtoinitializethecorrespondingformalparameterActsasalocalvariableinthesubprogramPass-by-valueisnormallyimplementedbyactualdatatransfer.Thedisadvantage:AdditionalstoragefortheformalparameterThestorageandthemoveoperationscanbecostlyiftheparameterislargePass-by-valueisimplementedbytransmittinganaccesspathtothevalueoftheactualparameterrequirethatthevaluebeinawrite-protectedcell

9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ResultPass-by-resultisanimplementationmodelforout-modeparameters.NovalueistransmittedtothesubprogramThecorrespondingformalparameteractsasalocalvariable,justbeforecontrolistransferredbacktothecaller,itsvalueispassedbacktothecaller'sactualparameter

AlsorequirestheextrastorageandthecopyoperationsBeingimplementedbydatatransfer,theproblemisinensuringthattheinitialvalueoftheactualparameterisnotusedinthecalledsubprogram.9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Result(con.)Oneproblemwiththepass-by-resultmodelisthattherecanbeanactualparametercollisionsub(p1,p1)〔调用语句中〕AssumingthetwoformalparametershavedifferentnamesTheorderinwhichtheactualparametersareassigneddeterminestheirvalueBecausetheorderisusuallyimplementationdependent,portability(可移植性)problemscanoccurthataredifficulttodiagnose(诊断)Anotherproblemistochoosetwodifferenttimestoevaluatetheaddressesoftheactualparameters(atthetimeofthecalloratthetimeofthereturn)9.5Parameter-PassingMethodsForexample:

list[index]Ifindexischangedbythesubprogram,thentheaddressoflist[index]willchangebetweenthecallandthereturnCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-Value-ResultPass-by-value-resultisanimplementationmodelforinout-modeparametersinwhichactualvaluesaremoved.Acombinationofpass-by-valueandpass-by-result.Pass-by-value-resultissometimescalledpass-by-copybecausetheactualparameteriscopiedtotheformalparameteratsubprogramentryandthencopiedbackatsubprogramtermination.Disadvantagesofrequiringmultiplestorageforparametersandtimeforcopyingvalues.Theproblemsassociatedwiththeorderinwhichactualparametersareassigned.9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*COPY机制:机制实参进入时出去时功能功能传值方式表达式X:=实参

传结果方式变量实参:=x传值--结果方式变量X:=实参实参:=x传值方式、传结果方式、传值--结果方式通称为传递形式参数的COPY机制。形参x被指定为被调用过程的一个局部变量;进入时将实参值复制给x,出去时将x的值复制给实参。X如同一个局部变量一样,它的值可以取出和更新,进入时创立出去时删除。9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ReferenceThepass-by-referencemethodtransmitsanaccesspath,justanaddresstothecalledsubprogram.Advantagesisthatthepassingprocessitselfisefficient,intermsofbothtimeandspace.Duplicatespaceisnotrequired,norisanycopying.Disadvantageslikelybeslowerbecauseindirectaddressingifonlyonewaycommunicationtothecalledsubprogramisrequired,Inadvertent(不注意的)anderroneous(错误的)changesmaybemadeAnotherseriousproblemofpass-by-referenceisthataliasescanbecreated.9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ReferenceseveralwaysaliasescanbecreatedCollisions(冲突)canoccurbetweenactualparametersvoidfun(int*first,int*second)fun(&total,&total)Collisionsbetweenarrayelementscanalsocausealiasesfun(&list[i],&list[j])ifI==jCollisionsbetweenarray-elementparametersandelementsofarrayspassedasarray-nameparametersfun1(&list[i],&list)9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-Referenceseveralwaysaliasescanbecreated(con.)Collisionsbetweenformalparametersandnonlocalvariables9.5Parameter-PassingMethodsint*global;voidmain(){extern

int*global;……

sub(global);

}voidsub(int*local){externint*global;……}Insidesub,localandglobalarealiases.Copyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-NamePass-by-nameisaninout-modeparametertransmissionmethod.Whenparametersarepassedbyname,theactualparameteristextuallysubstitutedforthecorrespondingformalparameterinallitsoccurrencesinthesubprogram.Apass-by-nameformalparameterisboundtoanaccessmethodatthetimeofthesubprogramcall,buttheactualbindingtoavalueoranaddressisdelayeduntiltheformalparameterisassignedorreferenced.9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Name〔con.〕Iftheactualparameterisascalarvariable(标量变量),thenpass-by-nameisequivalenttopass-by-reference.Iftheactualparameterisaconstantexpression,thenpass-by-nameisequivalenttopass-by-valueIftheactualparameterisanarrayelement,pass-by-namemaybedifferentfromanyothermethodIftheactualparameterisanexpressionthatcontainsavariable,pass-by-nameisagaindifferentfromanyothermethod9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Name〔con.〕9.5Parameter-PassingMethods

beginLIST[1]:=2;LIST[2]:=2;GLOBAL:=1;SUB(LIST[GLOBAL])

end;procedureBIGSUB;integerGLOBAL;integer

arrayLIST[1:2];procedureSUB(PARAM);integerPARAM;beginPARAM:=3;GLOBAL:=GLOBAL+1;PARAM:=5end;List[1]=3;List[2]=5;Copyright©2006ShujuanJiang.Allrightsreserved.*传名方式〔callbyname〕用实参替换形参后的过程体来代替原过程体。必要时,变量必须重命名〔如果同名时〕。例:procedureroots(a:float)isd:float;begind:=discr(a,a,a)//假设discr已定义过endroots那么用传名机制来调用roots(x),有一个分程序:d:float;begind:=discr(x,x,x)//假设discr已定义过endroots9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Name〔con.〕Theprimaryadvantageofpass-by-nameistheflexibilityThemaindisadvantageistheslownessoftheprocess9.5Parameter-PassingMethodsCopyright©2006ShujuanJiang.Allrightsreserved.*再谈参数传递(按引用调用)例如语句global:=1;PROC(global);

执行时把指向global的指针传递给形式参数pp最后global=0ProcedurePROC(pp);begin

pp:=0; pp:=(pp+global)*1000;endPROC;

Copyright©2006ShujuanJiang.Allrightsreserved.*再谈参数传递〔按值调用〕例如ProcedurePROC(pp);begin pp:=0; pp:=(pp+global)*1000;endPROC;

语句global:=1;PROC(global);

执行时把global的值1的传递给形式参数pp最后global=1Copyright©2006ShujuanJiang.Allrightsreserved.*再谈参数传递〔按值-结果调用〕例如ProcedurePROC(pp);begin pp:=0; pp:=(pp+global)*1000;endPROC;

语句global:=1;PROC(global);

执行时:

global

ppppglobal最后global=1000Copyright©2006ShujuanJiang.Allrightsreserved.*再谈参数传递〔例1〕简单变量与常量(在C语言中,在子程序头部前面带有*的形式参数是按引用传递,而没有*的是按值传递。以下图中I是按值传递,j是按引用传递)两个打印语句的结果是什么?结果是:1213213Copyright©2006ShujuanJiang.Allrightsreserved.*当P调用Q时,实际参数表达式a和&b被计值,实际传递的参数是a的右值和b的左值。由于a是按值传递的,形式参数i被表示为Q中的局部整数变量,并且当子程序Q开始执行时,a的值在调用时被作为初值赋予i。之后,a和i不再有联系。当i被赋为新值12时,a不会改变。当调用Q结束后,a的值仍为2再谈参数传递〔例1〕参数b是按引用传递。这意味着j是Q中指向整数的指针类型的局部变量。当Q开始执行时,指向数据对象b的指针作为j的右值来存放。当10加到j的值中时,j本身并不改变。每次对j的引用〔j的右值即b的左值〕紧跟着一个指针值的选取操作,使得对j的引用实际上是访问数据对象b的地址。Copyright©2006ShujuanJiang.Allrightsreserved.*虽然看起来对j的赋值类似于对i的赋值,但是它意味着完全不同。实际参数b的值改变为13,当形式参数i和j的值在Q中打印时,结果是12和13,当返回到P时,对应于实际参数a和b的值被打印,仅有b的值被修改。当Q终止时,在Q中赋予i的值12被丧失了,因为Q中的局部变量在结束时被删除了。当然j的值也被丧失了,但是这是指针而非值13.再谈参数传递〔例1〕Copyright©2006ShujuanJiang.Allrightsreserved.*Pascal:

typevect=array[1..3]ofinteger;procedureQ(k:vect;varl:vect);varn:integer;begink[2]:=k[2]+10;l[2]:=l[2]+10;forn:=1to3dowrite(k[n]);forn:=1to3dowrite(l[n]);end;再谈参数传递〔例2〕

procedureP;varc,d:vect;m:integer;beginc[1]:=6;c[2]:=7;c[3]:=8;d[1]:=6;d[2]:=7;d[3]:=8;Q(c,d);form:=1to3dowrite(c[m]);form:=1to3dowrite(d[m]);end;当P执行时,打印的结果是什么?617861786786178Copyright©2006ShujuanJiang.Allrightsreserved.*Def:An

overloaded

subprogramisasubprogramthathasthesamenameasanothersubprograminthesamereferencingenvironment.Everyversionofanoverloadedsubprogrammusthaveaunique(唯一的)protocol;Itmustbedifferentfromtheothersinthenumber,order,ortypesofitsparameters,orinitsreturntypeifitisafunction.C++,Java,andAdaincludepredefinedoverloadedsubprograms.InAda,thereturntypeofanoverloadedfunctionisusedtodisambiguate(消除…的歧义)calls.

9.7OverloadSubprogramsCopyright©2006ShujuanJiang.Allrightsreserved.*Thecapabilityofcompilingpartsofaprogramwithoutcompilingthewholeprogramisessentialtotheconstructionoflargesoftwaresystems.Thepartsofprogramthatcanbecompiledaresometimescalledcompilationunits(编译单元).Thetermseparatecompilation(分别编译)

meansthatcompilationunitscanbecompiledatdifferenttimes,buttheircompilationsarenotindependentofeachotherifeitheraccessesorusesanyentitiesoftheother.9.9SeparateandIndependentCompilationCopyright©2006ShujuanJiang.Allrightsreserved.*Usingwith,theprogrammerspecifiestheexternalunitsthatthecodeinthecompilationmustaccess

withGLOBALS,TEXT_IO;procedureEXAMPLEis……endEXAMPLE;FORTRAN90alsoallowsseparatecompilationofitssubprogramsandmodules.Withindependent

compilation,programunitscanbecompiledwithoutinformationaboutanyotherprogramunits.9.9SeparateandIndependentCompilationCopyright©2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCallsandReturns

Subprogramlinkage:ThesubprogramcallandreturnoperationsofalanguageAsubprogramcallmustincludethemechanism

-Iflocalvariablesarenotstatic,itmustcausestoragetobeallocatedforthelocalsdeclaredinthecalledsubprogramandbindthosevariablestothatstorage-Itmustsavetheexecutionstatusofthecallingprogramunit

Copyright©2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

Asubprogramcallmustincludethemechanism(continue)-Itmusttransfercontroltothecodeofthesubprogramandreturntotheproperplacewhenthesubprogramexecutioniscompleted.-Thecallmustcausesomemechanismtobecreatedtoprovideaccesstononlocalvariablesthatarevisibletothecalledsubprogram.Copyright©2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

AsubprogramReturnmustincludethemechanismIfthesubprogramhasparametersthatareoutmodeandareimplementedbycopyFirst,movelocalvaluesoftheassociatedformalparameterstotheactualparametersNext,ItmustdeallocatethestorageusedforlocalvariablesandrestoretheexecutionstatusofthecallingprogramunitThen,returnthemechanismusedfornonlocalreferencestotheconfigurationithadbeforethecallFinally,

controlmustbereturnedtothecallingprogramunitCopyright©2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCallsandReturns

Subprogramlinkage:ThesubprogramcallandreturnoperationsofalanguageAsubprogramcallmustincludethemechanism

-Iflocalvariablesarenotstatic,itmustcausestoragetobeallocatedforthelocalsdeclaredinthecalledsubprogramandbindthosevariablestothatstorage-Itmustsavetheexecutionstatusofthecallingprogramunit

Copyright©2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

Asubprogramcallmustincludethemechanism(continue)-Itmusttransfercontroltothecodeofthesubprogramandreturntotheproperplacewhenthesubprogramexecutioniscompleted.-Thecallmustcausesomemechanismtobecreatedtoprovideaccesstononlocalvariablesthatarevisibletothecalledsubprogram.Copyright©2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

AsubprogramReturnmustincludethemechanismIfthesubprogramhasparametersthatareoutmodeandareimplementedbycopyFirst,movelocalvaluesoftheassociatedformalparameterstotheactualparametersNext,ItmustdeallocatethestorageusedforlocalvariablesandrestoretheexecutionstatusofthecallingprogramunitThen,returnthemechanismusedfornonlocalreferencestotheconfigurationithadbeforethecallFinally,

controlmustbereturnedtothecallingprogramunitCopyright©2006ShujuanJiang.Allrightsreserved.*10.3ImplementingSubprogramsin

ALGOL-likeLanguages

InthecaseoftheALGOL-likelanguages,activationrecordinstancesmustbecreateddynamicallyCopyright©2006ShujuanJiang.Allrightsreserved.*Thereturnaddressoftenconsistsofapointertothecodesegmentofthecallerandanoffsetaddressinthatcodesegmentoftheinstructionfollowingthecall(调用语句之后那条指令的偏移地址)Thestatic

link(sometimescalledastatic

scopepointer)pointstothebottomoftheactivationrecordinstanceofanactivationofthe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论