C# 的 Lex/Yacc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:22:41  来源:igfitidea点击:

Lex/Yacc for C#?

c#parsingyacc

提问by Roger Lipscombe

Actually, maybe not full-blown Lex/Yacc. I'm implementing a command-interpreter front-end to administer a webapp. I'm looking for something that'll take a grammar definition and turn it into a parser that directly invokes methods on my object. Similar to how ASP.NET MVC can figure out which controller method to invoke, and how to pony up the arguments.

实际上,也许不是成熟的 Lex/Yacc。我正在实现一个命令解释器前端来管理 web 应用程序。我正在寻找一些可以接受语法定义并将其转换为直接调用我的对象上的方法的解析器的东西。类似于 ASP.NET MVC 如何确定要调用的控制器方法,以及如何使用参数。

So, if the user types "create foo" at my command-prompt, it should transparently call a method:

因此,如果用户在我的命令提示符下键入“create foo”,它应该透明地调用一个方法:

private void Create(string id) { /* ... */ }

Oh, and if it could generate help text from (e.g.) attributes on those controller methods, that'd be awesome, too.

哦,如果它可以从那些控制器方法的(例如)属性生成帮助文本,那也太棒了。

采纳答案by Anton Gogolev

I'm not sure Lex/Yacc will be of any help. You'll just need a basic tokenizer and an interpreter which are faster to write by hand. If you're still into parsing route see Irony.

我不确定 Lex/Yacc 会有什么帮助。您只需要一个基本的标记器和一个手动编写速度更快的解释器。如果您仍在解析路线,请参阅Irony

As a sidenote: have you considered PowerShell and its commandlets?

作为旁注:您是否考虑过 PowerShell 及其命令行开关?

回答by Kieron

As Anton said, PowerShell is probably the way to go. If you do want a lex/ yacc implementation then Malcolm Crowehas a good set.

正如 Anton 所说,PowerShell 可能是要走的路。如果你确实想要一个 lex/yacc 实现,那么Malcolm Crowe有一个很好的集合。

Edit:Direct Link to the Compiler Tools

编辑:直接链接到编译器工具

回答by KristoferA

Still early CTP so can't be used in production apps but you may be interested in Oslo/MGrammar: http://msdn.microsoft.com/en-us/oslo/

仍然是早期的 CTP,因此不能用于生产应用程序,但您可能对 Oslo/MGrammar 感兴趣:http: //msdn.microsoft.com/en-us/oslo/

回答by Magnus Akselvoll

If you don't fear alpha software and want an alternative to Lex / Yacc for creating your own languages, you might look into Oslo. I would recommend you to sit through session recordings of sessions TL27 and TL31 from last years PDC. TL31 directly addresses the creation of Domain Specific Languages using Oslo.

如果您不害怕 alpha 软件,并且想要替代 Lex/Yacc 来创建您自己的语言,您可以查看Oslo。我建议您观看去年 PDC 的TL27 和 TL31 会议录音。TL31 直接解决了使用 Oslo 创建领域特定语言的问题。

回答by rix0rrr

Coco/R is a compiler generator with a .NET implementation. You could try that out, but I'm not sure if getting such a library to work would be faster than writing your own tokenizer.

Coco/R 是一个带有 .NET 实现的编译器生成器。您可以尝试一下,但我不确定让这样的库工作是否比编写自己的标记器更快。

http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

回答by Bartek Szabat

I would suggest csflex- C# port of flex - most famous unix scanner generator.

我建议csflex- flex 的 C# 端口 - 最著名的 unix 扫描仪生成器。

回答by Jonathan Parker

I believe that lex/yacc are in one of the SDKs already (i.e. RTM). Either Windows or .NET Framework SDK.

我相信 lex/yacc 已经在其中一个 SDK 中(即 RTM)。Windows 或 .NET Framework SDK。

回答by Kris Erickson

Also look at Antlr, which has C# support.

另请查看Antlr,它具有 C# 支持。

回答by Andy Mortimer

I've done a couple of small projects with GPLEX/GPPG, which are pretty straightforward reimplementations of LEX/YACC in C#. I've not used any of the other tools above, so I can't really compare them, but these worked fine.

我已经用 GPLEX/GPPG 完成了几个小项目,它们是在 C# 中对 LEX/YACC 的非常简单的重新实现。我没有使用过上面的任何其他工具,所以我无法真正比​​较它们,但是这些工作得很好。

GPPG can be found hereand GPLEX here.

GPPG可以发现这里和GPLEX这里

That being said, I agree, a full LEX/YACC solution probably is overkill for your problem. I would suggest generating a set of bindings using IronPython: it interfaces easily with .NET code, non-programmers seem to find the basic syntax fairly usable, and it gives you a lot of flexibility/power if you choose to use it.

话虽如此,我同意,完整的 LEX/YACC 解决方案可能对您的问题来说太过分了。我建议使用 IronPython 生成一组绑定:它可以轻松地与 .NET 代码交互,非程序员似乎发现基本语法相当可用,如果您选择使用它,它会为您提供很大的灵活性/功能。

回答by Tamas Ionut

Gardens Point Parser Generator hereprovides Yacc/Bison functionality for C#. It can be donwloaded here. A usefull example using GPPG is provided here

花园点分析器发电机在这里提供的Yacc /鬼功能为C#。可以在 这里下载此处提供一个使用 GPPG 的有用示例