Definition
Therearetwowaysofunderstandingcompilerphrases.
1.Compilingaprogramisanaction,basedontheprincipleofcompilationtechnology,translatedintoamachinelanguagebinarycodebyahigh-levelprogramminglanguagecompiler.
Second,thecompilerisagerund,specificallyreferringtothesoftwareprogramthatgeneratesthecompiler.
Introduction
Compiler
Theimplementationalgorithmofthecompilerismorecomplicated.Thisisbecausethesentenceittranslatesisnotaone-to-onecorrespondencewiththeinstructionsofthetargetlanguage,butaone-to-manycorrespondence;atthesametime,ithastodealwithrecursivecalls,dynamicstorageallocation,multipledatatypes,andclosedependenciesbetweenstatementsrelation.However,becauseprogramswritteninhigh-levelprogramminglanguagesareeasytoread,easytotransplant,andhavestrongexpressivecapabilities,compilersarewidelyusedtotranslatesourceprogramswritteninhigh-levellanguagesthatarelargeinscale,complex,andrequireefficientoperation..
Function
Thebasicfunctionofthecompileristotranslatethesourceprogram(high-levellanguage)intothetargetprogram.However,asacompilingsystemwithpracticalapplicationvalue,inadditiontobasicfunctions,itshouldalsohaveimportantfunctionssuchasgrammarchecking,debuggingmeasures,modificationmethods,coverageprocessing,targetprogramoptimization,combinationofdifferentlanguages,andhuman-machinecontact.①Grammarcheck:Checkwhetherthesourceprogramconformstothegrammar.Ifitdoesnotconformtothegrammar,thecompilershouldpointoutthelocation,natureandrelatedinformationofthegrammaticalerror.Thecompilershouldenabletheusertogetonthemachineonceandbeabletofindasmanyerrorsaspossible.②Debuggingmeasures:checkwhetherthesourceprogramconformstothedesigner'sintention.Forthisreason,thecompilerisrequiredtoplacesomeoutputinstructionsinthecompiledtargetprogramsothatitcanoutputinformationaboutthedynamicexecutionoftheprogramwhenthetargetprogramisrunning,suchasthechangeofvariablevaluesandthelinesexperiencedduringprogramexecution.Thisinformationhelpsuserstoverifyandverifywhetherthesourceprogramexpressesthealgorithmrequirements.③Modificationmeans:provideuserswithsimplemeanstomodifythesourceprogram.Compilersusuallyprovidemeansforbatchmodification(usedtomodifyalargenumberoferrorsthatarenoteasytomodifytemporarily)andon-sitemodificationmethods(usedtomodifyasmallnumberoferrorsthataretemporarilyeasytomodifyduringruntime).④Coveringprocessing:Itismainlysetuptodealwithlargeproblemprogramswithlongproceduresandlargeamountsofdata.Thebasicideaistoletsomeprogramsegmentsanddatasharecertainstorageareas,inwhichonlythecurrentlyusedprogramsordataarestored;therestoftheprogramsanddatathatarenotusedtemporarilyarefirststoredinauxiliarystoragesuchasdisks,andthendynamicallyloadedwhenneeded..⑤Targetprogramoptimization:Improvethequalityofthetargetprogram,thatis,itoccupieslessstoragespaceandtheprogramrunsforashorttime.Accordingtodifferentoptimizationgoals,thecompilercanchoosetoimplementexpressionoptimization,loopoptimization,orprogramglobaloptimization.Someofthetargetprogramoptimizationisperformedatthesourceprogramlevel,andsomeareperformedatthetargetprogramlevel.⑥Differentlanguagesareusedtogether:itsfunctionhelpsuserswriteapplicationprogramsinmultipleprogramminglanguagesorapplyexistingprogrammoduleswrittenindifferentlanguages.Themostcommonisthecombinationofhigh-levellanguageandassemblylanguage.Thiscannotonlymakeupforthedifficultyofexpressingcertainnon-numericalprocessingoperationsordirectcontrol,accesstoperipheraldevicesandhardwareregistersinhigh-levellanguages,butalsofacilitatethecompilationofcorepartsoftheprograminassemblylanguagetoimproveoperatingefficiency.⑦Human-machinecontact:Whendeterminingtheimplementationofthecompilerprogramtoachieveawell-designedfunction.Thepurposeistofacilitateuserstounderstandtheinternalworkingconditionsintimeduringthecompilationandoperationstages,andtoeffectivelysuperviseandcontroltheoperationofthesystem.Theearlyimplementationofthecompilerprogramwastocompletelyincorporatetheabove-mentionedfunctionsinthecompilerprogram.However,thecustomarypracticeistoconfigurethedebugger,edittheprogram,andconnecttheassemblyprogramwiththesupportoftheoperatingsystemtoassistinthedebugging,modification,andcoverageprocessingoftheprogram,aswellasthefunctionofcombiningdifferentlanguages.However,whendesigningthecompiler,itisstillnecessarytocarefullyconsiderissuessuchashowtoconnectwiththesesubsystems.
Features
Thecompilermustanalyzethesourceprogramandthensynthesizeitintothetargetprogram.First,checkthecorrectnessofthesourceprogramanddecomposeitintoanumberofbasiccomponents;secondly,establishcorrespondingequivalenttargetprogrampartsbasedonthesebasiccomponents.Inordertocompletethesetasks,thecompilermustestablishsometablesintheanalysisstage,andtransformthesourceprogramintoanintermediatelanguageform,sothatitiseasytoquoteandprocessduringanalysisandsynthesis.
Themaindatastructureusedindatastructureanalysisandsynthesis,includingsymboltable,constanttableandintermediatelanguageprogram.Thesymboltableconsistsoftheidentifiersusedinthesourceprogramandtheirattributes.Theattributesincludetypes(suchasvariables,arrays,structures,functions,procedures,etc.),types(suchasintegers,realtypes,strings,complextypes,labels)Etc.),andotherinformationrequiredbythetargetprogram.Theconstanttableiscomposedoftheconstantsusedinthesourceprogram,includingtheinternalrepresentationoftheconstantsandthetargetprogramaddressesassignedtothem.Anintermediatelanguageprogramisanintermediateformofprogramthatisintroducedbeforethesourceprogramistranslatedintothetargetprogram.Thechoiceofitsrepresentationdependsonhowtouseandprocessitinthefuture.CommonlyusedintermediatelanguageformsincludePolishrepresentation,triples,quadruples,andindirecttriples.
Analysisofpartofthesourceprogramisrealizedthroughthreestepsoflexicalanalysis,syntaxanalysisandsemanticanalysis.Lexicalanalysisiscompletedbyalexicalanalysisprogram(alsocalledascanningprogram).Itstaskistorecognizewords(namely,identifiers,constants,reservedwords,andvariousoperators,punctuationmarks,etc.),createsymboltablesandconstanttables,andconvertThesourcecodeescapeistheinternalformofthecompilerwhichiseasytoanalyzeandprocess.Thegrammaticalanalysisprogramisthecorepartofthecompiler,anditsmaintaskistocheckwhetherthesourceprogramconformstothegrammaraccordingtothegrammaticalrulesofthelanguage.Ifitdoesnotconformtothegrammar,outputthegrammaticalerrormessage;ifitconformstothegrammar,thendecomposethegrammaticalstructureofthesourceprogramandconstructaninternalprogramintheformofanintermediatelanguage.Thepurposeofgrammaticalanalysisistograsphowwordsformsentences,andhowsentencesformprograms.Thesemanticanalysisprogramistofurthercheckthesemanticcorrectnessofthelegalprogramstructure.Itspurposeistoensurethecorrectuseofidentifiersandconstants,collectandsavenecessaryinformationinthesymboltableorintermediatelanguageprogram,andperformcorrespondingsemanticprocessing.
Workingprocess
Thecompilerisalsocalledthecompilationsystem,whichisalanguageprocessingprogramthattranslatesaprocess-orientedsourceprogramwritteninahigh-levellanguageintoatargetprogram.Thecompilertranslatesasourceprogramintoatargetprograminfivestages:lexicalanalysis;syntaxanalysis;intermediatecodegeneration;codeoptimization;targetcodegeneration.Itismainlyforlexicalanalysisandgrammaticalanalysis,alsoknownassourceprogramanalysis.Intheprocessofanalysis,agrammaticalerrorisfoundandpromptinformationisgiven.
(1)Lexicalanalysis
Thetaskoflexicalanalysisistoprocesswordscomposedofcharacters,scanthesourceprogramcharacterbycharacterfromlefttoright,andgenerateonebyoneThewordsymboltransformsthesourceprogramasacharacterstringintoanintermediateprogramofthewordsymbolstring.Theprogramthatperformslexicalanalysisiscalledalexicalanalysisprogramorscanner.
Thewordsymbolsinthesourceprogramareanalyzedbythescanner,andabinaryexpressionisgenerallygenerated:wordtype;valueoftheworditself.Wordtypesareusuallycodedbyintegers.Ifatypecontainsonlyonewordsymbol,thenforthiswordsymbol,thetypecodecompletelyrepresentsitsownvalue.Ifacategorycontainsmanywordsymbols,thenforeachofitswordsymbols,inadditiontothecategorycode,itshouldalsogiveitsownvalue.
Generallyspeaking,lexicalanalyzerscanbeconstructedintwoways:manualconstructionandautomaticgeneration.Manualconstructioncanusestatediagramstowork,andautomaticgenerationcanbeachievedusingcertainfiniteautomata.
(2)Syntaxanalysis
Thesyntaxanalyzerofthecompilertakeswordsymbolsasinput,andanalyzeswhetherthewordsymbolstringformsagrammaticalunitthatconformstothegrammaticalrules,suchasexpressions,assignments,Loop,etc.,andfinallyseewhetheritconstitutesaprogramthatmeetstherequirements.Accordingtothegrammaticalrulesofthelanguage,itisanalyzedtocheckwhethereachsentencehasthecorrectlogicalstructure.Theprogramisthefinalgrammaticalunit.Thegrammaticalrulesofthecompilercanbedescribedbyacontext-freegrammar.
Therearetwomethodsofgrammaticalanalysis:top-downanalysisandbottom-upanalysis.Fromtoptobottomistostartfromthebeginningsymbolsofthegrammar,derivedownwards,anddeducesentences.Thebottom-upanalysismethodusestheshift-inreductionmethod.Thebasicideais:useafirst-in-and-last-outofregisteredsymbolstomovetheinputsymbolsintothestackonebyone.WhenthetopofthestackformsacertainproductionInthecaseofacandidate,thepartatthetopofthestackisreducedtotheleft-neighborsymboloftheproduction.
(3)Intermediatecodegeneration
Intermediatecodeisaninternalrepresentationofthesourceprogram,orintermediatelanguage.Thefunctionoftheintermediatecodeistomakethestructureofthecompilerlogicallysimplerandclearer,especiallytomaketheoptimizationofthetargetcodeeasiertoachieve.Theintermediatecodeistheintermediatelanguageprogram,andthecomplexityoftheintermediatelanguageliesbetweenthesourceprogramlanguageandthemachinelanguage.Therearemanyformsofintermediatelanguage,commononesareinversePolishnotation,quaternion,ternaryandtree.
(4)Codeoptimization
Codeoptimizationreferstoavarietyofequivalenttransformationstotheprogram,sothatamoreeffectivetargetcodecanbegeneratedfromthetransformedprogram.Theso-calledequivalencemeansthattherunningresultoftheprogramisnotchanged.Theso-calledeffectivemainlyreferstotheshortrunningtimeofthetargetcodeandthesmallstoragespaceoccupied.Thistransformationiscalledoptimization.
Therearetwotypesofoptimization:oneistooptimizetheintermediatecodeaftergrammaticalanalysis,whichdoesnotdependonthespecificcomputer;theotheristobeperformedwhenthetargetcodeisgenerated,whichistoalargeextentItdependsonthespecificcomputer.Fortheformertypeofoptimization,itcanbedividedintothreedifferentlevels:localoptimization,loopoptimizationandglobaloptimizationaccordingtothescopeoftheprograminvolved.
(5)Targetcodegeneration
Targetcodegenerationisthelaststageofcompilation.Thetargetcodegeneratortransformsthesyntacticallyanalyzedoroptimizedintermediatecodeintotargetcode.Therearethreeformsofobjectcode:
①Machinelanguagecodethatcanbeexecutedimmediately,alladdressesarerelocated;
②Themachinelanguagemoduletobeassembled,whenitneedstobeexecuted,isconnectedTheloaderconnectsthemwithsomerunningprogramsandconvertsthemintoexecutablemachinelanguagecodes;
③Theassemblylanguagecodesmustbeassembledbytheassemblertobecomeexecutablemachinelanguagecodes.
Inthetargetcodegenerationstage,threeissuesthatdirectlyaffectthespeedofthetargetcodeshouldbeconsidered:oneishowtogenerateashortertargetcode;theotherishowtomakefulluseoftheregistersinthecomputertoreducethetargetcode'saccesstothestorageunitThethirdishowtomakefulluseofthecharacteristicsofthecomputerinstructionsystemtoimprovethequalityofthetargetcode.
Synthesispart
Thesynthesisstagemustgeneratethetargetprogramaccordingtothesymboltableandtheintermediatelanguageprogram,anditsmainworkincludescodeoptimization,storageallocationandcodegeneration.Codeoptimizationisthroughrearrangingandchangingcertainoperationsintheprogramtoproduceamoreeffectivetargetprogram.Thetaskofstorageallocationistoallocateruntimestorageunitsforprogramsanddata.Themaintaskofcodegenerationistogeneratethetargetprogramequivalenttotheintermediatelanguageprogramsymbol,processtheintermediatelanguageprogramsequentially,andgenerateaseriesofassemblylanguageormachinelanguageinstructionsusingtheinformationinthesymboltableandconstanttable.
Structure
Thecompilationprocessisdividedintotwoparts:analysisandsynthesis,andisfurtherdividedintosixparts:lexicalanalysis,syntaxanalysis,semanticanalysis,codeoptimization,storageallocationandcodegeneration.Successivelogicalsteps.Thesesixstepsonlyrepresentthelogicalconnectionbetweenthevariouspartsofthecompiler,notthetimerelationship.Thecompilationprocesscanbeexecutedinsequenceaccordingtothesesixlogicsteps,orcanbeexecutedinaparallelinterlockingmanner.Whendeterminingthespecificstructureofthecompiler,itisoftenimplementedinseveralpasses.Forasourceprogramoranintermediatelanguageprogram,ascanfrombeginningtoendtoachievethespecifiedworkiscalledapass.Eachpasscancompleteoneorseverallogicalsteps.Forexample,youcanuselexicalanalysisasthefirstpass;syntaxanalysisandsemanticanalysisasthesecondpass;codeoptimizationandstorageallocationasthethirdpass;andcodegenerationasthefourthpass.Conversely,inordertoadapttoasmallerstoragespaceorimprovethequalityofthetargetprogram,theworkofalogicalstepcanalsobedividedintoseveralpassestoexecute.Forexample,codeoptimizationcanbedividedintotwosteps:codeoptimizationpreparationworkandactualcodeoptimization.
Whetheracompilerprogramisdivided,andhowtodivideit,dependsonthespecificsituation.Thecriterioncanbethesizeofthestoragecapacity,thecomplexityofthesourcelanguage,thewidthoftheproblem-solvingrange,andthenumberofdesignandpreparationstaff.Theadvantagesofsplittingarethatthefunctionsofeachpassareindependentandsimple,theinterconnectionissimple,thelogicalstructureisclear,andtheoptimizationpreparationissufficient.Thedisadvantageisthattherewillinevitablybesomerepetitivepartsineachpass,andtheremustbehandoverworkbetweenpasses,whichincreasesthelengthofthecompilerandthecompilationtime.
Compilingtheprogramonceisanextremecase.Theentirecompilerresidesinmemoryatthesametime,andisconnectedtoeachotherbycalltransfer(Figure2).Whenasyntaxanalysisprogramneedsanewsymbol,itcallsalexicalanalysisprogram;whenitrecognizesacertaingrammaticalstructure,itcallsasemanticanalysisprogram.Thesemanticanalysisprogramperformssemanticinspectionontheidentifiedstructure,andcallsthe"storageallocation"and"codegeneration"programstogeneratethecorrespondingtargetlanguageinstructions.
Withthedevelopmentofprogramminglanguageinformal,structured,intuitiveandintelligentaspects,asacompilerthatrealizesthecorrespondinglanguagefunctions,itisalsodevelopingtowardsthegoalofautomaticprogramminginordertoprovideTheidealprogrammingtool.
ReferenceBibliography
EditedbyChenHuowang,QianJiahua,andSunYongqiang:"PrinciplesofCompilation",NationalDefenseIndustryPress,Beijing,1980.
AVAho,PrinciplesofCompilerDesign,AddisonWes-ley,Reading,Massachusetts,1977.
Dynamic
Afterthe1980s,programminglanguageIthasmadegreatprogressanddevelopmentinformalization,structuring,visualizationandintelligence,mainlyintwoaspects:①Withthedevelopmentofprogrammingtheoryandmethods,aseriesofnewprogramminglanguageshavebeenintroduced,suchasStructuredprogramminglanguage,concurrentprogramminglanguage,distributedprogramminglanguage,functionalprogramminglanguage,intelligentprogramminglanguage,object-orientedprogramminglanguage,etc.;②Basedontheresearchresultsofgrammar,semanticsandpragmatics,fromDifferentanglesandlevelsdeeplyrevealtheinternallawsandexternalmanifestationsofprogramminglanguages.Correspondingly,thecompiler,whichisoneoftheimportantmeanstorealizeprogramminglanguage,hasdeveloped,changedandexpandedtovaryingdegreesintermsofarchitecture,designideas,realizationtechnologyandprocessingcontent.
Inaddition,thecompilerhasbeenincludedinthebasicsoftwaretoolsofthesoftwaresupportenvironmentasanimportantsoftwaretoolforprogramming.Therefore,whenplanningtheimplementationofthecompiler,youshouldstartfromthespecificsoftwaresupportenvironmentyouarein.Itisnecessarytocomplywiththeoverallrequirementsandregulationsoftheentireenvironment,butalsotocarefullyconsiderthemutualsupport,cooperationandconnectionwithothersoftwaretools.relation.