{"title":"基于解释器的数学表达式的面向对象实现","authors":"Jing Shi","doi":"10.38007/proceedings.0000084","DOIUrl":null,"url":null,"abstract":"In this paper, the basic concept of design pattern is introduced at first. Through the detailed introduction of the behavior pattern of Interpreter pattern, an object-oriented realization of mathematical expression is designed by using it. At last, the advantages and disadvantages of the Interpreter pattern are summarized. 1.Design Pattern Overview Design pattern is a combination of methodology and a group of abstract patterns used to design reusable object-oriented software in the field of object-oriented software design in recent years. Design pattern is a very powerful tool, which uses the accumulated knowledge and experience of programmers to solve the programming problems that people often encounter, and provides a high-level scheme that is easy to reuse and maintainable. If we use design pattern flexibly, it can save us a lot of time. A design pattern provides a solution to a specific problem. In object-oriented programming, a design pattern has a specific structure. With this structure, the program architecture is more flexible, the code can be reused and maintained more easily, and the program has better adaptability. Design patterns make it more convenient for designers to improve or reuse some past mature designs and architectures. It will be proved by practice that it will make it easier for new system developers to understand their design ideas. As long as designers understand the design pattern, they can absorb the valuable experience in the pattern to a large extent, and have a better understanding of the object-oriented system. At the same time, these patterns can be directly used to guide the critical modeling problems in object-oriented systems. If you have the same problem background, you can apply it directly. More specifically, through the use of design patterns, it can enhance the reuse function of the packaged classes, and effectively handle the change of requirements. Some patterns can reduce the coupling and dependence between classes. More importantly, designers familiar with design patterns can give them new design ideas. 2.Interpreter --Behavior Pattern of Class The interpreter is the behavior pattern of a class. Given any language, the explain mode can define a indication of its grammar and provide an interpreter, which uses the indication to interpret the sentences in the language. The client can use this interpreter to interpret sentences in this language. In the event of a specially appointed mold of problem happens frequently, it may be deserve expressing each instance of the problem as a sentence in a simple language. This allows you to build an interpreter, this interpreter is used to interpret these sentences, so as to solve such problems. How to define a grammar for a simple language can be described by an interpreter pattern,The Interpreter pattern describes how to define a grammar for a simple language, and how to express a sentence in the language, and how to interpret these sentences. Composition of Interpreter pattern: 1) Abstract expression role: declare an abstract interpretation operation, which is implemented by 2020 International Conference on Social and Human Sciences (ICSHS2020) DOI: 10.38007/Proceedings.0000084 ISBN: 978-1-80052-000-4 465 all concrete expression roles (nodes in the abstract syntax tree). Each node of the abstract syntax tree represents a statement, and interpretation methods can be executed on each node. The execution of this interpretation method represents that the statement is interpreted. Because each statement is interpreted on behalf of this statement. Since each statement represents an instance of a common problem, the explanation operation on each node represents an answer to a problem instance. 2) Terminator expression role: specific expression. a) Implement the interpretation operation associated with the terminator in grammar b) And each terminator in a sentence requires an instance of the class to correspond to it 3) Nonterminal expression role: concrete expression. a) Every rule in grammar R::=R1R2...Rn requires a non terminator table band role b) Maintain an instance variable for the abstract expression role for each symbol from R1 to RN c)To implement the interpretation operation, it is generally necessary to call the interpretation operation representing the objects from R1 to RN recursively 4) Context (environment) role: contains some global information outside the interpreter. 5) Customer roles: a) Construct (or be given) an abstract syntax tree that represents a specific sentence in the grammar defined language b) Call interpretation operation Interpreter mode has its applicability: When you need to interpret and execute a language and represent sentences in that language in an abstract syntax tree, you can use the pattern of interpreter . When existing the following conditions this mode effects best : • For complex grammar, the class level of grammar becomes huge and cannot be managed. At this point, a tool like parser generator is a better choice. In this way, the abstract syntax tree can interpret the corresponding expressions without being built, which can save a lot of time and space. • The high efficiency interpreter for a key problem is general not implemented by directly explaining the parsing tree, but at first converting them into another kind of shape. For example, this form of state machine is transformed by regular expression. Even though in this case, the converter can still be realized in the mode of interpreter, which is still useful. 3.Realization of Mathematical Expression Through Interpreter Pattern Each grammar rule of the Interpreter pattern is represented by a class. Regular expression is a standard language for describing string patterns. Instead of constructing a specific algorithm for each pattern, it is better to use a general search algorithm to interpret and execute a regular expression defines be matched. We also use regular expressions to express mathematical expressions. We define grammar and use Interpreter pattern to complete the object-oriented implementation of mathematical expressions. When the mathematical expression is implemented through interpreter mode, the grammar is defined as follows: expression ::= variableexp | literal | addexp | subexp | mulexp | divexp | extract rootexp | squexp | absolute valueexp `(`expression`)` addexp ::= expression `add` expression subexp ::= expression `sub` expression mulexp ::= expression `mul` expression divexp ::= expression `div` expression extract rootexp ::= expression `extract root` expression squexp ::= expression `squ` expression literal ::= `1` | `2` | `3` |... variableexp ::= `A` | `B` | ... | `X` | `Y` | `Z` In the grammar above, the symbol expression is the start symbol and the literal is the terminator that defines a simple word. In this language, the terminators are numbers, i.e. 1, 2, 3, etc. A","PeriodicalId":202744,"journal":{"name":"2020 International Conference on Social and Human Sciences (ICSHS2020)","volume":"50 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"1900-01-01","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"0","resultStr":"{\"title\":\"Object-oriented Realization of Mathematical Expression Based on Interpreter\",\"authors\":\"Jing Shi\",\"doi\":\"10.38007/proceedings.0000084\",\"DOIUrl\":null,\"url\":null,\"abstract\":\"In this paper, the basic concept of design pattern is introduced at first. Through the detailed introduction of the behavior pattern of Interpreter pattern, an object-oriented realization of mathematical expression is designed by using it. At last, the advantages and disadvantages of the Interpreter pattern are summarized. 1.Design Pattern Overview Design pattern is a combination of methodology and a group of abstract patterns used to design reusable object-oriented software in the field of object-oriented software design in recent years. Design pattern is a very powerful tool, which uses the accumulated knowledge and experience of programmers to solve the programming problems that people often encounter, and provides a high-level scheme that is easy to reuse and maintainable. If we use design pattern flexibly, it can save us a lot of time. A design pattern provides a solution to a specific problem. In object-oriented programming, a design pattern has a specific structure. With this structure, the program architecture is more flexible, the code can be reused and maintained more easily, and the program has better adaptability. Design patterns make it more convenient for designers to improve or reuse some past mature designs and architectures. It will be proved by practice that it will make it easier for new system developers to understand their design ideas. As long as designers understand the design pattern, they can absorb the valuable experience in the pattern to a large extent, and have a better understanding of the object-oriented system. At the same time, these patterns can be directly used to guide the critical modeling problems in object-oriented systems. If you have the same problem background, you can apply it directly. More specifically, through the use of design patterns, it can enhance the reuse function of the packaged classes, and effectively handle the change of requirements. Some patterns can reduce the coupling and dependence between classes. More importantly, designers familiar with design patterns can give them new design ideas. 2.Interpreter --Behavior Pattern of Class The interpreter is the behavior pattern of a class. Given any language, the explain mode can define a indication of its grammar and provide an interpreter, which uses the indication to interpret the sentences in the language. The client can use this interpreter to interpret sentences in this language. In the event of a specially appointed mold of problem happens frequently, it may be deserve expressing each instance of the problem as a sentence in a simple language. This allows you to build an interpreter, this interpreter is used to interpret these sentences, so as to solve such problems. How to define a grammar for a simple language can be described by an interpreter pattern,The Interpreter pattern describes how to define a grammar for a simple language, and how to express a sentence in the language, and how to interpret these sentences. Composition of Interpreter pattern: 1) Abstract expression role: declare an abstract interpretation operation, which is implemented by 2020 International Conference on Social and Human Sciences (ICSHS2020) DOI: 10.38007/Proceedings.0000084 ISBN: 978-1-80052-000-4 465 all concrete expression roles (nodes in the abstract syntax tree). Each node of the abstract syntax tree represents a statement, and interpretation methods can be executed on each node. The execution of this interpretation method represents that the statement is interpreted. Because each statement is interpreted on behalf of this statement. Since each statement represents an instance of a common problem, the explanation operation on each node represents an answer to a problem instance. 2) Terminator expression role: specific expression. a) Implement the interpretation operation associated with the terminator in grammar b) And each terminator in a sentence requires an instance of the class to correspond to it 3) Nonterminal expression role: concrete expression. a) Every rule in grammar R::=R1R2...Rn requires a non terminator table band role b) Maintain an instance variable for the abstract expression role for each symbol from R1 to RN c)To implement the interpretation operation, it is generally necessary to call the interpretation operation representing the objects from R1 to RN recursively 4) Context (environment) role: contains some global information outside the interpreter. 5) Customer roles: a) Construct (or be given) an abstract syntax tree that represents a specific sentence in the grammar defined language b) Call interpretation operation Interpreter mode has its applicability: When you need to interpret and execute a language and represent sentences in that language in an abstract syntax tree, you can use the pattern of interpreter . When existing the following conditions this mode effects best : • For complex grammar, the class level of grammar becomes huge and cannot be managed. At this point, a tool like parser generator is a better choice. In this way, the abstract syntax tree can interpret the corresponding expressions without being built, which can save a lot of time and space. • The high efficiency interpreter for a key problem is general not implemented by directly explaining the parsing tree, but at first converting them into another kind of shape. For example, this form of state machine is transformed by regular expression. Even though in this case, the converter can still be realized in the mode of interpreter, which is still useful. 3.Realization of Mathematical Expression Through Interpreter Pattern Each grammar rule of the Interpreter pattern is represented by a class. Regular expression is a standard language for describing string patterns. Instead of constructing a specific algorithm for each pattern, it is better to use a general search algorithm to interpret and execute a regular expression defines be matched. We also use regular expressions to express mathematical expressions. We define grammar and use Interpreter pattern to complete the object-oriented implementation of mathematical expressions. When the mathematical expression is implemented through interpreter mode, the grammar is defined as follows: expression ::= variableexp | literal | addexp | subexp | mulexp | divexp | extract rootexp | squexp | absolute valueexp `(`expression`)` addexp ::= expression `add` expression subexp ::= expression `sub` expression mulexp ::= expression `mul` expression divexp ::= expression `div` expression extract rootexp ::= expression `extract root` expression squexp ::= expression `squ` expression literal ::= `1` | `2` | `3` |... variableexp ::= `A` | `B` | ... | `X` | `Y` | `Z` In the grammar above, the symbol expression is the start symbol and the literal is the terminator that defines a simple word. In this language, the terminators are numbers, i.e. 1, 2, 3, etc. A\",\"PeriodicalId\":202744,\"journal\":{\"name\":\"2020 International Conference on Social and Human Sciences (ICSHS2020)\",\"volume\":\"50 1\",\"pages\":\"0\"},\"PeriodicalIF\":0.0000,\"publicationDate\":\"1900-01-01\",\"publicationTypes\":\"Journal Article\",\"fieldsOfStudy\":null,\"isOpenAccess\":false,\"openAccessPdf\":\"\",\"citationCount\":\"0\",\"resultStr\":null,\"platform\":\"Semanticscholar\",\"paperid\":null,\"PeriodicalName\":\"2020 International Conference on Social and Human Sciences (ICSHS2020)\",\"FirstCategoryId\":\"1085\",\"ListUrlMain\":\"https://doi.org/10.38007/proceedings.0000084\",\"RegionNum\":0,\"RegionCategory\":null,\"ArticlePicture\":[],\"TitleCN\":null,\"AbstractTextCN\":null,\"PMCID\":null,\"EPubDate\":\"\",\"PubModel\":\"\",\"JCR\":\"\",\"JCRName\":\"\",\"Score\":null,\"Total\":0}","platform":"Semanticscholar","paperid":null,"PeriodicalName":"2020 International Conference on Social and Human Sciences (ICSHS2020)","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.38007/proceedings.0000084","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
Object-oriented Realization of Mathematical Expression Based on Interpreter
In this paper, the basic concept of design pattern is introduced at first. Through the detailed introduction of the behavior pattern of Interpreter pattern, an object-oriented realization of mathematical expression is designed by using it. At last, the advantages and disadvantages of the Interpreter pattern are summarized. 1.Design Pattern Overview Design pattern is a combination of methodology and a group of abstract patterns used to design reusable object-oriented software in the field of object-oriented software design in recent years. Design pattern is a very powerful tool, which uses the accumulated knowledge and experience of programmers to solve the programming problems that people often encounter, and provides a high-level scheme that is easy to reuse and maintainable. If we use design pattern flexibly, it can save us a lot of time. A design pattern provides a solution to a specific problem. In object-oriented programming, a design pattern has a specific structure. With this structure, the program architecture is more flexible, the code can be reused and maintained more easily, and the program has better adaptability. Design patterns make it more convenient for designers to improve or reuse some past mature designs and architectures. It will be proved by practice that it will make it easier for new system developers to understand their design ideas. As long as designers understand the design pattern, they can absorb the valuable experience in the pattern to a large extent, and have a better understanding of the object-oriented system. At the same time, these patterns can be directly used to guide the critical modeling problems in object-oriented systems. If you have the same problem background, you can apply it directly. More specifically, through the use of design patterns, it can enhance the reuse function of the packaged classes, and effectively handle the change of requirements. Some patterns can reduce the coupling and dependence between classes. More importantly, designers familiar with design patterns can give them new design ideas. 2.Interpreter --Behavior Pattern of Class The interpreter is the behavior pattern of a class. Given any language, the explain mode can define a indication of its grammar and provide an interpreter, which uses the indication to interpret the sentences in the language. The client can use this interpreter to interpret sentences in this language. In the event of a specially appointed mold of problem happens frequently, it may be deserve expressing each instance of the problem as a sentence in a simple language. This allows you to build an interpreter, this interpreter is used to interpret these sentences, so as to solve such problems. How to define a grammar for a simple language can be described by an interpreter pattern,The Interpreter pattern describes how to define a grammar for a simple language, and how to express a sentence in the language, and how to interpret these sentences. Composition of Interpreter pattern: 1) Abstract expression role: declare an abstract interpretation operation, which is implemented by 2020 International Conference on Social and Human Sciences (ICSHS2020) DOI: 10.38007/Proceedings.0000084 ISBN: 978-1-80052-000-4 465 all concrete expression roles (nodes in the abstract syntax tree). Each node of the abstract syntax tree represents a statement, and interpretation methods can be executed on each node. The execution of this interpretation method represents that the statement is interpreted. Because each statement is interpreted on behalf of this statement. Since each statement represents an instance of a common problem, the explanation operation on each node represents an answer to a problem instance. 2) Terminator expression role: specific expression. a) Implement the interpretation operation associated with the terminator in grammar b) And each terminator in a sentence requires an instance of the class to correspond to it 3) Nonterminal expression role: concrete expression. a) Every rule in grammar R::=R1R2...Rn requires a non terminator table band role b) Maintain an instance variable for the abstract expression role for each symbol from R1 to RN c)To implement the interpretation operation, it is generally necessary to call the interpretation operation representing the objects from R1 to RN recursively 4) Context (environment) role: contains some global information outside the interpreter. 5) Customer roles: a) Construct (or be given) an abstract syntax tree that represents a specific sentence in the grammar defined language b) Call interpretation operation Interpreter mode has its applicability: When you need to interpret and execute a language and represent sentences in that language in an abstract syntax tree, you can use the pattern of interpreter . When existing the following conditions this mode effects best : • For complex grammar, the class level of grammar becomes huge and cannot be managed. At this point, a tool like parser generator is a better choice. In this way, the abstract syntax tree can interpret the corresponding expressions without being built, which can save a lot of time and space. • The high efficiency interpreter for a key problem is general not implemented by directly explaining the parsing tree, but at first converting them into another kind of shape. For example, this form of state machine is transformed by regular expression. Even though in this case, the converter can still be realized in the mode of interpreter, which is still useful. 3.Realization of Mathematical Expression Through Interpreter Pattern Each grammar rule of the Interpreter pattern is represented by a class. Regular expression is a standard language for describing string patterns. Instead of constructing a specific algorithm for each pattern, it is better to use a general search algorithm to interpret and execute a regular expression defines be matched. We also use regular expressions to express mathematical expressions. We define grammar and use Interpreter pattern to complete the object-oriented implementation of mathematical expressions. When the mathematical expression is implemented through interpreter mode, the grammar is defined as follows: expression ::= variableexp | literal | addexp | subexp | mulexp | divexp | extract rootexp | squexp | absolute valueexp `(`expression`)` addexp ::= expression `add` expression subexp ::= expression `sub` expression mulexp ::= expression `mul` expression divexp ::= expression `div` expression extract rootexp ::= expression `extract root` expression squexp ::= expression `squ` expression literal ::= `1` | `2` | `3` |... variableexp ::= `A` | `B` | ... | `X` | `Y` | `Z` In the grammar above, the symbol expression is the start symbol and the literal is the terminator that defines a simple word. In this language, the terminators are numbers, i.e. 1, 2, 3, etc. A