在java中编写解释器时切换或if语句 [英] Switch or if statements in writing an interpreter in java

查看:127
本文介绍了在java中编写解释器时切换或if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前的分配需要我编写一个程序来读取一个文件,其中包含一个非常小的基本编程语言的指令(行为有点像FORTRAN)并执行这些指令。基本上它是我猜的语言的简单解释器。它是完全线性的,语句全部按顺序定义,只有String和整数变量。如果它们存在于源文件中,我需要找到并定义8个关键字和4个算术运算符,并且每行必须以其中一个保留字开始。
这个语言的程序可能会是这样的:

Current assignment needs me to write a program to read a file with instructions in a very tiny and basic programming language (behaves a little like FORTRAN) and execute those instructions. Basically it's a simple interpreter for the language I guess. It's completely linear, with statements all being defined in sequence and it only has String and integer variables. There are 8 keywords and 4 arithmetic operators I would need to find and define if they exist within the source file, and each line must start off with one of the reserved words. A program in this language might look something like this:

#COMMENTS
LET.... (declares variables with values)
INTEGER myINT
STRING myString
CALCULATE...
PRINT
PRINTLN
END

我可以使用switch块而不是if-loops来查找然后执行所有这些吗?我担心的是交换机不能与Java 6中的Strings一起使用,这是我应该使用的,但是我没有看到如何轻松地分配各种int值,因此switch块可以工作。提前感谢任何建议和意见!

Can I use a switch block instead of if-loops to find and then execute all these? My concern is that switches don't work with Strings in Java 6, which is what I'm supposed to be using, but I don't see how to easily assign various int values so the switch block would work. Thanks in advance for any suggestions and advice!

推荐答案

如果您的语言如此简单,以至于每个语句都以自己的行开头,并且只用一个单词标识,然后(正如格雷在另一个评论中指出的那样)你可以拆分每一行中的单词,然后将第一个单词与地图进行比较。但是,我建议,不要将单词映射到整数然后做一个大开关,而是将它们映射到对象中,就像这样(Dave Newton建议):

If your language is so simple that every statement begins in its own line and is identified by one word only, then (as Gray pointed out in another comment) you can split the words in each line, then compare the first word against a map. However, I would suggest, instead of mapping the words to ints and then doing one big switch, to map them into objects instead, like this (suggested by Dave Newton):

interface Directive {
    public void execute(String line);
}

class LetDirective implements Directive {
    public void execute(String line) { ...handle LET directive here... }
}

...define other directives in the same way...

然后定义地图:

private Map<String, Directive> directives = new HashMap<String, Directive>();
directives.put("LET", new LetDirective());
...

然后在你的解析方法中:

Then in your parsing method:

int firstSpace = line.indexOf(' ');
String command = line;
if (firstSpace > 0)
    command = line.substring(0, firstSpace);
Directive directive = directives.get(command.toUpperCase());
if (directive != null)
    directive.execute(line);
else
    ...show some error...

每个指令都会必须自己解析该行的其余部分并在其execute()方法中正确处理它。

Each directive would have to parse the rest of the line on its own and handle it correctly inside its execute() method.

这对交换机的好处是你可以处理一个大量的命令没有一个巨大的方法结束,而是每个命令使用一个较小的方法。

The benefit of this over a switch is that you can handle a larger amount of commands without ending up with one gigantic method, but instead with one smaller method per each command.

这篇关于在java中编写解释器时切换或if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆