如何实现一个迷你语言? [英] How to implement a mini language?

查看:34
本文介绍了如何实现一个迷你语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Windows 8.1 64 位
  • 微软 Visual Studio
  • C#

在 C# 中实现微型语言

我在公司的工作是开发一款软件,可以自动评估公司产品的性能.评估包括打开阀门、流动化学品、从传感器获取信号、计算一些值等......

My job in my company is to make a software which automates evaluation of the performance of company products. The evaluation involves opening valves, flowing chemicals, fetching signals from sensors, calculating some values, etc...

问题是当用户想改变反应时间多长、阀门打开顺序、反应导管是否冲洗等程序和参数时,程序员必须更改源代码并构建另一个每次用户需要的软件.

The issue is when a user wants to change the procedure and parameters like how long the reaction time is, the order of opening valves, whether reaction conduit is flushed out or not, the programmer has to change the source code and build another software every time the user requires it.

所以我正在尝试用 C# 实现一种迷你语言.用户可以用通俗易懂的语言组织测量过程.用户在txt文件中写一段代码,我的软件读取它,解析每一行代码,提取命令和参数,调用软件中相应的方法.

So I'm trying to implement a mini language in C#. A user can organize a procedure of measurement with the easy-to-understand language. The user writes a code in a txt file, my software reads it, parse each line of code, extract commands and parameters, call corresponding method in the software.

例如,用户编写了一行代码,如 open_valve(3),我的软件读取代码并调用内部方法 send_commands_to_ADconverter(VALVE, 3)通过连接到计算机的 A/D 转换器向目标阀发送适当的命令.这是我的构想.

For example, a user writes a line of code like open_valve(3), my software reads the code and call an internal method send_commands_to_ADconverter(VALVE, 3) which sends appropriate commands to a target valve via an A/D converter connected to the computer. This is my conception.

然而,由于我缺乏编程技能,我不知道从哪里开始.什么技术可用?实现目标的关键词是什么?我怎样才能实现这个目标?任何帮助,将不胜感激.谢谢.

However, due to lack of my programming skill, I have no idea where to start. What technology is available? What are the keywords to achieve the goal? How can I achieve this objective? Any help would be appreciated. Thanks.

c# - 向 .NET 应用程序添加脚本功能 - 堆栈溢出'不是指通过 A/D 转换器访问传感器、阀门等外部设备,这对我的目的至关重要,因此不清楚这是上述链接的重复问题.

c# - Adding scripting functionality to .NET applications - Stack Overflow doesn't refer to accessing external devices like sensors, valves via A/D converter which is crucial for my purpose, so it is unclear this is a duplicate question of the link above.

推荐答案

为了创建一种语言,您需要某种解析器".您需要定义一个语法".通过语法解析您的程序"将产生一个结构,然后您可以在代码中调用实现您语言的每个功能的方法.

In order to create a language you need a "parser" of some sort. You will need to define a "grammar". Parsing your "progam" via the grammar will result in a structure that you can then call methods in your code that implement each feature of your language.

你在这里的学习曲线很大:) 很多奇怪的东西,比如 EBNF.您可能会看到很多对 Gold 和 ANTLR 等内容的引用.这些功能非常强大,但涉及诸如编译器"之类的东西,并增加了一定程度的复杂性,可能会令人困惑,并且需要在构建管道中执行额外的步骤.

You are on a big learning curve here :) lots of strange things like EBNF. You will probably see lots of references to things like Gold and ANTLR. These are hugely capable but involve things like "compiler compilers" and add a level of complexity that can be confusing and require extra steps in you build pipeline.

这里有几个我使用过的库,它们允许您在 c# 中定义语法.

Here are a couple of libraries I've used that allow you to define you grammar in c#.

Irony 非常好(我已经将它用于各种项目)但已经有一段时间没有维护了.

Irony is very good (I've used it for various projects) but hasn't been maintained for a while.

Eto.Parse 是更新的.它还内置了解析器,允许您通过解析 BNF 来创建语法.这很酷

Eto.Parse is more recent. It also has built in parsers that allow you to create a Grammer by parsing BNF. This is very cool

这篇关于如何实现一个迷你语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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