简体   繁体   中英

Translate Haskell Parsec grammar to Scala?

I'm trying to translate a grammar written in Haskell using Parsec into Scala's parser combinators.

The translation of the actual matching expressions is pretty straightforward and, at least in my opinion, even a little easier in Scala. What's not at all clear to me is how to handle the statefulness that Parsec passes around using monads.

A Scala parser reads in Input and produces a ParseResult[T].

In contrast, a GenParser in Haskell reads in input and a state and produces another parser. Passing that state around in Scala has me confused.

Does anyone have an example of stateful parsing in Scala that they'd be willing to share?

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). 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.

See also this question . There was a very interesting paper about Scala parser combinators, but I was not able to find it. Some spelunking on Scala Lang might turn it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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