简体   繁体   English

将Haskell Parsec语法翻译成Scala?

[英]Translate Haskell Parsec grammar to Scala?

I'm trying to translate a grammar written in Haskell using Parsec into Scala's parser combinators. 我正在尝试使用Parsec将用Haskell编写的语法翻译成Scala的解析器组合器。

The translation of the actual matching expressions is pretty straightforward and, at least in my opinion, even a little easier in Scala. 实际匹配表达式的翻译非常简单,至少在我看来,在Scala中更容易一些。 What's not at all clear to me is how to handle the statefulness that Parsec passes around using monads. 对我来说一点都不清楚的是如何处理Parsec使用monad传递的状态。

A Scala parser reads in Input and produces a ParseResult[T]. Scala解析器读入Input并生成ParseResult [T]。

In contrast, a GenParser in Haskell reads in input and a state and produces another parser. 相反,Haskell中的GenParser读入输入和状态并生成另一个解析器。 Passing that state around in Scala has me confused. 在Scala中传递这种状态令我感到困惑。

Does anyone have an example of stateful parsing in Scala that they'd be willing to share? 有没有人在Scala中有一个状态解析的例子,他们愿意分享?

The only way I know of to handle state-fullness in Scala Parsers Combinators is through the into method, also known as >> and flatMap (and, yes, you can use it in for-comprehensions). 我知道在Scala Parsers Combinators中处理状态丰满的唯一方法是通过into方法,也称为>>flatMap (是的,你可以在for-comprehensions中使用它)。 However, it passes state (or, more precisely, parse result) into a parser, and not along the next parsers, which seems to be what you are describing. 然而,它通过状态(或者,更准确地说,分析结果) 解析器,而不是沿下一个解析器,这似乎是你所描述的。

Not knowing Haskell's Parsec, it is difficult for me to guess at how that can be used to translate your grammar. 不知道Haskell的Parsec,我很难猜测如何用它来翻译你的语法。

See also this question . 另见这个问题 There was a very interesting paper about Scala parser combinators, but I was not able to find it. 有一篇关于Scala解析器组合的非常有趣的论文,但我无法找到它。 Some spelunking on Scala Lang might turn it up. Scala Lang上的一些人可能会把它打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM