From fd0dc7399ae5e6b1f90a23bb5adb617f7a4a5ae8 Mon Sep 17 00:00:00 2001 From: "1923365156@qq.com" <$$@@zhaofan666> Date: Sun, 10 Sep 2023 09:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E9=87=8A=E5=99=A8=E6=A8=A1=E5=BC=8F&&?= =?UTF-8?q?=20=E4=BC=98=E7=BC=BA=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 2 + .idea/encodings.xml | 2 + .idea/misc.xml | 1 + .idea/uiDesigner.xml | 124 ++++++++++++++++++ README.md | 29 ++++ byv-interpreter-patttern-21/.gitignore | 38 ++++++ byv-interpreter-patttern-21/pom.xml | 58 ++++++++ .../boyunv/interpreter/example01/Client.java | 18 +++ .../example01/ExpressionInterpreter.java | 66 ++++++++++ .../boyunv/interpreter/example02/Client.java | 18 +++ .../interpreter/example02/DivExpression.java | 29 ++++ .../interpreter/example02/Expression.java | 16 +++ .../example02/ExpressionInterpreter.java | 55 ++++++++ .../interpreter/example02/MulExpression.java | 29 ++++ .../example02/NumberExpression.java | 33 +++++ .../interpreter/example02/PluExpression.java | 29 ++++ .../interpreter/example02/SubExpression.java | 29 ++++ .../src/main/resources/config.xml | 4 + 18 files changed, 580 insertions(+) create mode 100644 .idea/uiDesigner.xml create mode 100644 byv-interpreter-patttern-21/.gitignore create mode 100644 byv-interpreter-patttern-21/pom.xml create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/Client.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/ExpressionInterpreter.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/Client.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/DivExpression.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/Expression.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/ExpressionInterpreter.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/MulExpression.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/NumberExpression.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/PluExpression.java create mode 100644 byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example02/SubExpression.java create mode 100644 byv-interpreter-patttern-21/src/main/resources/config.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index f28b7ce..0c6a6d9 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -22,11 +22,13 @@ + + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b54dff9..9265d3b 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -15,6 +15,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index d093d14..e471b02 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -21,6 +21,7 @@ diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 70b05fc..a8f22d0 100644 --- a/README.md +++ b/README.md @@ -412,7 +412,36 @@ value ::= integer * 非终结符表达式(Nonterminal Expression)角色:也是抽象表达式的子类,用来实现文法中与非终结符相关的操作,文法中的每条规则都对应于一个非终结符表达式。上例中的 plus , minus 都是非终结符表达式 * 环境(Context)角色:通常包含各个解释器需要的数据或是公共的功能,一般用来传递被所有解释器共享的数据,后面的解释器可以从这里获取这些值。 * 客户端(Client):主要任务是将需要分析的句子或表达式转换成使用解释器对象描述的抽象语法树,然后调用解释器的解释方法,当然也可以通过环境角色间接访问解释器的解释方法。 +## 总结 优缺点呢 +**1) 解释器优点** +- 易于改变和扩展文法 + + 因为在解释器模式中使用类来表示语言的文法规则的,因此就可以通过继承等机制改变或者扩展文法.每一个文法规则都可以表示为一个类,因此我们可以快速的实现一个迷你的语言 + +- 实现文法比较容易 + + 在抽象语法树中每一个表达式节点类的实现方式都是相似的,这些类的代码编写都不会特别复杂 + +- 增加新的解释表达式比较方便 + + 如果用户需要增加新的解释表达式,只需要对应增加一个新的表达式类就可以了.原有的表达式类不需要修改,符合开闭原则 + +**2) 解释器缺点** + +- 对于复杂文法难以维护 + + 在解释器中一条规则至少要定义一个类,因此一个语言中如果有太多的文法规则,就会使类的个数急剧增加,当值系统的维护难以管理. + +- 执行效率低 + + 在解释器模式中大量的使用了循环和递归调用,所有复杂的句子执行起来,整个过程也是非常的繁琐 + +**3) 使用场景** + +- 当语言的文法比较简单,并且执行效率不是关键问题. +- 当问题重复出现,且可以用一种简单的语言来进行表达 +- 当一个语言需要解释执行,并且语言中的句子可以表示为一个抽象的语法树的时候 1. 使用 Readme.md 来支持不同的语言, Readme\_en.md: 支持英文, Readme\_zh.md: 支持中文 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) diff --git a/byv-interpreter-patttern-21/.gitignore b/byv-interpreter-patttern-21/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/byv-interpreter-patttern-21/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/byv-interpreter-patttern-21/pom.xml b/byv-interpreter-patttern-21/pom.xml new file mode 100644 index 0000000..250c030 --- /dev/null +++ b/byv-interpreter-patttern-21/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.example + byv-interpreter-patttern-21 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.projectlombok + lombok + 1.18.28 + compile + + + org.dom4j + dom4j + 2.1.3 + + + jaxen + jaxen + 1.1.6 + + + log4j + log4j + 1.2.12 + + + org.slf4j + slf4j-api + 1.7.36 + + + com.alibaba + fastjson + 1.2.62 + + + junit + junit + 4.12 + compile + + + + + \ No newline at end of file diff --git a/byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/Client.java b/byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/Client.java new file mode 100644 index 0000000..b392009 --- /dev/null +++ b/byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/Client.java @@ -0,0 +1,18 @@ +package com.boyunv.interpreter.example01; +/* + *@description + * + *@author boyunv + *@create 2023/9/10 8:32 + *@version 1.0 + */ + +public class Client { + + public static void main(String[] args) { + ExpressionInterpreter interpreter = new ExpressionInterpreter(); + long result = interpreter.interpreter("9 5 7 3 - + *"); + System.out.println("9 5 7 3 - + * 结果是:"+result); + + } +} diff --git a/byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/ExpressionInterpreter.java b/byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/ExpressionInterpreter.java new file mode 100644 index 0000000..ed3bd8d --- /dev/null +++ b/byv-interpreter-patttern-21/src/main/java/com/boyunv/interpreter/example01/ExpressionInterpreter.java @@ -0,0 +1,66 @@ +package com.boyunv.interpreter.example01; +/* + *@description + * 表达式解释器 + *@author boyunv + *@create 2023/9/10 8:12 + *@version 1.0 + */ + +import java.util.Deque; +import java.util.LinkedList; + +public class ExpressionInterpreter { + + //双向队列,可以从队列两端增加或者删除元素 + private Deque numbers=new LinkedList(); + + + //接受表达式进行解析 + public long interpreter(String expression){ + //9 5 7 3 - + * + String[] element = expression.split(" "); + int length=element.length; + //获取表达式中数字 + for(int i=0;i< (length+1)/2;++i){ + //像集合的尾部去添加元素 + numbers.addLast(Long.parseLong(element[i])); + + } + + //获取表达式中符号,去进行计算 + for (int i = (length+1)/2; i numbers=new LinkedList<>(); + public long interpret(String expression){ + String[] elements = expression.split(" "); + int length = elements.length; + for (int i = 0; i < (length+1)/2; ++i) { + numbers.addLast(new NumberExpression(elements[i])); + + } + + for (int i = (length+1)/2; i + + com.boyunv.strategy.example03.MT1101HandlerStrategy + \ No newline at end of file -- Gitee