构建一个简单的解释器 [英] Constructing a simple interpreter

查看:32
本文介绍了构建一个简单的解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一个需要实现轻量级解释器的项目.解释器用于执行简单的科学算法.此解释器将使用的编程语言应该很简单,因为它针对的是非软件开发人员(例如,数学家).

I’m starting a project where I need to implement a light-weight interpreter. The interpreter is used to execute simple scientific algorithms. The programming language that this interpreter will use should be simple, since it is targeting non- software developers (for example, mathematicians.)

解释器应该支持基本的编程语言特性:

The interpreter should support basic programming languages features:

  • 实数、变量、多维数组
  • 二进制(+、-、*、/、%)和布尔(==、!=、<、>、<=、>=)运算
  • 循环(for、while)、条件表达式(if)
  • 功能

MathWorks MatLab 是我要去的一个很好的例子,只是简单得多.解释器将用作演示算法的环境;简单的算法,例如查找数据集/数组的平均值,或者稍微复杂一些的算法,例如 高斯消元RSA.

MathWorks MatLab is a good example of where I’m heading, just much simpler. The interpreter will be used as an environment to demonstrate algorithms; simple algorithms such as finding the average of a dataset/array, or slightly more complicated algorithms such as Gaussian elimination or RSA.

我在该主题上找到的最佳/最实用的资源是 Ron Ayoub 在代码项目中的条目 (使用解释器模式解析代数表达式) - 我的问题的缩小版本的完美示例.

Best/Most practical resource I found on the subject is Ron Ayoub’s entry on Code Project (Parsing Algebraic Expressions Using the Interpreter Pattern) - a perfect example of a minified version of my problem.

紫龙书好像太多了,还有什么实用的?

The Purple Dragon Book seems to be too much, anything more practical?

解释器将使用 C# 实现为 .NET 库.但是,欢迎提供任何平台的资源,因为此问题的设计架构部分最具挑战性.

The interpreter will be implemented as a .NET library, using C#. However, resources for any platform are welcome, since the design-architecture part of this problem is the most challenging.

任何实用资源?

(请避免这不是微不足道的"或为什么要重新发明轮子"的回答)

(please avoid "this is not trivial" or "why re-invent the wheel" responses)

推荐答案

我会把它写在 ANTLR 中.写好语法,让ANTLR生成一个C#解析器.您可以 ANTLR 请求解析树,并且解释器可能已经可以对解析树进行操作.也许您必须将解析树转换为一些更抽象的内部表示(尽管 ANTLR 已经允许在生成树时省略不相关的标点符号).

I would write it in ANTLR. Write the grammar, let ANTLR generate a C# parser. You can ANTLR ask for a parse tree, and possibly the interpreter can already operate on the parse tree. Perhaps you'll have to convert the parse tree to some more abstract internal representation (although ANTLR already allows to leave out irrelevant punctuation when generating the tree).

这篇关于构建一个简单的解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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