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

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

问题描述

我开始一个项目,我需要实现一个轻量级的解释器。
解释器用于执行简单的科学算法。
这个解释器将使用的编程语言应该很简单,因为它面向非软件开发人员(例如,数学家)。

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:


  • 实数,变量,多维数组

  • , - ,*,/,%)和布尔(==,!=,<>,< =,> =)操作

  • 循环表达式(if)

  • 函数

  • Real numbers, variables, multi-dimensional arrays
  • Binary (+, -, *, /, %) and Boolean (==, !=, <, >, <=, >=) operations
  • Loops (for, while), Conditional expressions (if)
  • Functions

MathWorks MatLab 是一个很好的例子,我的标题,只是更简单。
解释器将用作演示算法的环境;简单的算法,例如查找数据集/数组的平均值,或稍微更复杂的算法,例如高斯消除< a>或 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)

推荐答案

p>我会将其写入 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天全站免登陆