Sunteți pe pagina 1din 5

IntroductionofCompilerDesign

Translator

Atranslatorisaprogramthattakesinputaprogramwritteninoneprogramminglanguage
andproduceasoutputaprograminanotherlanguage.

Needoftranslator

Machinelanguageisintheformofbitsitsverytoughtocommunicate.
Itstoughtomakeprogram.
Itsterriblytedioustoremoveerror.

Compiler

Compilerisaprogramwhichtranslatesaprogramwritteninonelanguage(thesource
language)toanequivalentprograminotherlanguage(thetargetlanguage).

Translatesfromonerepresentationoftheprogramtoanother
Typicallyfromhighlevelsourcecodetolowlevelmachinecodeorobjectcode
Sourcecodeisnormallyoptimizedforhumanreadability
Expressive:matchesournotionoflanguages(andapplication?!)
Redundanttohelpavoidprogrammingerrors
Machinecodeisoptimizedforhardware
Redundancyisreduced
Informationabouttheintentislost

Bootstrapping:Acompilercanbecharacterizedbythreelanguages:thesourcelanguage
(S),thetargetlanguage(T),andtheimplementationlanguage(I).ThethreelanguagesS,I,
andTcanbequitedifferent.Suchacompileriscalledcrosscompiler
Compilersareoftwokinds:nativeandcross.
Nativecompilersarewritteninthesamelanguageasthetargetlanguage.Forexample,
SMMisacompilerforthelanguageSthatisinalanguagethatrunsonmachineMand
generatesoutputcodethatrunsonmachineM.
Crosscompilersarewrittenindifferentlanguageasthetargetlanguage.Forexample,SNM
isacompilerforthelanguageSthatisinalanguagethatrunsonmachineNandgenerates
outputcodethatrunsonmachineM.
MajorPartsofaCompiler:Therearetwomajorpartsofacompiler:AnalysisandSynthesis.

Analysisphase:Anintermediaterepresentationiscreatedfromthegivensource
program.Lexicalanalyzer,syntaxanalyzerandsemanticanalyzerarepartofthe
analysisphase.
Synthesisphase:Anequivalenttargetprogramiscreatedfromtheintermediate
representation.Intermediatecodegenerator,codegeneratorandcodeoptimizerare
partofsynthesisphase.

PhasesofaCompiler:Lexicalanalyzerreadsthesourceprogramcharacterbycharacter
andreturnsthetokensofthesourceprogram.Itputsinformationaboutidentifiersintothe
symboltable.

Eachphasedoesthefollowing.

Transformsthesourceprogramfromonerepresentationintoanotherrepresentation.
Theycommunicatewitherrorhandlers.
Theycommunicatewiththesymboltable.

LexicalAnalyzer:readsthesourceprogramcharacterbycharacterandreturnsthetokensof
thesourceprogram.
Atokendescribesapatternofcharactershavingsamemeaninginthesourceprogram.
(Suchasidentifiers,operators,keywords,numbers,delimitersandsoon)

Putsinformationaboutidentifiersintothesymboltable.
Regularexpressionsareusedtodescribetokens(lexicalconstructs).
A(Deterministic)FiniteStateAutomatoncanbeusedintheimplementationofalexical
analyzer.

Syntaxanalyzer:

Asyntaxanalyzercreatesthesyntacticstructure(generallyaparsetree)ofthegiven
program.
Asyntaxanalyzerisalsocalledasaparser.
Aparsetreedescribesasyntacticstructure.

Inaparsetree,allterminalsareatleaves.
Allinnernodesarenonterminalsinacontextfreegrammar.

Thesyntaxofalanguageisspecifiedbyacontextfreegrammar(CFG).
TherulesinaCFGaremostlyrecursive.
Asyntaxanalyzercheckswhetheragivenprogramsatisfiestherulesimpliedbya

CFGornot.

Ifitsatisfies,thesyntaxanalyzercreatesaparsetreeforthegivenprogram.
Ex:WeuseBNF(BackusNaurForm)tospecifyaCFGassgstmt>identifier:=
expressionexpression>identifierexpression>numberexpression>
expression+expression

Semanticanalyzer:
Asemanticanalyzerchecksthesourceprogramforsemanticerrorsandcollectsthe
typeinformationforthecodegeneration.
Typecheckingisanimportantpartofsemanticanalyzer.
Normallysemanticinformationcannotberepresentedbyacontextfreelanguageused
insyntaxanalyzers.
Contextfreegrammarsusedinthesyntaxanalysisareintegratedwithattributes
(semanticrules)
Theresultisasyntaxdirectedtranslation,
Attributegrammars
Ex:newval:=oldval+12
Thetypeoftheidentifiernewvalmustmatchwithtypeofthe

expression(oldval+12)

Thefollowingcanbedoneduringsemanticanalysis:
Checksemantics
Errorreporting
Disambiguateoverloadedoperators
Typecoercion
Staticchecking
Typechecking
Controlflowchecking
Uniquenesschecking
Namechecks

Intermediatecodegeneration:

Acompilermayproduceanexplicitintermediatecodesrepresentingthesource
program.
Theseintermediatecodesaregenerallymachine(architectureindependent).Butthe
levelofintermediatecodesisclosetothelevelofmachinecodes.
Example:newval:=oldval*fact+1id1:=id2*id3+1MULT
id2,id3,temp1IntermediatesCodes(Quadruples)ADDtemp1,#1,temp2MOV
temp2,,id1

Codeoptimizer:

Thecodeoptimizeroptimizesthecodeproducedbytheintermediatecodegeneratorin
thetermsoftimeandspace.
Ex:MULTid2,id3,temp1ADDtemp1,#1,id1
NostrongcounterpartwithEnglish,butissimilartoediting/prciswriting
Automaticallymodifyprogramssothatthey
Runfaster
Uselessresource(memory,registers,space,fewerfetchesetc.)
Somecommonoptimizations:
Commonsubexpressionelimination
Copypropagation
Deadcodeelimination
Codemotion
Strengthreduction
Constantfolding

Codegenerator:

Producesthetargetlanguageinaspecificarchitecture.
Thetargetprogramisnormallyisarelocatableobjectfilecontainingthemachine
codes.
Ex:(assumethatwehavearchitecturewithinstructionswhoseatleastoneofits
operandsisamachineregister)MOVEid2,R1MULTid3,R1ADD#1,R1MOVER1,id1

Usuallyatwostepprocess

Generateintermediatecodefromthesemanticrepresentationoftheprogram
Generatemachinecodefromtheintermediatecode

Theadvantageisthateachphaseissimple

1.Requiresdesignofintermediatelanguage
2.Mostcompilersperformtranslationbetweensuccessiveintermediaterepresentations
3.Intermediatelanguagesaregenerallyorderedindecreasinglevelofabstractionfrom
highest(source)tolowest(machine)
4.However,typicallytheoneaftertheintermediatecodegenerationisthemostimportant

S-ar putea să vă placă și