接近 Scala 中的文本解析
我正在制作一个将解析 Scala 中的命令的应用程序。命令的一个例子是:
todo get milk for friday
因此计划是让一个非常智能的解析器将行分开并识别命令部分以及字符串中存在对时间的引用这一事实。
一般来说,我需要在 Scala 中制作一个分词器。所以我想知道我对此有什么选择。我熟悉正则表达式,但我还计划制作一个类似 SQL 的搜索功能:
search todo for today with tags shopping
而且我觉得正则表达式在实现有很多变化的命令时会不够灵活。这让我想到实施某种语法。
在 Scala 中我有哪些选择?
I'm making an application that will parse commands in Scala. An example of a command would be:
todo get milk for friday
So the plan is to have a pretty smart parser break the line apart and recognize the command part and the fact that there is a reference to time in the string.
In general I need to make a tokenizer in Scala. So I'm wondering what my options are for this. I'm familiar with regular expressions but I plan on making an SQL like search feature also:
search todo for today with tags shopping
And I feel that regular expressions will be inflexible implementing commands with a lot of variation. This leads me to think of implementing some sort of grammar.
What are my options in this regard in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要搜索“解析器组合器”。我有一篇使用这种方法的博客文章(http://cleverlytitled.blogspot。 com/2009/04/shunting-yard-algorithm.html),但我认为最好的参考是 Stefan Zieger 的这一系列帖子(http://szeiger.de/blog/2008/07/27/formal-language-processing-in- scala-part-1/)
You want to search for "parser combinators". I have a blog post using this approach (http://cleverlytitled.blogspot.com/2009/04/shunting-yard-algorithm.html), but I think the best reference is this series of posts by Stefan Zieger (http://szeiger.de/blog/2008/07/27/formal-language-processing-in-scala-part-1/)
以下是我在 2009 年 9 月所做的关于 Scala 解析器组合器的演示中的幻灯片。 (http://sites.google.com/site/compulsiontocode/files /lambdalounge/ImplementingExternalDSLsUsingScalaParserCombinators.ppt) 演示了一个简单的类 Logo 语言的实现。它可能会提供一些见解。
Here are slides from a presentation I did in Sept. 2009 on Scala parser combinators. (http://sites.google.com/site/compulsiontocode/files/lambdalounge/ImplementingExternalDSLsUsingScalaParserCombinators.ppt) An implementation of a simple Logo-like language is demonstrated. It might provide some insights.
Scala 有一个解析器库 (scala.util.parsing.combinator),它使人们能够直接从其 编写解析器EBNF 规范。如果您的语言有 EBNF,那么编写 Scala 解析器应该很容易。如果没有,您最好首先尝试正式定义您的语言。
Scala has a parser library (scala.util.parsing.combinator) which enables one to write a parser directly from its EBNF specification. If you have an EBNF for your language, it should be easy to write the Scala parser. If not, you'd better first try to define your language formally.