parser-combinators

Can parser combinators be made efficient?

拈花ヽ惹草 提交于 2019-11-28 14:30:38
问题 Around 6 years ago, I benchmarked my own parser combinators in OCaml and found that they were ~5× slower than the parser generators on offer at the time. I recently revisited this subject and benchmarked Haskell's Parsec vs a simple hand-rolled precedence climbing parser written in F# and was surprised to find the F# to be 25× faster than the Haskell. Here's the Haskell code I used to read a large mathematical expression from file, parse and evaluate it: import Control.Applicative import Text

Scala: How to combine parser combinators from different objects

给你一囗甜甜゛ 提交于 2019-11-28 08:23:40
Given a family of objects that implement parser combinators, how do I combine the parsers? Since Parsers.Parser is an inner class, and in Scala inner classes are bound to the outer object , the story becomes slightly complicated. Here's an example that attempts to combine two parsers from different objects. import scala.util.parsing.combinator._ class BinaryParser extends JavaTokenParsers { def anyrep: Parser[Any] = rep(any) def any: Parser[Any] = zero | one def zero: Parser[Any] = "0" def one: Parser[Any] = "1" } object LongChainParser extends BinaryParser { def parser1: Parser[Any] = zero

Recursive definitions with scala-parser-combinators

大兔子大兔子 提交于 2019-11-28 05:33:44
问题 I have been trying to build a SQL parser with the scala-parser-combinator library, which I've simplified greatly into the code below. class Expression case class FalseExpr() extends Expression case class TrueExpr() extends Expression case class AndExpression(expr1: Expression, expr2: Expression) extends Expression object SimpleSqlParser { def parse(sql: String): Try[Expression] = new SimpleSqlParser().parse(sql) } class SimpleSqlParser extends RegexParsers { def parse(sql: String): Try[_ <:

Understanding the tilde in Scala's parser combinators

 ̄綄美尐妖づ 提交于 2019-11-27 12:56:27
问题 I'm fairly new to Scala and while reading about parser combinators(The Magic Behind Parser Combinators, Domain-Specific Languages in Scala) I came across method definitions like this: def classPrefix = "class" ~ ID ~ "(" ~ formals ~ ")" I've been reading throught the API doc of scala.util.parsing.Parsers which defines a method named (tilde) but I still dont't really understand its usage in the example above. In that example (tilde) is a method which is called on java.lang.String which doesn't

Use Scala parser combinator to parse CSV files

痞子三分冷 提交于 2019-11-27 06:20:54
I'm trying to write a CSV parser using Scala parser combinators. The grammar is based on RFC4180 . I came up with the following code. It almost works, but I cannot get it to correctly separate different records. What did I miss? object CSV extends RegexParsers { def COMMA = "," def DQUOTE = "\"" def DQUOTE2 = "\"\"" ^^ { case _ => "\"" } def CR = "\r" def LF = "\n" def CRLF = "\r\n" def TXT = "[^\",\r\n]".r def file: Parser[List[List[String]]] = ((record~((CRLF~>record)*))<~(CRLF?)) ^^ { case r~rs => r::rs } def record: Parser[List[String]] = (field~((COMMA~>field)*)) ^^ { case f~fs => f::fs }

Scala: How to combine parser combinators from different objects

◇◆丶佛笑我妖孽 提交于 2019-11-27 02:17:20
问题 Given a family of objects that implement parser combinators, how do I combine the parsers? Since Parsers.Parser is an inner class, and in Scala inner classes are bound to the outer object, the story becomes slightly complicated. Here's an example that attempts to combine two parsers from different objects. import scala.util.parsing.combinator._ class BinaryParser extends JavaTokenParsers { def anyrep: Parser[Any] = rep(any) def any: Parser[Any] = zero | one def zero: Parser[Any] = "0" def one

Use Scala parser combinator to parse CSV files

旧城冷巷雨未停 提交于 2019-11-26 11:58:00
问题 I\'m trying to write a CSV parser using Scala parser combinators. The grammar is based on RFC4180. I came up with the following code. It almost works, but I cannot get it to correctly separate different records. What did I miss? object CSV extends RegexParsers { def COMMA = \",\" def DQUOTE = \"\\\"\" def DQUOTE2 = \"\\\"\\\"\" ^^ { case _ => \"\\\"\" } def CR = \"\\r\" def LF = \"\\n\" def CRLF = \"\\r\\n\" def TXT = \"[^\\\",\\r\\n]\".r def file: Parser[List[List[String]]] = ((record~((CRLF