Description
The interpreter mode has been widely used in the compiler of object-oriented language implementation. However, this mode is applicable to the construction of large grammar interpretation, the drawbacks are many, so very few other aspects of use. For example, a string such as "1+2+3-4" is entered into a python console, but Python does not recognize these strings, and it needs to define a set of grammar rules to interpret the strings, that is, to design a custom language.
Interpreter mode: Given a language, define a representation of its grammar and define an interpreter that uses that representation to interpret sentences in the language. The "language" referred to in the definition of the interpreter pattern is code that uses the prescribed format and syntax.
Structure of the Interpreter pattern
The interpreter pattern consists of the following 4 roles: Abstractexpression (abstract expression) terminalexpression (terminator expression) nonterminalexpression (non-terminator expression) context (Environment Class)
Instance:
#In order to develop a guitar simulator that automatically recognizes sheet music, the results can be achieved by spectral sounding by the input spectrum. #In addition to the audible device (assuming it is done), the most important is the ability to read and interpret the spectrum. #analysis of their needs, the whole process can be divided into two parts:#translate the contents of a spectrum according to the rules; Play according to the content of the translation. #we use an interpreter model to complete this function. classPlaycontext (): Play_text=NoneclassExpression ():definterpret (self, context):ifLen (context.play_text) = =0:return Else: Play_segs=context.play_text.split (" ") forPlay_seginchPlay_segs:pos=0 forEleinchplay_seg:ifEle.isalpha ():#verifies whether a string consists of only lettersPos+=1Continue BreakPlay_chord=Play_seg[0:pos] Play_value=Play_seg[pos:] Self.execute (play_chord,play_value)defExecute (self,play_key,play_value):PassclassNormguitar (Expression):defExecute (Self, key, value):Print("Normal Guitar playing--chord:%s Play tune:%s"%(key,value))#The Playcontext class is the content of the spectrum, which contains only one field and no method. #Expression , which contains only two methods, interpret is responsible for interpreting the spectrum, and execute is responsible for playing, and the Normguitar class is written in Execute and played by guitar. #The business scenario is as follows:if __name__=="__main__": Context=playcontext () Context.play_text="C53231323 Em43231323 F43231323 G63231323"Guitar=Normguitar () Guitar.interpret (context)
Printing results:
Normal Guitar playing--chord:c Play tune:53231323
Normal Guitar playing--chord:em Play tune:43231323
Normal Guitar playing--chord:f Play tune:43231323
Normal Guitar playing--chord:g Play tune:63231323
Pattern Benefits
Easy to change and expand grammar easy to implement a simple language implementation grammar easier (with automatic generation tools) Add new explanatory expressions more convenient
Pattern disadvantage
Difficult to maintain execution efficiency for complex grammars
Mode applicable environment
You can represent a sentence in a language that needs to be interpreted as an abstract syntax tree some recurring problems can be expressed in a simple language the grammar of a language is simpler to implement efficiency is not a key issue
python-Interpreter mode