基于解释器的数学表达式的面向对象实现

Jing Shi
{"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}
引用次数: 0

摘要

在这一点上,像解析器生成器这样的工具是更好的选择。这样,抽象语法树无需构建就可以解释相应的表达式,可以节省大量的时间和空间。•对于关键问题的高效解释器通常不是通过直接解释解析树来实现的,而是首先将它们转换为另一种形状。例如,这种形式的状态机通过正则表达式进行转换。即使在这种情况下,转换器仍然可以在解释器模式下实现,这仍然是有用的。3.通过解释器模式实现数学表达式解释器模式的每个语法规则用一个类来表示。正则表达式是描述字符串模式的标准语言。与其为每个模式构建特定的算法,不如使用通用的搜索算法来解释和执行匹配的正则表达式定义。我们也用正则表达式来表示数学表达式。我们定义语法并使用解释器模式来完成数学表达式的面向对象实现。当数学表达式通过解释器模式实现时,语法定义如下:Expression::= variableexp | literal | addexp | subexp | mulexp | divexp | extract rootexp | squexp |绝对值eexp ' (' 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 ' extract root ' Expression squexp::= Expression ' squ ' Expression literal::= ' 1 ' | ' 2 ' | ' 3 ' |…variableexp::= ' A ' | ' B ' |…在上面的语法中,符号表达式是开始符号,而文字是定义一个简单单词的结束符。在这种语言中,终止符是数字,即1、2、3等。一个
本文章由计算机程序翻译,如有差异,请以英文原文为准。
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
求助全文
通过发布文献求助,成功后即可免费获取论文全文。 去求助
来源期刊
自引率
0.00%
发文量
0
×
引用
GB/T 7714-2015
复制
MLA
复制
APA
复制
导出至
BibTeX EndNote RefMan NoteFirst NoteExpress
×
提示
您的信息不完整,为了账户安全,请先补充。
现在去补充
×
提示
您因"违规操作"
具体请查看互助需知
我知道了
×
提示
确定
请完成安全验证×
copy
已复制链接
快去分享给好友吧!
我知道了
右上角分享
点击右上角分享
0
联系我们:info@booksci.cn Book学术提供免费学术资源搜索服务,方便国内外学者检索中英文文献。致力于提供最便捷和优质的服务体验。 Copyright © 2023 布克学术 All rights reserved.
京ICP备2023020795号-1
ghs 京公网安备 11010802042870号
Book学术文献互助
Book学术文献互助群
群 号:604180095
Book学术官方微信