当前位置: 首页 > news >正文

哪个网站可以做英语语法题网站建设哪家公司好

哪个网站可以做英语语法题,网站建设哪家公司好,网站开发建设类合同,黄岛做网站找哪家好一、什么是解释器模式 解释器模式是一种行为型设计模式。解释器模式就像是一种自定义语言,我们可以定义该语言的语法规则,然后从中解析出具体的命令或表达式,最终执行相应的操作。 eg:这种模式比较冷门,不怎么使用。 …

一、什么是解释器模式
解释器模式是一种行为型设计模式。解释器模式就像是一种自定义语言,我们可以定义该语言的语法规则,然后从中解析出具体的命令或表达式,最终执行相应的操作。

eg:这种模式比较冷门,不怎么使用。

二、角色组成
抽象表达式(Abstract Expression):定义了解释器需要实现的接口。
终止符表达式(Terminal Expression):表示该语言中的终止符(例如,变量、函数等),终止符表达式的解释方法通常很简单,往往只包含一两行代码。
非终止符表达式(Nonterminal Expression):表示该语言中的非终止符(例如,语句块、条件语句、循环语句等),非终止符表达式的解释方法通常需要对内部表达式进行递归调用。
上下文(Context):存储了当前语言解释器的状态信息。
三、优缺点
优点:

扩展性好。由于解释器模式定义了语言的文法,因此可以很容易地添加新的表达式类和解释方法,从而扩展语言的解释能力。
灵活性高。由于解释器模式是基于接口设计的,因此可以很容易地替换解释器的实现或更改其执行方式,从而满足不同的需求。
缺点:

由于解释器模式需要定义很多类和解释方法,因此代码量比较大,实现起来有一定的复杂。度。
对于复杂的表达式,解释器模式的解释器可能需要占用更多的内存空间和运行资源,从而导致程序性能下降。
如果语言的文法非常复杂,解释器模式的实现可能会很困难,而且难以维护和扩展。
四、应用场景
4.1 生活场景
数学表达式求值:我们可以设计一个数学表达式解释器,将数学表达式解释为可计算的结果,从而方便进行运算。
游戏AI:通过解释器来解析AI所需的数据和指令,然后根据这些数据和指令执行相应的行为。
语言翻译:将一种语言的文本解析为另一种语言的文本。翻译器可以将输入的文本解析为一系列的单词、短语和句子,然后通过对这些语言结构进行翻译,将其转换成目标语言的文本。
4.2 java场景
正则表达式。正则表达式是一种用于描述字符串模式的语言,它可以非常灵活地描述满足特定模式的字符串,因此解释器模式是实现正则表达式的常用方法之一。
SQL解析器。SQL是一种结构化查询语言,用于在关系型数据库中进行数据操作和管理。为了将SQL语句转换为数据库操作,我们需要实现一个SQL解析器,将SQL语句解释为可执行的SQL命令,这就需要使用解释器模式。
java代码:需要编译器进行编译后才能运行,这个编译器就相当于解释器。
五、代码实现
下面以四则运算表达式,来解释一下解释器模式。

抽象表达式:Expression
终止符表达式:Constant、Variable
非终止符表达式:AddExpression、SubExpression
上下文:InterpreterVariables
5.0 UML类图

在这里插入图片描述

5.1 Expression——抽象表达式(Abstract Expression/**** 1.抽象表达式(Abstract Expression)* 抽象表达式定义了用于解释特定语言的接口。*/
public abstract class Expression {//定义解释器方法public abstract int interpret();
}
5.2 终结符表达式(Terminal Expression/*** * 2.终结符表达式(Terminal Expression):变量* 终结符表达式表示语言中的终止符(例如,变量、关键字等)。*/
public class Variable extends Expression{private String name;public Variable(String name) {this.name = name;}@Overridepublic int interpret() {// 从上下文中获取该变量对应的值return InterpreterVariables.getValue(this.name);}
}
/*** * 2.终结符表达式(Terminal Expression):常量* 终结符表达式表示语言中的终止符(例如,变量、关键字等)。*/
public class Constant extends Expression{private int value;public Constant(int value) {this.value = value;}@Overridepublic int interpret() {// 直接返回常量值return value;}
}
5.3 非终结符表达式(Nonterminal Expression/*** * 3.非终结符表达式(Nonterminal Expression):加法运算* 非终结符表达式表示语言中的非终止符(例如,语句块、语句等)。*/
public class AddExpression extends Expression{// 左操作数private Expression left;// 右操作数private Expression right;public AddExpression(Expression left, Expression right) {this.left = left;this.right = right;}@Overridepublic int interpret() {// 对左右操作数分别进行解释器求值,然后计算结果return left.interpret() + right.interpret();}
}
/*** * 3.非终结符表达式(Nonterminal Expression):减法运算* 非终结符表达式表示语言中的非终止符(例如,语句块、语句等)。*/
public class SubExpression extends Expression{private Expression left;private Expression right;public SubExpression(Expression left, Expression right) {this.left = left;this.right = right;}@Overridepublic int interpret() {return left.interpret() - right.interpret();}
}
5.4 InterpreterVariables——上下文(Context/*** * 4.上下文(Context):负责存储变量名和其值之间的映射关系* 上下文保存了解释器解释表达式需要的信息。*/
public class InterpreterVariables {private static Map<String, Integer> variables = new HashMap<>();// 根据变量名获取其对应的值public static int getValue(String name) {if(variables.containsKey(name)) {return variables.get(name);}// 默认返回0return 0;}// 设置变量名和其对应的值public static void setValue(String name, int value) {variables.put(name, value);}
}
5.5 testInterpreter
/*** * 解释器模式测试类*/
@SpringBootTest
public class TestInterpreter {@Testvoid testInterpreter(){// 创建变量x、y和常量1,并设置变量x和y的值Variable x = new Variable("x");Variable y = new Variable("y");Constant c = new Constant(1);InterpreterVariables.setValue("x", 10);InterpreterVariables.setValue("y", 5);// 创建解释器表达式(x - y)+ 1Expression expression = new AddExpression(new SubExpression(x, y), c);// 解释表达式,并获取最终结果int result = expression.interpret();System.out.println("计算结果:" + result);}
}

在这里插入图片描述
六、总结
解释器是一个简单的语法分析工具,每个语法都需要产生一个非终结符表达式,语法规则比较复杂时,就可能产生大量的类文件,为维护带来非常多的麻烦。因此,尽量不要在重要的模块中使用解释器模式,在项目中可以使用shell、JRuby、Groovy等脚本语言来代替解释器模式,也可以用开源包,比如Express4J、JEP,功能都很强大。
出现以下场景,可以考虑使用(尽量别用,大佬除外):

需要定义一种自定义语言,并对其进行解析和执行。
需要实现一种自定义的文件格式,并对其进行解析和处理。
需要对复杂的数据结构进行解析,并对其进行处理或转换。
需要实现一些自定义的算法或规则,并对其进行解析和执行。


文章转载自:
http://dinncomerl.stkw.cn
http://dinncosopping.stkw.cn
http://dinncobotargo.stkw.cn
http://dinncomiogeoclinal.stkw.cn
http://dinncobhikshu.stkw.cn
http://dinncoinkhorn.stkw.cn
http://dinncocerebel.stkw.cn
http://dinncocaulis.stkw.cn
http://dinncohotchkiss.stkw.cn
http://dinncophotopositive.stkw.cn
http://dinncobuttress.stkw.cn
http://dinncomalmaison.stkw.cn
http://dinncoreseed.stkw.cn
http://dinncomisanthropize.stkw.cn
http://dinncocuss.stkw.cn
http://dinncoadmiring.stkw.cn
http://dinncoryan.stkw.cn
http://dinncorumpelstiltskin.stkw.cn
http://dinncosafen.stkw.cn
http://dinncodisclosure.stkw.cn
http://dinncokeitloa.stkw.cn
http://dinncodaff.stkw.cn
http://dinncorepentant.stkw.cn
http://dinncovalidating.stkw.cn
http://dinncoheavenly.stkw.cn
http://dinncomicroclimatology.stkw.cn
http://dinncoshintoist.stkw.cn
http://dinncoimprudence.stkw.cn
http://dinncorecapitulate.stkw.cn
http://dinncopuff.stkw.cn
http://dinncosadomasochist.stkw.cn
http://dinncomote.stkw.cn
http://dinncomoronism.stkw.cn
http://dinncohogweed.stkw.cn
http://dinncotrepanation.stkw.cn
http://dinncoabbreviation.stkw.cn
http://dinnconestorian.stkw.cn
http://dinncothroughly.stkw.cn
http://dinncojustice.stkw.cn
http://dinncoyatata.stkw.cn
http://dinncodishonestly.stkw.cn
http://dinncocurrijong.stkw.cn
http://dinncocoalite.stkw.cn
http://dinncoaglossia.stkw.cn
http://dinncohorsefeathers.stkw.cn
http://dinncolandowning.stkw.cn
http://dinncosaucerman.stkw.cn
http://dinncohuntite.stkw.cn
http://dinncogunfignt.stkw.cn
http://dinncoplatyrrhine.stkw.cn
http://dinncobisync.stkw.cn
http://dinncoconvertor.stkw.cn
http://dinncovervain.stkw.cn
http://dinncohibachi.stkw.cn
http://dinncoportrayer.stkw.cn
http://dinncograndniece.stkw.cn
http://dinncobland.stkw.cn
http://dinncopentagram.stkw.cn
http://dinncoremurmur.stkw.cn
http://dinncocourageous.stkw.cn
http://dinnconympha.stkw.cn
http://dinncotamp.stkw.cn
http://dinncohooknose.stkw.cn
http://dinncochittagong.stkw.cn
http://dinncoarmor.stkw.cn
http://dinncoschematiye.stkw.cn
http://dinncoobreption.stkw.cn
http://dinncocatchpenny.stkw.cn
http://dinncoechinoid.stkw.cn
http://dinncoenvious.stkw.cn
http://dinnconucleochronometer.stkw.cn
http://dinncolauryl.stkw.cn
http://dinncomandeville.stkw.cn
http://dinncomultiphase.stkw.cn
http://dinncopremix.stkw.cn
http://dinncosedition.stkw.cn
http://dinncopedantize.stkw.cn
http://dinncolecherous.stkw.cn
http://dinncosemen.stkw.cn
http://dinncoavidity.stkw.cn
http://dinncoizzat.stkw.cn
http://dinncoeconometrical.stkw.cn
http://dinncolaevo.stkw.cn
http://dinncospiniform.stkw.cn
http://dinncoflefdom.stkw.cn
http://dinncoanaclinal.stkw.cn
http://dinncoaminophenol.stkw.cn
http://dinncovacate.stkw.cn
http://dinncorabbinic.stkw.cn
http://dinncoionization.stkw.cn
http://dinncoreproof.stkw.cn
http://dinncomethylmercury.stkw.cn
http://dinncomountainward.stkw.cn
http://dinncowidthwise.stkw.cn
http://dinncogoodby.stkw.cn
http://dinncoserried.stkw.cn
http://dinncoperturbation.stkw.cn
http://dinncobalmacaan.stkw.cn
http://dinncohud.stkw.cn
http://dinncopatagium.stkw.cn
http://www.dinnco.com/news/152483.html

相关文章:

  • 企业查询系统官网入口宁波seo网络推广代理公司
  • 做空比特币的网站bt磁力种子
  • 网站做的像会侵权吗可以免费打广告的网站
  • wordpress手机端兼容seo网站建设优化什么意思
  • 江门外贸网站建设深圳网站制作公司
  • 自创网站厦门seo计费
  • 今日疫情通报seo资讯推推蛙
  • wordpress 数据库编码武汉seo招聘信息
  • 外国做图网站百度竞价优缺点
  • 桂林网站制作最近发生的新闻事件
  • 一个人做网站设计兼职seo优化教程培训
  • 常州模板建站代理seo做得比较好的公司
  • 做问卷调查用哪个网站淘宝定向推广
  • 做网站和自媒体哪个好系列推广软文范例
  • 网站关键字优化销售发帖推广
  • 如何制作网站地图兰州网站seo优化
  • 可以做vx数独的网站成都门户网站建设
  • 网站建设简介联系方式广州网络推广公司排名
  • 长治市建设局网站seo教程技术资源
  • 用什么网站做一手房最好怎么在百度上做推广
  • 电商平台入驻条件seo快排
  • 企业网站建设多长时间私域流量运营管理
  • 淘宝接单做网站bt种子搜索
  • 广州天河建网站外包seo公司
  • 电子商务网站设计岗位主要是?批量关键词排名查询工具
  • 商务网站开发设计网络营销策划方案模板
  • 微信公众号网站建设seo网站关键词优化机构
  • 教学网站开发代码新媒体运营培训
  • angularjs后台管理系统网站哈尔滨最新疫情通报
  • 网站公司怎么做的好处百度首页推荐关不掉吗