一,Interpreter使用面不是很广,描述了一个语言解释器是怎么构成的,在实际应用中,我们可能很少去构造一个语言的解释器.
没在平常使用中发现例子,故以java与模式一书中的例子为例。。。
这个例子是针对 与,或,非提供的一套解析器
提供一个实现的环境
import java.util.HashMap;
import java.util.Map;
/**
*提供一个boolean环境,用于作判断
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:39:24 PM
*/
public class Context {
private Map map=new HashMap();
public void assign(Variable var,boolean value){
map.put(var, new Boolean(value));
}
public boolean lookup(Variable var)throws IllegalAccessException{
Boolean value=(Boolean)map.get(var);
if(value==null){
throw new IllegalAccessException();
}
return value.booleanValue();
}
}
解析器的接口
/**
*进行and or !操作的抽象
*
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:36:54 PM
*/
public interface Expression {
//用于判断操作
public boolean interpret(Context ctx);
public boolean equals(Object o);
public int hashCode();
public String toString();
}
//And的操作
/**
* i与操作
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:47:12 PM
*/
public class And implements Expression {
private Expression left,right;
public And(Expression left,Expression right){
this.left=left;
this.right=right;
}
public boolean interpret(Context ctx) {
// TODO Auto-generated method stub
return left.interpret(ctx)&&right.interpret(ctx);
}
public boolean equals(Object o){
if(o!=null&&o instanceof And){
return this.left.equals(((And)o).left)&&this.right.equals(((And)o).right);
}
return false;
}
public int hashCode(){
return(this.toString()).hashCode();
}
public String toString(){
return "("+left.toString()+"AND"+right.toString()+")";
}
}
//or操作
/**
* 或操作
*
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:53:04 PM
*/
public class Or implements Expression {
private Expression left, right;
public Or(Expression left, Expression right) {
this.left = left;
this.right = right;
}
public boolean interpret(Context ctx) {
// TODO Auto-generated method stub
return left.interpret(ctx) || right.interpret(ctx);
}
public boolean equals(Object o) {
if (o != null && o instanceof Or) {
return this.left.equals(((Or) o).left)
|| this.right.equals(((Or) o).right);
}
return false;
}
public int hashCode() {
return (this.toString()).hashCode();
}
public String toString() {
return "(" + left.toString() + "OR" + right.toString() + ")";
}
}
//Not操作
/**
* 非操作
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:40:35 PM
*/
public class Constant implements Expression{
private boolean value;
public Constant(boolean value){
this.value=value;
}
public boolean interpret(Context ctx) {
return value;
}
public boolean equals(Object o){
if(o!=null&&o instanceof Constant){
return this.value==((Constant)o).value;
}
return false;
}
public int hashCode(){
return(this.toString()).hashCode();
}
public String toString(){
return new Boolean(value).toString();
}
}
//-----------------------为其提供操作的类型Variable Constant
Variable
/**
* 使用相应的名称到context中进行查找
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:44:36 PM
*/
public class Variable implements Expression {
private String name;
public Variable(String name){
this.name=name;
}
public boolean interpret(Context ctx) {
try {
return ctx.lookup(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
public boolean equals(Object o){
if(o!=null&&o instanceof Constant){
return this.name.equals(((Variable)o).name);
}
return false;
}
public int hashCode(){
return(this.toString()).hashCode();
}
public String toString(){
return name;
}
}
//Constant
/**
* 返回固定类型
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 9:40:35 PM
*/
public class Constant implements Expression{
private boolean value;
public Constant(boolean value){
this.value=value;
}
public boolean interpret(Context ctx) {
return value;
}
public boolean equals(Object o){
if(o!=null&&o instanceof Constant){
return this.value==((Constant)o).value;
}
return false;
}
public int hashCode(){
return(this.toString()).hashCode();
}
public String toString(){
return new Boolean(value).toString();
}
}
//------------------------------------test
可在此对and or not进行任意的操作
/**
*
* @author wu_quanyin(09817)
* @version 1.0
* @date Jul 9, 2010 10:01:31 PM
*/
public class Client {
private static Context ctx;
private static Expression exp;
public static void main(String[] args) {
ctx=new Context();
Variable x=new Variable("x");
Variable y=new Variable("y");
Constant c=new Constant(true);
ctx.assign(x, false);
ctx.assign(y, true);
exp=new Or(new And(c,x),new And(y,new Not(x)));
System.out.println(exp.interpret(ctx));
}
}
/**
--false
*/
分享到:
相关推荐
C2j编译器 将C语言转换为Java字节码...启动模式,默认为解释器 -d 真的 错误的 是否打开调试信息,默认为false(建议不开启) -F 指定运行文件的路径 一个例子 源文件 void swap ( int arr[ 10 ], int i, int j) {
第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) 附录A常见面向对象设计原则 附录...
第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) 附录A常见面向对象设计原则 附录...
第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式(Chain of Responsibility) 第24章 桥接模式(Bridge) 第25章 访问者模式(Visitor) 附录A常见面向对象设计原则 附录...
5.3InterpreterPattern(解释器模式) 190 5.3.1定义 190 5.3.2现实示例——音乐符号 192 5.3.3C#实例——中国金钱大写转换 192 5.3.4Java实例——自定义程序解释器 197 5.3.5优势和缺陷 204 5.3.6应用情景 ...
5.3InterpreterPattern(解释器模式) 190 5.3.1定义 190 5.3.2现实示例——音乐符号 192 5.3.3C#实例——中国金钱大写转换 192 5.3.4Java实例——自定义程序解释器 197 5.3.5优势和缺陷 204 5.3.6应用情景 ...
com.javapatterns.interpreter 解释器模式 com.javapatterns.isp 接口隔离原则 com.javapatterns.iterator 迭代子模式 com.javapatterns.javaio 专题:设计模式在Java I/O中的应用 com.javapatterns.keygen 专题...
数学解释器就像标题所说的那样,这是一个简单的数学解释器。 现在,您可以使用: 小数和整数职能: 棕褐色罪cos sqrt 一元运算符: 加号(+) 减 (-) 二进制运算符: 乘以(*) 除(/) 减 (-) 加号(+) 战俘...
设计模式之 Interpreter(解释器) 主要用来对语言的分析,应用机会不多. 设计模式之 Visitor(访问者) 访问者在进行访问时,完成一系列实质性操作,而且还可以扩展. 设计模式引言 设计面向对象软件比较困难,而设计...
纯Java语言的BrainFuck解释器该函数仅要求BrainFuck中的代码进行转换,当有','时将执行该函数,并且将对BrainFuck类型的每个值(+,-,<...)执行该函数。 例子: > bf( '++++++[>+++++++++++<-]>-.', () => ...
giao.js 迷你Javascript解释器。例子文章有关的执照