parser-combinators

Scala combinator parser, what does >> mean?

浪子不回头ぞ 提交于 2019-12-03 07:56:37
问题 I am little bit confusing about ">>" in scala. Daniel said in Scala parser combinators parsing xml? that it could be used to parameterize the parser base on result from previous parser. Could someone give me some example/hint ? I already read scaladoc but still not understand it. thanks 回答1: As I said, it serves to parameterize a parser, but let's walk through an example to make it clear. Let's start with a simple parser, that parses a number follow by a word: def numberAndWord = number ~

How do Scala parser combinators compare to Haskell's Parsec? [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-03 04:54:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I have read that Haskell parser combinators (in Parsec) can parse context sensitive grammars. Is this also true for Scala parser combinators? If so, is this what the "into" (aka ">>") function is for? What are some strengths/weaknesses of Scala's implementation of parser

Arithmetic Expression Grammar and Parser

安稳与你 提交于 2019-12-03 03:15:50
Recently I was looking for a decent grammar for arithmetic expressions but found only trivial ones, ignoring pow(..., ...) for example. Then I tried it on my own, but sometimes it didn´t worked as one expects. For example, I missed to allow a unary - in front of expressions and fixed it. Perhaps someone can take a look at my current approach and improve it. Furthermore I think others can take advantage because it´s a common task to be able to parse arithmetic expressions. import scala.math._ import scala.util.parsing.combinator._ import scala.util.Random class FormulaParser(val constants: Map

Ignoring C-style comments in a Scala combinator parser

心不动则不痛 提交于 2019-12-03 02:59:48
What is the most simple way to make my parser respect (ignore) C-style comments. I'm interested in both comment types, though a solution for only one type is also welcome. I'm currently simply extending JavaTokenParsers. You can use a simple regular expression, as long as you don't nest comments. Put it inside whiteSpace : scala> object T extends JavaTokenParsers { | protected override val whiteSpace = """(\s|//.*|(?m)/\*(\*(?!/)|[^*])*\*/)+""".r | def simpleParser = ident+ | } defined module T scala> val testString = """ident // comment to the end of line | another ident /* start comment |

Scala combinator parser, what does >> mean?

大憨熊 提交于 2019-12-02 23:04:14
I am little bit confusing about ">>" in scala. Daniel said in Scala parser combinators parsing xml? that it could be used to parameterize the parser base on result from previous parser. Could someone give me some example/hint ? I already read scaladoc but still not understand it. thanks As I said, it serves to parameterize a parser, but let's walk through an example to make it clear. Let's start with a simple parser, that parses a number follow by a word: def numberAndWord = number ~ word def number = "\\d+".r def word = "\\w+".r Under RegexParsers , this will parse stuff like "3 fruits". Now,

How do Scala parser combinators compare to Haskell's Parsec? [closed]

感情迁移 提交于 2019-12-02 18:09:04
I have read that Haskell parser combinators (in Parsec) can parse context sensitive grammars. Is this also true for Scala parser combinators? If so, is this what the "into" (aka ">>") function is for? What are some strengths/weaknesses of Scala's implementation of parser combinators, vs Haskell's? Do they accept the same class of grammars? Is it easier to generate error messages or do other miscellaneous useful things with one or the other? How does packrat parsing (introduced in Scala 2.8) fit into this picture? Is there a webpage or some other resource that shows how different operators

parsec: string choice parser with useful error messages

橙三吉。 提交于 2019-12-01 09:18:58
Let's have following parser: parser :: GenParser Char st String parser = choice (fmap (try . string) ["head", "tail", "tales"] <?> "expected one of ['head', 'tail', 'tales']") When we parse the malformed input "ta" it will return the defined error but because of backtracking it will also talk about unexpected "t" at first position instead of unexpected " " at position 3. Is there an easy (or built-in) way of matching one of multiple expected strings that produces good error messages? I am talking about showing the correct position and in this case something like expected "tail" or "tales"

FParsec identifiers vs keywords

可紊 提交于 2019-12-01 08:31:36
For languages with keywords, some special trickery needs to happen to prevent for example "if" from being interpreted as an identifier and "ifSomeVariableName" from becoming keyword "if" followed by identifier "SomeVariableName" in the token stream. For recursive descent and Lex/Yacc, I've simply taken the approach (as per helpful instruction) of transforming the token stream between the lexer and the parser. However, FParsec doesn't really seem do a separate lexer step, so I'm wondering what the best way to deal with this is. Speaking of, it seems like Haskell's Parsec supports a lexer layer,

Combining lexer and parser in a parser combinator

放肆的年华 提交于 2019-12-01 06:30:28
I'm using uu-parsinglib , but I think the following question is parser combinator generic. Let's consider the following example: I've got a lexer with a combinator pLex , which produces a list of tokens (of type MyToken ). I now want to write a parser, which will consume the tokens and build an AST . What is the best way to connect the lexer and parser? Right now I have a lex function: lex s = parse ( (,) <$> pLex <*> pEnd) (createStr (LineColPos 0 0 0) s) Should I create a function parse p = ... ? If yes, how do I construct it to keep track of columns and lines from lexer? Or should I create

parsec: string choice parser with useful error messages

那年仲夏 提交于 2019-12-01 06:20:29
问题 Let's have following parser: parser :: GenParser Char st String parser = choice (fmap (try . string) ["head", "tail", "tales"] <?> "expected one of ['head', 'tail', 'tales']") When we parse the malformed input "ta" it will return the defined error but because of backtracking it will also talk about unexpected "t" at first position instead of unexpected " " at position 3. Is there an easy (or built-in) way of matching one of multiple expected strings that produces good error messages? I am