给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
解释器模式的结构中包括四个角色。
+ 抽象表达式(AbstractExpression):该角色为一个接口,负责定义抽象的解释操作。
+ 终结符表达式(TerminalExpression):实现AbstractExpression接口的类。该类将接口中的解释器操作实现为与文法中的终结符相关联的操作,即文法中的每个终结符需要一个TerminalExpression类。
+ 非终结符表达式(NonterminalExpression):实现AbstractExpression接口的类。文法中的每一条规则R::=R1R2…Rn都需要一个NonterminalExpression类。NonterminalExpression类为文法中的非终结符号实现解释操作,该解释操作通常使用递归用表示Ri到Rn的那些对象的解释操作。
+ 上下文(Context):包含解释器之外的一些全局信息。
package Interpreter;public interface Node {public void parse(Context text);public void execute();
}
package Interpreter;import java.util.StringTokenizer;public class Context {StringTokenizer tokenizer;String token;public Context(String text) {setContext(text);}public void setContext(String text) {tokenizer = new StringTokenizer(text);}String nextToken() {if (tokenizer.hasMoreTokens()) {token = tokenizer.nextToken();} else token = "";return token;}
}
package Interpreter;public class SubjectPronounOrNounNode implements Node {String[] word = {"You", "He", "Teacher", "Student"};String token;boolean boo;@Overridepublic void parse(Context context) {token = context.nextToken();int i = 0;for (; i < word.length; i++) {if (token.equalsIgnoreCase(word[i])) {boo = true;break;}}if (i == word.length) {boo = false;}}@Overridepublic void execute() {if (boo) {if (token.equalsIgnoreCase(word[0]))System.out.print("你");if (token.equalsIgnoreCase(word[1]))System.out.print("他");if (token.equalsIgnoreCase(word[2]))System.out.print("老师");if (token.equalsIgnoreCase(word[3]))System.out.print("学生");} else {System.out.println(token + "(不是该语言中的语句)");}}
}
package Interpreter;public class ObjectPronounOrNounNode implements Node {String[] word = {"Me", "Him", "Tiger", "Apple"};String token;boolean boo;@Overridepublic void parse(Context context) {token = context.nextToken();int i = 0;for (; i < word.length; i++) {if (token.equalsIgnoreCase(word[i])) {boo = true;break;}}if (i == word.length) {boo = false;}}@Overridepublic void execute() {if (boo) {if (token.equalsIgnoreCase(word[0]))System.out.print("我");if (token.equalsIgnoreCase(word[1]))System.out.print("他");if (token.equalsIgnoreCase(word[2]))System.out.print("老虎");if (token.equalsIgnoreCase(word[3]))System.out.print("苹果");} else {System.out.println(token + "(不是该语言中的语句)");}}
}
package Interpreter;public class VerbNode implements Node {String[] word = {"Drink", "Eat", "Look", "beat"};String token;boolean boo;@Overridepublic void parse(Context context) {token = context.nextToken();int i = 0;for (; i < word.length; i++) {if (token.equalsIgnoreCase(word[i])) {boo = true;break;}}if (i == word.length) {boo = false;}}@Overridepublic void execute() {if (boo) {if (token.equalsIgnoreCase(word[0]))System.out.print("喝");if (token.equalsIgnoreCase(word[1]))System.out.print("吃");if (token.equalsIgnoreCase(word[2]))System.out.print("看");if (token.equalsIgnoreCase(word[3]))System.out.print("打");} else {System.out.println(token + "(不是该语言中的语句)");}}
}
package Interpreter;public class SubjectNode implements Node {Node node;@Overridepublic void parse(Context text) {node = new SubjectPronounOrNounNode();node.parse(text);}@Overridepublic void execute() {node.execute();}
}
papackage Interpreter;public class PredicateNode implements Node {Node verbNode, objectNode;@Overridepublic void parse(Context text) {verbNode = new VerbNode();objectNode = new ObjectPronounOrNounNode();verbNode.parse(text);objectNode.parse(text);}@Overridepublic void execute() {verbNode.execute();objectNode.execute();}
}
package Interpreter;public class SentenceNode implements Node {Node subjectNode, predicateNode;@Overridepublic void parse(Context text) {subjectNode = new SubjectNode();predicateNode = new PredicateNode();subjectNode.parse(text);predicateNode.parse(text);}@Overridepublic void execute() {subjectNode.execute();predicateNode.execute();}
}
package Interpreter;public class Application {public static void main(String[] args) {String text = "Teacher beat tiger";Context context = new Context(text);Node node = new SentenceNode();node.parse(context);node.execute();System.out.println();text = "You eat apple";context.setContext(text);node.parse(context);node.execute();}
}
老师打老虎
你吃苹果
注意:如果文法过于复杂,那么过多的文法规则使我们很艰难维护所给出的类。
MyGitHub
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态