parser-combinators

Scala Parser Combinators tricks for recursive bnf?

送分小仙女□ 提交于 2021-02-05 20:36:41
问题 Im trying to match this syntax: pgm ::= exprs exprs ::= expr [; exprs] expr ::= ID | expr . [0-9]+ My scala packrat parser combinator looks like this: import scala.util.parsing.combinator.PackratParsers import scala.util.parsing.combinator.syntactical._ object Dotter extends StandardTokenParsers with PackratParsers { lexical.delimiters ++= List(".",";") def pgm = repsep(expr,";") def expr :Parser[Any]= ident | expr~"."~num def num = numericLit def parse(input: String) = phrase(pgm)(new

Turning a list/sequence of combinator parsers into a single one

筅森魡賤 提交于 2020-02-02 00:38:08
问题 I have a list of values from which I can construct a list of parsers, that depend on these values by mapping (see example). Then what I want to do is turn the list of parsers into a single parser by concatenation. One possibility is using foldLeft and ~ : parsers.foldLeft(success(Nil)){case (ps,p) => rs ~ p ^^ {case xs ~ x => x ::xs}} ^^ (_.reverse) Is this efficient? I don't know how combinator parsers work; will there be a call stack with depth of length of the list? Thus may I run into SO

Scala parser combinator based calculator that can also take a dataRecord

妖精的绣舞 提交于 2020-01-05 11:57:05
问题 I have created a Scala parser combinator to filter data records based on the answer I got to an previous question How to parse a string with filter citeria in scala and use it to filter objects I would like to add the calculator parser combinator from the answer to this question Operator Precedence with Scala Parser Combinators to the bottom of the parser combinator that I created based on the first question. The calculator parser combinator therefore needs to accept a dataRecord so that an

Parsing a blank / whitespace with RegexParsers

别等时光非礼了梦想. 提交于 2020-01-04 02:19:14
问题 What is the problem with parsing the blank/whitespace? scala> object BlankParser extends RegexParsers { def blank: Parser[Any] = " " def foo: Parser[Any] = "foo" } defined module BlankParser scala> BlankParser.parseAll(BlankParser.foo, "foo") res15: BlankParser.ParseResult[Any] = [1.4] parsed: foo scala> BlankParser.parseAll(BlankParser.blank, " ") res16: BlankParser.ParseResult[Any] = [1.2] failure: ` ' expected but ` ' found ^ scala> 回答1: the lexer for scala throws blankspaces away. try

Cannot compute minimal length of a parser - uu-parsinglib in Haskell

China☆狼群 提交于 2020-01-02 04:42:08
问题 Lets see the code snippet: pSegmentBegin p i = pIndentExact i *> ((:) <$> p i <*> ((pEOL *> pSegment p i) <|> pure [])) if I change this code in my parser to: pSegmentBegin p i = do pIndentExact i ((:) <$> p i <*> ((pEOL *> pSegment p i) <|> pure [])) I've got an error: canot compute minmal length of a parser due to occurrence of a moadic bind, use addLength to override I thought the above parser should behave the same way. Why this error can occur? EDIT The above example is very simple (to

Parsing an indentation based language using scala parser combinators

青春壹個敷衍的年華 提交于 2020-01-02 03:12:08
问题 Is there a convenient way to use Scala's parser combinators to parse languages where indentation is significant? (e.g. Python) 回答1: Let's assume we have a very simple language where this is a valid program block inside the block and we want to parse this into a List[String] with each line inside the block as one String . We first define a method that takes a minimum indentation level and returns a parser for a line with that indentation level. def line(minIndent:Int):Parser[String] = repN

FParsec: how to combine parsers so that they will be matched in arbitrary order

霸气de小男生 提交于 2019-12-31 03:19:25
问题 The task is find particular key-value pairs and parse them. The pairs can occur in any order. My partially working attempt: open FParsec type Parser<'a> = Parser<'a, unit> type Status = Running | Done type Job = { Id: int Status: Status Count: int } let ws = spaces let jobId: Parser<int> = ws >>. skipStringCI "Job id" >>. ws >>. skipChar '=' >>. ws >>. pint32 let status: Parser<Status> = ws >>. skipStringCI "Status" >>. ws >>. skipChar '=' >>. ws >>. ( (skipStringCI "Running" >>% Running) <|>

When to use scala triple caret (^^^) vs double caret (^^) and the into method (>>)

烂漫一生 提交于 2019-12-30 08:51:48
问题 Can someone explain how and when to use the triple caret ^^^ (vs the double caret ^^) when designing scala parser combinators? And also when / how to use the parser.into() method (>>). 回答1: I'll begin with an example using Scala's Option type, which is similar in some important ways to Parser , but can be easier to reason about. Suppose we have the following two values: val fullBox: Option[String] = Some("13") val emptyBox: Option[String] = None Option is monadic, which means (in part) that

How to get left assoc operators with scala combinators?

自古美人都是妖i 提交于 2019-12-24 17:46:25
问题 I've tried /* inside RegexParser class */ def exp : Parser[Exp] = term~addop_chain ^^ {case l~ThenAdd(optype,r) => AritOp(l,optype,r)} def addop_chain : Parser[Exp] = ("+"|"-")~term~addop_chain ^^ {case sym~term~ThenAdd(optype,r) => ThenAdd(sym, AritOp(term,optype,r)) } | ("+"|"-")~term ^^ {case sym~term => ThenAdd(sym, term)} def term = /* right now, only int literal. code unrelevant */ /* Case classes for storing: */ case class ThenAdd(sym: String, r: Exp) case class AritOp(l: Exp, sym:

Stack overflow when using parser combinators

随声附和 提交于 2019-12-24 12:01:13
问题 import scala.util.parsing.combinator._ object ExprParser extends JavaTokenParsers { lazy val name: Parser[_] = "a" ~ rep("a" | "1") | function_call lazy val function_call = name ~ "(" ~> name <~ ")" } recurs indefinitely for function_call.parseAll("aaa(1)") . Obviously, it is because 1 cannot inter the name and name enters the function_call , which tries the name, which enters the funciton call. How do you resolve such situations? There was a solution to reduce name to simple identifier def