最好的现代文本解析? 又名 何时使用 Lex 和 Yacc?

发布于 2024-07-19 05:46:40 字数 498 浏览 5 评论 0原文

我有带有命令和参数的文本输入块,每行一个,例如

XYZ ARG1,ARG2,ARG3,...,ARGN

我想验证 XYZ 的参数是否适合该特定命令,并执行正确的代码块,如果他们是。 有大约 100 个命令,其中一些具有可变数量的参数、不同的关系(即,如果调用命令 XYZ ,那么我也需要调用命令 ABC )。

还存在命令,例如:

COMMAND
XYZ ARG1
BEF ARG1 ARG2
ENDCOMMAND

重要的是文本包含在 COMMANDENDCOMMAND 中。

通常,对于这样的事情,我会使用 Lex 和 Yacc 而不是正则表达式,但是还有更现代的吗? 该代码是用 C# 编写的。 除了老派的 C Lex 和 Yacc 之外,MSDN 中还有其他东西可以做到这一点吗?

I have as input blocks of text with commands and arguments, one per line, such as

XYZ ARG1,ARG2,ARG3,...,ARGN

And I want to verify that the arguments to XYZ are well formed for that particular command and execute the correct block of code if they are. There are something like ~100 commands, some of which have variable numbers of arguments, different relationships (i.e. if command XYZ was called then I need to have command ABC called as well).

Also commands exist such as:

COMMAND
XYZ ARG1
BEF ARG1 ARG2
ENDCOMMAND

It is important that the text is contained within COMMAND and ENDCOMMAND.

Typically for something like this I would use Lex and Yacc rather than regex's, but is there anything more modern? The code is written in C#. Is there anything in MSDN that does this rather than old school C Lex and Yacc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我不会写诗 2024-07-26 05:46:40

如果您正在寻找 Lex/Yacc 的替代方案,请查看 ANTLR。 它支持多种语言的代码生成,包括 C#

If you are looking for an alternative to Lex/Yacc, check out ANTLR. It supports code generation in a variety of lanagages, including C#.

寄离 2024-07-26 05:46:40

ANTLR 可以处理词法分析和解析,并且可以生成 C#(除了 Java、C++ 和 Python)。 它非常成熟,有大量文档和示例。 它还生成比 YACC 更好的错误消息。

ANTLR can handle both lexing and parsing and it can generate C# (in addition to Java, C++ and Python). It's very mature, has lots of documentation and lots of examples. It also generates much nicer error messages that YACC.

别想她 2024-07-26 05:46:40

你有一个比“年龄”更大的问题,因为我不确定任何知名的 C-ish 编译器是否能够与 C# 一起工作。 Boost 的新奇解析模板也是如此。

您可能必须使用一些深奥的东西,例如 GrammaticaSpart(选择我最喜欢的两个Google 点击

编辑:
经过进一步查看,ANTLR 似乎支持 C#。 ANTLR 非常有名,而且比 LEX/YACC 更新得多,所以我建议检查一下。

You have a bigger problem than "age", in that I'm not sure any of the big well-known C-ish compiler-compilers are going to work with C#. The same goes for Boost's newfangled parsing templates.

You are probably going to have to go with something esoteric like Grammatica or Spart (to pick my top two Google hits)

EDIT:
After a bit more looking, it appears that ANTLR has support for C#. ANTLR is very well known, and much newer than LEX/YACC, so I'd suggest checking it out.

杀手六號 2024-07-26 05:46:40

对于像这样的简单解析问题,您可以编写一个递归下降解析器。 当然,假设您的语言相对固定,并且不会发展成为完整的编程语言。 如果存在任何危险,请硬着头皮使用 ANTLR 或同等产品。

For a simple parsing problem like this, you can write a recursive descent parser. Assuming of course, your language is relatively fixed and isn't going to grow into a full programming language. If there's any danger of that, bite the bullet and use ANTLR or equivalent.

怪我鬧 2024-07-26 05:46:40

看看 jay,yacc 重定向到 C# 和 Java。 它包含在 mono 项目中。

http://code.google.com/p/jayc/

Take a look at jay, yacc retargeted to C# and Java. It is included in the mono project.

http://code.google.com/p/jayc/

〃安静 2024-07-26 05:46:40

如果您是这么想的话,.NET Framework 中并没有什么特别的东西。

乍一看,您的命令结构看起来相对简单,因此手动解析非常适合这里,并且它几乎总是快速的解决方案。
这还允许您检查命令参数的实际值的正确性,而不仅仅是它们的语法有效性。

There is no special thing in the .NET Framework, if you mean that.

At first glance your command structure looks relatively simple, so manual parsing would well suited here and it is almost always the fasted solution.
This also would allow you to check actual values of the command arguments for correctness and not just their syntax validity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文