parser-combinators

FParsec: backtracking `sepBy`

怎甘沉沦 提交于 2019-12-12 10:54:36
问题 Consider the following toy grammar and parser: (* in EBNF: ap = "a", { "ba" } bp = ap, "bc" *) let ap = sepBy1 (pstring "a") (pstring "b") let bp = ap .>> (pstring "bc") let test = run bp "abababc" I get the following output: Error in Ln: 1 Col: 7 abababc ^ Expecting: 'a' Clearly sepBy1 sees the last b and expects it to lead into another a , failing when it doesn't find one. Is there a variant of sepBy1 which would backtrack over b and make this parse succeed? Is there any reason why I

Operator Precedence with Scala Parser Combinators

这一生的挚爱 提交于 2019-12-12 08:01:05
问题 I am working on a Parsing logic that needs to take operator precedence into consideration. My needs are not too complex. To start with I need multiplication and division to take higher precedence than addition and subtraction. For example: 1 + 2 * 3 should be treated as 1 + (2 * 3). This is a simple example but you get the point! [There are couple more custom tokens that I need to add to the precedence logic, which I may be able to add based on the suggestions I receive here.] Here is one

Positional before Named argument list parsing

不羁岁月 提交于 2019-12-12 03:44:09
问题 How would you do that in the parser combinators def namedAfterPos[P, N](pos: Parser[P], nmd: Parser[N], sep: Parser[_] = ",") = ??? List("a", "a,a,a", "a,a,a=b,a=b", "a=b, a=b") map (_ parseWith namedAfterPos("a", "a=b")) map {case Success(res, _) => res} val Failure("positional is not expected after named", pos) = "a,a=b,a" parseWith namedAfterPos("a", "a=b") 回答1: Ok, here is mind approach scala> def namedAfterPos[P, N](pos: Parser[P], nmd: Parser[N], sep: Parser[_] = ",") = { // NB! http:/

Validating list of strings

与世无争的帅哥 提交于 2019-12-12 03:39:45
问题 This is a follow up to my previous question. Suppose I need to write a function validate in order to make sure that a given list of strings consists of "a", "b", and one or more "c". def validate(ss: List[String]): Either[NonEmptyList[MyError], Unit] = ??? Suppose I have three functions to check if a given string is "a", "b", or "c": def validateA(str: String): Either[MyError, Unit] = ??? def validateB(str: String): Either[MyError, Unit] = ??? def validateC(str: String): Either[MyError, Unit]

Using `err` in a Child Parser

半世苍凉 提交于 2019-12-11 17:52:27
问题 In the following Parser: object Foo extends JavaTokenParsers { def word(x: String) = s"\\b$x\\b".r lazy val expr = aSentence | something lazy val aSentence = noun ~ verb ~ obj lazy val noun = word("noun") lazy val verb = word("verb") | err("not a verb!") lazy val obj = word("object") lazy val something = word("FOO") } It will parse noun verb object . scala> Foo.parseAll(Foo.expr, "noun verb object") res1: Foo.ParseResult[java.io.Serializable] = [1.17] parsed: ((noun~verb)~object) But, when

Returning Error for Invalid Parse inside of `rep`

风格不统一 提交于 2019-12-11 09:59:09
问题 In the following DSL, I'm successfully parsing "foo", followed by 0 or more repititions of conj ~ noun . object Foo extends JavaTokenParsers { def word(x: String) = s"\\b$x\\b".r lazy val expr = word("foo") ~ rep(conj ~ noun) val noun = word("noun") val conj = word("and") | err("not a conjunction!") } credit : Thanks to Travis Brown for explaining the need for the word function here. It looks good when testing out an invalid conjunction. scala> Foo.parseAll(Foo.expr, "foo an3 noun") res29:

I don't understand how to use the lexeme function

主宰稳场 提交于 2019-12-11 04:34:04
问题 From Text.Parsec.Token : lexeme p = do { x <- p; whiteSpace; return x } It appears that lexeme takes a parser p and delivers a parser that has the same behavior as p, except that it also skips all the trailing whitespace. Correct? Then how come the following does not work: constant :: Parser Int constant = do digits <- many1 digit return (read digits) lexConst :: Parser Int lexConst = lexeme constant The last line results in the following error message: Couldn't match expected type `ParsecT

Transform data frame into matrix with counts

跟風遠走 提交于 2019-12-11 03:39:15
问题 I have data files structured like this: OTU1 PIA0 1120 OTU2 PIA1 2 OTU2 PIA3 6 OTU2 PIA4 10 OTU2 PIA5 1078 OTU2 PIN1 24 OTU2 PIN2 45 OTU2 PIN3 261 OTU2 PIN4 102 OTU3 PIA0 16 OTU3 PIA1 59 OTU3 PIA2 27 OTU3 PIA3 180 OTU3 PIA4 200 OTU3 PIA5 251 OTU3 PIN0 36 OTU3 PIN1 61 OTU3 PIN2 156 OTU3 PIN3 590 OTU3 PIN4 277 OTU4 PIA0 401 OTU4 PIN0 2 And I want to create a matrix that shows combination of data from the second column taking the first column as reference for the counts of combination (showing

Differentiating logical from other infix operators

家住魔仙堡 提交于 2019-12-11 02:44:14
问题 I'm trying to parse SQL search conditions and having trouble getting the parser to differentiate logical ( AND , OR ) from other infix operators. I'm parsing them as different nodes (perhaps that's difficult to do), but simplifies the evaluation phase. Here's the relevant code snippet (I can include more if necessary). let opp = OperatorPrecedenceParser<_,_,_>() let scalarExpr = opp.ExpressionParser opp.TermParser <- constant <|> id <|> between lparen rparen scalarExpr <|> scalarExpr //infix

Using parser combinators to collate lines of text

痞子三分冷 提交于 2019-12-11 01:55:16
问题 I'm trying to parse a text file using parser combinators. I want to capture the index and text in a class called Example . Here's a test showing the form on an input file: object Test extends ParsComb with App { val input = """ 0) blah1 blah2 blah3 1) blah4 blah5 END """ println(parseAll(examples, input)) } And here's my attempt that doesn't work: import scala.util.parsing.combinator.RegexParsers case class Example(index: Int, text: String) class ParsComb extends RegexParsers { def examples: