如何为小语言建立翻译? [英] How to build an interpreter for a small language?
问题描述
我正在试图弄清楚从这个项目开始的位置。也许有人可以引导我朝着正确的方向前进。
I'm trying to figure out where to start with this project. Perhaps someone can steer me in the right direction.
我得到一种小语言,我必须为其编写翻译。该语言由括号中的表达式组成:
I am given a small language that I must write an interpreter for. The language consists of either an expression in parentheses:
(integer integer operator)
或由表达式组成的算术IF语句:
or an arithmetic IF statement made up of expressions in the form:
IF exp1 exp2 exp3 exp4
如果exp1为负数,则返回exp2,$ b如果exp1为零,则返回$ b exp3,如果exp1为正,则返回exp4。
where exp2 is returned if exp1 is negative, exp3 is returned if exp1 is zero, and exp4 is returned if exp1 is positive.
运算符为+或x(用于加法和乘法)分别)。
The operator is either + or x (for addition and multiplication respectively).
我必须一起实现一个扫描器/解析器,然后是将输出结果的解释器。解释器部分并不困难,但我无法弄清楚如何开始扫描/解析过程。
I have to implement a scanner/parser together, then the interpreter that will output the result. The interpreter part will not be difficult, but I'm having trouble figuring out how to start the scanning/parsing process.
我已经开始使用Java了,并且有一个Scanner对象收集输入并将其存储在字符串中。然后我将String拆分为一个String数组,不使用任何作为分隔符(这样每个单个字符,符号,空格等都存储在它自己的字符串索引中)。这可能不是最好的方法,因为我无法弄清楚从哪里开始。我不能理解的部分是如果没有遵循这种语法,如何返回错误,或者如何检测括号和/或IF等等。
I have started by using Java, and have a Scanner object collect the input and store it in a string. Then I split the String into a String array using nothing as the delimiter (so that each single character, symbol, space, etc. is stored in its own index of a string). This may not be the best way to do this, as I cannot figure out where to go from here. The part I cannot grasp is how to return errors if this syntax isn't followed, or how to detect the parenthesis and/or IF and etc.
这是以下的片段我在上一段描述的代码:
Here's the snippet of code that I described in the last paragraph:
public void run() {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String sLine = sc.nextLine();
String[] scanned = sLine.split("");
输入示例:
(7 2 +)
Output: 9
IF (2 -2 +) (5 2 +) (5 -2 x) (5 2 x)
Output: -10
如果有人有好的方向对我来说,请分享。 :)
If anybody has a good direction for me to take, please share. :)
推荐答案
我认为使用ANTLR,JavaCC,SampleCC或其他解析器生成器工具将会使用大锤来解决问题。如果语法定义中没有递归,只需要几个方法就足够了。下面的代码给出了一个基本的想法(它可能无法编译或工作,我从头开始编写如何开始的插图):
I think using ANTLR, JavaCC, SampleCC or other parser generator tools would be using a sledgehammer to crack a nut. if there is no recursion in grammar definition just a few methods would be sufficient. the following code gives a basic idea (it may not compile or work, I wrote it from scratch as an illustration how to start):
public int parse(String input) {
Scanner scanner = new Scanner(input);
return consumeLine(scanner);
}
public int consumeLine(Scanner scanner) {
if( scanner.hasNext("(") ) {
return consumeExpression(scanner);
} else if( scanner.hasNext("IF") ) {
return consumeIf(scanner);
}
}
public int consumeExpression(Scanner scanner) {
scanner.next("(");
int a = scanner.nextInt();
int b = scanner.nextInt();
String op = scanner.next("[+-/*]");
scanner.next(")");
if( "+".equals(op) ) {
return a + b;
} else if( "-".equals(op) ) {
return a - b;
} ...
throw new RuntimeException("parsing error");
}
public int consumeIf(Scanner scanner) {
scanner.next("IF");
int exp1 = consumeExpression(scanner);
int exp2 = consumeExpression(scanner);
int exp3 = consumeExpression(scanner);
int exp4 = consumeExpression(scanner);
if( exp1 < 0 ) {
return exp2;
} else if( exp1 == 0 ) {
return exp3;
} ...
throw new RuntimeException("should not be here (TM)");
}
这篇关于如何为小语言建立翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!