当前位置:网站首页>行为型模式之解释器模式
行为型模式之解释器模式
2020-11-06 21:35:00 【程序猿欧文】
目录
- 定义与特点
- 结构与实现
- 模式的结构
- 模式的实现
- 应用场景
- 扩展:Expression 表达式树和Flee
在软件开发中,会遇到有些问题多次重复出现,而且有一定的相似性和规律性。如果将它们归纳成一种简单的语言,那么这些问题实例将是该语言的一些句子,这样就可以用“编译原理”中的解释器模式来实现了。
虽然使用解释器模式的实例不是很多,但对于满足以上特点,且对运行效率要求不是很高的应用实例,如果用解释器模式来实现,其效果是非常好的,本文将介绍其工作原理与使用方法。
定义与特点
解释器(Interpreter)模式的定义:给分析对象定义一个语言,并定义该语言的文法表示,再设计一个解析器来解释语言中的句子。也就是说,用编译语言的方式来分析应用中的实例。这种模式实现了文法表达式处理的接口,该接口解释一个特定的上下文。
这里提到的文法和句子的概念同编译原理中的描述相同,“文法”指语言的语法规则,而“句子”是语言集中的元素。例如,汉语中的句子有很多,“我是中国人”是其中的一个句子,可以用一棵语法树来直观地描述语言中的句子。
解释器模式是一种类行为型模式,其主要优点如下:
- 扩展性好:由于在解释器模式中使用类来表示语言的文法规则,因此可以通过继承等机制来改变或扩展文法。
- 容易实现:在语法树中的每个表达式节点类都是相似的,所以实现其文法较为容易。
解释器模式的主要缺点如下:
- 执行效率较低:解释器模式中通常使用大量的循环和递归调用,当要解释的句子较复杂时,其运行速度很慢,且代码的调试过程也比较麻烦。
- 会引起类膨胀:解释器模式中的每条规则至少需要定义一个类,当包含的文法规则很多时,类的个数将急剧增加,导致系统难以管理与维护。
- 可应用的场景比较少:在软件开发中,需要定义语言文法的应用实例非常少,所以这种模式很少被使用到。
结构与实现
解释器模式常用于对简单语言的编译或分析实例中,为了掌握好它的结构与实现,必须先了解编译原理中的“文法、句子、语法树”等相关概念。
文法:文法是用于描述语言的语法结构的形式规则。没有规矩不成方圆,任何事情都要有规则,语言也一样,不管它是机器语言还是自然语言,都有它自己的文法规则。例如,中文中的“句子”的文法如下:
〈句子〉::=〈主语〉〈谓语〉〈宾语〉〈主语〉::=〈代词〉|〈名词〉〈谓语〉::=〈动词〉〈宾语〉::=〈代词〉|〈名词〉〈代词〉你|我|他〈名词〉7大学生I筱霞I英语〈动词〉::=是|学习
注:这里的符号“::=”表示“定义为”的意思,用“〈”和“〉”括住的是非终结符,没有括住的是终结符。
句子:句子是语言的基本单位,是语言集中的一个元素,它由终结符构成,能由“文法”推导出。例如,上述文法可以推出“我是大学生”,所以它是句子。
语法树:语法树是句子结构的一种树型表示,它代表了句子的推导结果,它有利于理解句子语法结构的层次。下图所示是“我是大学生”的语法树:
有了以上基础知识,现在来介绍解释器模式的结构就简单了。解释器模式的结构与组合模式相似,不过其包含的组成元素比组合模式多,而且组合模式是对象结构型模式,而解释器模式是类行为型模式。
模式的结构
解释器模式包含以下主要角色:
- 抽象表达式(Abstract Expression)角色:定义解释器的接口,约定.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4553636
边栏推荐
- html+vue.js 實現分頁可相容IE
- Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
- Analysis of serilog source code -- how to use it
- StickEngine-架构12-通信协议
- What is alicloud's experience of sweeping goods for 100 yuan?
- Introduction to the structure of PDF417 bar code system
- 【转发】查看lua中userdata的方法
- What is the purchasing supplier system? Solution of purchasing supplier management platform
- Introduction to quantitative investment and Trading (Python introduction to financial analysis)
- DRF JWT authentication module and self customization
猜你喜欢
How to turn data into assets? Attracting data scientists
C#和C/C++混合编程系列5-内存管理之GC协同
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
What course of artificial intelligence? Will it replace human work?
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
面试官: ShardingSphere 学一下吧
Discussion on the technical scheme of text de duplication (1)
Elasticsearch数据库 | Elasticsearch-7.5.0应用搭建实战
Unity性能优化整理
随机推荐
A brief history of neural networks
A course on word embedding
GUI engine evaluation index
Helping financial technology innovation and development, atfx is at the forefront of the industry
IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
Jetcache buried some of the operation, you can't accept it
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
electron 實現檔案下載管理器
How to understand Python iterators and generators?
How to turn data into assets? Attracting data scientists
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
Flink's datasource Trilogy 2: built in connector
Behind the first lane level navigation in the industry
Outsourcing is really difficult. As an outsourcer, I can't help sighing.
Gather in Beijing! The countdown to openi 2020
MongoDB与SQL常用语法对应表
MeterSphere开发者手册
一篇文章教会你使用Python网络爬虫下载酷狗音乐
Building and visualizing decision tree with Python
快速排序为什么这么快?