Interpreter

Executionmethod

Python,TCLandvariousShellprogramsaregenerallyexecutedusinganinterpreter.Microsoft'sQbasiclanguageisalsoaninterpretationmethod,itcannotgenerateexecutableprograms(butQuickBasicandVisualBasiccan);thewidelyusednetworkprogramminglanguagejavahasbothinterpretationandcompilationmethods.

Beforewestart,itisnecessarytoemphasizeagain:theinterpreterdescribedbelowisasourcecodeinterpreter.Inotherwords,whentheinterpreterisexecuting,itreadsonestatementatatime,andperformsaspecificoperationbasedonthisstatement;thenreadsthenextstatement,andsoon.Thisisdifferentfromthepseudo-codeinterpreter,suchastheearlyJavaruntimesystem.Thedifferencebetweenthetwois:thesourcecodeinterpreterdirectlyinterpretsandexecutesthesourcecodeoftheprogram;whilethepseudo-codeinterpreterfirstconvertsthesourcecodeoftheprogramintosomekindofintermediatecodethathasnothingtodowiththemachine,andthenexecutestheintermediatecode.Incontrast,asourcecodeinterpreteriseasiertocreateanddoesnotrequireaseparatecompilationprocess.

Subsystems

SmallBASICinterpreterincludestwomainsubsystems:oneistheexpressionparser,whichisresponsibleforprocessingnumericexpressions;theotheristheinterpreter,whichisresponsiblefortheprogramActualimplementation.Fortheformer,theexpressionparserintroducedinChapter2ofthisbookcanbeused.Butsomeimprovementshavebeenmadeheresothattheparsercanparsenumericexpressionscontainedinprogramstatements,ratherthanonlyparseisolatedexpressions.

Theinterpretersubsystemandtheparsersubsystemarecontainedinthesameinterpreterclass,whichisnamedSBasic.Althoughtheoreticallyyoucanusetwoindependentclasses:onecontainstheinterpreterandtheothercontainstheexpressionparser;butthegenerationefficiencywillbehigherifthetwoareimplementedinthesameclass,becausetheexpressionparserandtheinterpretationThecodeofthedeviceisinseparable.Forexample,bothsubsystemsoperateonthesamecharacterarraythatholdstheprogramcode.Iftheyarearrangedintwocategories,itwillincreaseconsiderableadditionaloverhead,andcauseperformancelossandfunctionalduplication.Inaddition,sincethetaskofprograminterpretationisheavyandparsingexpressionsisonlypartofit,itmakessensetoincludetheentireinterpretationmechanisminasingleclass.

Interpreter

Whentheinterpreterexecutes,itreadsanidentifierfromthesourcecodeoftheprogrameachtime.Ifwhatisreadisakeyword,theinterpreterwillperformthespecifiedoperationinaccordancewiththerequirementsofthekeyword.Forexample,whentheinterpreterreadsaPRINT,itwillprintthecharactersafterthePRINT;whenitreadsaGOSUB,itwillexecutethespecifiedsubroutine.Thisprocesswillberepeatedbeforereachingtheendoftheprogram.Asyoucansee,theinterpretersimplyperformstheactionsspecifiedbytheprogram.

Interpretationandcompilation

Theinterpreterrunstheprogram:

1.Directlyrunthehigh-levelprogramminglanguage(suchastheshell'sowninterpreter)

2.Convertthehigh-levelprogramminglanguagecodetosomeefficientbytecode,andrunthesebytecodes

3.Compilethehigh-levellanguagewiththecompilerincludedintheinterpreter,Andinstructtheprocessortorunthecompiledprogram(forexample:JIT)

Perl,Python,MATLAB,andRubybelongtothesecondmethod,whileUCSDPascalbelongstothethirdmethod.Intheprocessoftranslation,theprogramswritteninthisgroupofhigh-levellanguages​​arestillmaintainedinthesourcecodeformat(orsomerelaylanguageformat),andtheactionsorbehaviorsreferredtobytheprogramitselfarerepresentedbytheinterpreter.

Usinganinterpretertoruntheprogramwillbeslowerthanrunningthecompiledmachinecodedirectly,buttherelativebehaviorofthisliteraltranslationwillbefasterthancompilingandthenrunning.Thisisespeciallyimportantintheprototypestageofprogramdevelopmentandwhenwritingexperimentalcode,becausethis"edit-literal-debug"cycleisusuallymoretime-savingthanthe"edit-compile-run-debug"cycle.many.

Runningtheprogramontheinterpreterisslowerthanrunningthecompiledcodedirectly,becausetheinterpretermustanalyzeandinterprettheprogramlineitrunseverytime,andthecompiledprogramisjustRundirectly.Thisanalysisatruntimeiscalled"literalcost".Intheinterpreter,variableaccessisalsorelativelyslow,becauseeverytimeitwantstoaccessavariable,itmustfindoutwherethevariableisactuallystored,unlikethecompiledprogramthatdeterminesthevariablewhenitiscompiled.Location.

Therearemanycompromisesbetweenusinganinterpretertoachievefasterdevelopmentspeedandusingacompilertoachievefasterrunningprogress.Somesystems(suchassomeLISP)allowliteralandcompiledcodetocalleachotherandsharevariables.Thismeansthatonceasubroutinehasbeentestedintheinterpreterandthemissesareeliminated,itcanbecompiledtogetfasterexecutionprogress.Manyinterpretersdonotruntheoriginalcodeasthenamesuggests,butinsteadconverttheoriginalcodeintoamorecompressedinternalformat.Forexample,someBASICinterpretersreplacekeywordswithasinglebytesymbolthatcanbeusedtofindthecorrespondinginstructioninthejumptable.Theinterpretercanalsousealexicalanalyzerandaparserlikeacompiler,andthentranslatetheabstractsyntaxtreethatisgenerated.

Goodportability,literaltranslationprogramshavebetterportabilitythancompiledprograms,andcanbeeasilyrunondifferentsoftwareandhardwareplatforms.Thecompiledprogramisonlylimitedtorunningonthedevelopmentenvironmentplatform.

Byteinterpretation

Consideringthetimerequiredtoanalyzetheprogrambeforerunning,thereisapossibilitybetweenliteraltranslationandcompilation.Forexample,sourcecodewritteninEmacsLispwillbecompiledintoahighlycompressedandoptimizedLispsourcecodeformat.Thisisakindofbytecode,whichisnotmachinecode(soitwillnotbeTiedtospecifichardware).This"compiled"codewillthenbetranslatedbyabytecodeinterpreter(writteninC).Inthiscase,this"compiled"codecanbesaidtobethemachinecodeofavirtualmachine(notrealhardware,butabytecodeinterpreter).ThismethodisusedintheForthcodeusedbytheOpenFirmwaresystem:theoriginalprogramwillbecompiledinto"Fcode"(abytecode),andthenliterallytranslatedandrunbyavirtualmachineonaspecificplatform.

Related Articles
TOP