欢迎来到天天文库
浏览记录
ID:51326894
大小:126.00 KB
页数:33页
时间:2020-03-22
《东南大学 编译原理 教学教案 Cp6.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter6TypeCheckingZhaiYuqingZhouXiaoyuOverviewStaticCheckingCheckthatthesourceprogramfollowsboththesyntaxand(static)semanticconventionsofthesourcelanguage.ExamplesofStaticCheckTypechecks(incompatibleoperand)Flow-of-controlchecks(breakstatement)Uniquenesschecks(uniquelydeclaredidentifier)Name-r
2、elatedchecksPositionofTypeCheckerAtypecheckerverifiesthatthetypeofaconstructmatchesthatexpectedbyitscontext.Typeinformationgatheredbyatypecheckermaybeneededwhencodeisgenerated.parserTokenstreamTypecheckerSyntaxtreeIntermediatecodegeneratorSyntaxtreeIntermediaterepresentationOverloadAsymbolthatca
3、nrepresentdifferentoperationsindifferentcontextissaidtobe“overloaded”TypeSystemsTypeExpressions(1)(1)Abasictypeisatypeexpression.(2)Atypenameisatypeexpression.(3)Atypeconstructorappliedtotypeexpressionisatypeexpression.Constructorsinclude:Arrays,products,records,pointers,functionsTypeExpressions
4、(2)(4)Typeexpressionsmaycontainvariableswhosevaluesaretypeexpressions.Exampletyperow=recordaddress:integer;lexeme:array[1..15]ofcharend;Record((address*integer)*(lexeme*array(1..15,char)))Examplevarp:rowpointer(row)mod(i,j:int):intint*intintRepresentatypeexpressionbyagraphfunctionf(a,b:char):
5、integerchar*charpointer(integer)TypeSystemsAtypesystemisacollectionofrulesforassigningtypeexpressionstothevariouspartsofaprogram.(1)Atypecheckerimplementsatypesystem.(2)Thetypesystemsarespecifiedinasyntax-directedmanner.StaticandDynamicCheckingofTypesStaticChecking(Donebyacompiler)DynamicChecki
6、ng(donewhenthetargetprogramruns)table:array[0..255]ofchar;i:integer;…table[i]:=‘a’StaticandDynamicCheckingofTypesStronglytypedlanguage(ifitscompilercanguaranteethattheprogramsitacceptswillexecutewithouttypeerror)SpecificationofaSimpleTypeCheckeraSimpleLanguageGrammarfortheSourceLanguagePD;EDD;
7、D
8、id:TTchar
9、integer
10、array[num]ofT
11、TEliteral
12、num
13、id
14、EmodE
15、E[E]
16、ETranslationScheme(1)PD;EDD;DDid:T{addtype(id.entry,T.type)}Tchar{T.type=char}Tinteger{T.type=integer}TranslationScheme(2)Tarray[num]ofT1{T.type=array(1
此文档下载收益归作者所有