为简单的伪代码语言创建解析器

为简单的伪代码语言创建解析器

本文介绍了为简单的伪代码语言创建解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中创建一个简单的解析器,用于像伪语言(保持僵化)的伪代码。
一个示例伪代码是 -

I wanted to make a simple parser, for a "pseudo code" like language(kept rigid), in Java.A sample pseudo code would be -

//This is a comment
$x1 = readint
$x2 = readint

$dx = $x2 - $x1
#f = $dx / 2

if ($dx > 0)
{
  loop while(#f > 1)
  {
     print(#f)
     #f = #f / 2
  }
}

请注意,上面的代码是严格的,因此不能有多个语句一行,整数以$开头,浮点数以#等开头。

Note that above code is rigid in that, there can not be more than one statement on a line, integers start with $, floats start with # etc.

要解析此类代码,首先我可以使用 StringTokenizer ,然后是正则表达式,以匹配整数变量,浮点变量或关键字。

To parse such code, first I can use StringTokenizer, and then regular expression, to match integer-variables, float-variables, or Keywords.

这种方法有用吗?对于循环语句,我如何存储表达式,这样我就不必在每次迭代中都进行标记化?

Is this approach good? For statements in loop, how can i store expressions, so that i don't have to tokenize in each iteration?

我能想到转换表达式(比如#f = #f / 2)抛光表示法,然后存储在堆栈中。在每次迭代中,在弹出操作数时,我可以替换每个变量的值。但这有效吗?

I could think of converting expressions (like #f = #f / 2) to polish notation, and then to store in stack. And in each iteration, while popping operands I could replace value for each variable. But is this efficient enough?

提前致谢,任何建议。

推荐答案

虽然我认为你想为这样的语言构建一个解析器很棒,但这样做比它看起来要困难得多。解析是一个研究得很好的问题,你可以使用许多优秀的算法,但它们很难手工实现。虽然您可以使用转换为RPN等技巧来处理较小的示例(如解析表达式),但构建完整的编程语言需要更复杂的一系列技巧。

Although I think that it's great that you want to build a parser for a language like this, doing so is much harder than it looks. Parsing is a very well-studied problem and there are many excellent algorithms that you can use, but they are extremely difficult to implement by hand. While you can use tricks like conversions to RPN for smaller examples like parsing expressions, building up a full programming language requires a much more complex set of tricks.

解析语言对于这种复杂性,你可能最好使用解析器生成器,而不是试图手动编写自己的解析器。 和是两个众所周知的工具,可以准确地完成您想要完成的任务,我强烈建议您使用其中的两个。

To parse a language of this complexity, you are probably best off using a parser generator rather than trying to write your own by hand. ANTLR and Java CUP are two well-known tools for doing precisely what you're interested in accomplishing, and I would strongly suggest using one of the two of them.

希望这有助于!

这篇关于为简单的伪代码语言创建解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:08