Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 791
Todd O'Bryan Avatar asked Mar 23 '12 02:03

Todd O'Bryan


1 Answers

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.

like image 171
Daniel C. Sobral Avatar answered Oct 15 '22 06:10

Daniel C. Sobral