parser-combinators

def or val or lazy val for grammar rules?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 16:26:53
问题 I know the difference between def and val and lazy val in general, but I'm not sure about their repercussions when it comes to Parser Combinators. All examples I have seen contain code like this: def statement: Parser[Statement] = ifStatement | whileStatement | expressionStatement | ... From a few experiments I have done, it seems val and lazy val work as well, but I'm not sure if there are cases where they wouldn't work anymore, like recursion or positioned parsers or whatnot. Please

Parser combinator not terminating - how to log what is going on?

烂漫一生 提交于 2019-12-10 12:59:58
问题 I am experimenting with parser combinators and I often run into what seems like infinite recursions. Here is the first one I ran into: import util.parsing.combinator.Parsers import util.parsing.input.CharSequenceReader class CombinatorParserTest extends Parsers { type Elem = Char def notComma = elem("not comma", _ != ',') def notEndLine = elem("not end line", x => x != '\r' && x != '\n') def text = rep(notComma | notEndLine) } object CombinatorParserTest { def main(args:Array[String]): Unit =

Scalac hanging on phase typer of RegexParser

假如想象 提交于 2019-12-10 11:25:30
问题 I have a scala program which among other things has a parser-combinator. This is done by extending scala.util.parsing.combinator.RegexParsers . I had developed it using Scala 2.10 and all was working fine. Yesterday I upgraded my system to Scala 2.11.4, together with IntelliJ 14.02 (not that it matters). However, whenever I try to compile this program now, scalac hangs during this phase: scalac: phase typer on MyParser.scala I changed absolutely nothing to this code, I can't understand why it

Scala parser combinators for language embedded in html or text (like php)

我与影子孤独终老i 提交于 2019-12-10 11:07:38
问题 I have been playing around with Scala parser combinators for some time now, and learned some of the ways to make it behave nicely and do the most of the things I want, using the built in function. But how do you make an embedded language (like php or ruby's erb)? It requires whitespace to not be ignored, outside the embedding of real code. I managed to make a simple parser that matches all text up to a given regex match, but I am looking for a better, prettier way of doing this. There is

How can I ignore non-matching preceding text when using Scala's parser combinators?

心已入冬 提交于 2019-12-10 11:00:49
问题 I really like parser combinators but I'm not happy with the solution I've come up with to extract data when I don't care about the text before the relevant text. Consider this small parser to get monetary amounts: import scala.util.parsing.combinator._ case class Amount(number: Double, currency: String) object MyParser extends JavaTokenParsers { def number = floatingPointNumber ^^ (_.toDouble) def currency = """\w+""".r ^? ({ case "USD" => "USD" case "EUR" => "EUR" }, "Unknown currency code:

Simply using parsec in python

笑着哭i 提交于 2019-12-08 21:23:24
问题 I'm looking at this library, which has little documentation: https://pythonhosted.org/parsec/#examples I understand there are alternatives, but I'd like to use this library. I have the following string I'd like to parse: mystr = """ <kv> key1: "string" key2: 1.00005 key3: [1,2,3] </kv> <csv> date,windspeed,direction 20190805,22,NNW 20190805,23,NW 20190805,20,NE </csv>""" While I'd like to parse the whole thing, I'd settle for just grabbing the <tags> . I have: >>> import parsec >>> tag_start

How to skip whitespace but use it as a token delimeter in a parser combinator

怎甘沉沦 提交于 2019-12-07 18:52:20
问题 I am trying to build a small parser where the tokens (luckily) never contain whitespace. Whitespace (spaces, tabs and newlines) are essentially token delimeters (apart from cases where there are brackets etc.). I am extending the RegexParsers class. If I turn on skipWhitespace the parser is greedily joining tokens together when the next token matches the regular expression of the previous one. If I turn off skipWhitespace , on the other hand, it complains because of the spaces not being part

Scala Parser Combinators: Parsing in a stream

我是研究僧i 提交于 2019-12-07 18:12:56
问题 I'm using the native parser combinator library in scala, and I'd like to use it to parse a number of large files. I have my combinators set up, but the file that I'm trying to parse is too large to be read into memory all at once. I'd like to be able to stream from an input file through my parser and read it back to disk so that I don't need to store it all in memory at once.My current system looks something like this: val f = Source.fromFile("myfile") parser.parse(parser.document.+, f.reader

scala combinator parser not backtracking as I would have thought…

◇◆丶佛笑我妖孽 提交于 2019-12-07 07:46:00
问题 I've been staring myself blind on this problem I have and I guess this will probably be a real stupid question. But I have to swallow my pride. I have this combinator parser that doesn't backtrack like I thought it would. I've been reducing it down to a small example without entirely removing context. Feels like "foobar"-examples are just harder to read. Here I go: @RunWith(classOf[JUnitRunner]) class ParserBacktrackTest extends RegexParsers with Spec with ShouldMatchers { override def

Errors and failures in Scala Parser Combinators

这一生的挚爱 提交于 2019-12-07 03:01:08
问题 I would like to implement a parser for some defined language using Scala Parser Combinators. However, the software that will compile the language does not implements all the language's feature, so I would like to fail if these features are used. I tried to forge a small example below : object TestFail extends JavaTokenParsers { def test: Parser[String] = "hello" ~ "world" ^^ { case _ => ??? } | "hello" ~ ident ^^ { case "hello" ~ id => s"hi, $id" } } I.e., the parser succeeds on "hello" +