parser-combinators

Is Scalas/Haskells parser combinators sufficient?

I'm wondering if Scalas/Haskells parser combinators are sufficient for parsing a programming language. More specifically the language MiniJava. I'm currently reading compiller construction and jflex and java cup is quite painful to work with so I'm wondering if I could/should use parser combinators instead. The MiniJava syntax is very sm...

EBNF to Scala parser combinator

I have the following EBNF that I want to parse: PostfixExp -> PrimaryExp ( "[" Exp "]" | . id "(" ExpList ")" | . length )* And this is what I got: def postfixExp: Parser[Expression] = ( primaryExp ~ rep( "[" ~ expression ~ "]" | "." ~ ident ~"...

Scala Parser Token Delimiter Problem

I'm trying to define a grammar for the commands below. object ParserWorkshop { def main(args: Array[String]) = { ChoiceParser("todo link todo to database") ChoiceParser("todo link todo to database deadline: next tuesday context: app.model") } } The second command should be tokenized as: action = todo message =...

Accessing Scala Parser regular expression match data

I wondering if it's possible to get the MatchData generated from the matching regular expression in the grammar below. object DateParser extends JavaTokenParsers { .... val dateLiteral = """(\d{4}[-/])?(\d\d[-/])?(\d\d)""".r ^^ { ... get MatchData } } One option of course is to perform the match again inside the ...

Return type of "|" in Scala's parser combinators

I was reading Bernie Pope's slides on "Parser combinators in Scala". He quotes the method signature type of the "alternative" combinator |: def | [U >: T](q: => Parser[U]): Parser[U] and asks, "Homework: why doesn’t | have this type instead?" def | [U](q: => Parser[U]): Parser[Either[T,U]] ...

How can I create a parser combinator in which line endings are significant?

I am creating a DSL, and using Scala's parser combinator library to parse the DSL. The DSL follows a simple, Ruby-like syntax. A source file can contain a series of blocks that look like this: create_model do at 0,0,0 end Line endings are significant in the DSL, as they are effectively used as statement terminators. I wrote a Scala...

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

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 ...

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

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 gra...

what is wrong: "value Parsers is not a member of package scala.util.parsing.combinator"?

I've got the above odd error message that I don't understand "value Parsers is not a member of package scala.util.parsing.combinator". I'm trying to learn Parser combinators by writing a C parser step by step. I started at token, so I have the classes: import util.parsing.combinator.JavaTokenParsers object CeeParser extends JavaTokenPa...

Lexing newlines in scala StdLexical?

I'm trying to lex (then parse) a C like language. In C there are preprocessor directives where line breaks are significant, then the actual code where they are just whitespace. One way of doing this would be do a two pass process like early C compilers - have a separate preprocessor for the # directives, then lex the output of that. Ho...

Scala: How to combine parser combinators from different objects

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.com...

Parser combinators info

I am using parsing combinators in scala If I have recursive parser: val uninterestingthings = ".".r val parser = "(?ui)(regexvalue)".r | (uninterestingthings~>parser) How can I check how many characters of input my parser consumed? ...

Advanced control of recursive parser in scala

val uninterestingthings = ".".r val parser = "(?ui)(regexvalue)".r | (uninterestingthings~>parser) This recursive parser will try to parse "(?ui)(regexvalue)".r until the end of input. Is in scala a way to prohibit parsing when some defined number of characters were consumed by "uninterestingthings" ? UPD: I have one poor solution: ...

Scala parser combinators: how to parse "if(x)" if x can contain a ")"

I'm trying to get this to work: def emptyCond: Parser[Cond] = ("if" ~ "(") ~> regularStr <~ ")" ^^ { case s => Cond("",Nil,Nil) } where regularStr is defined to accept a number of things, including ")". Of course, I want this to be an acceptable input: if(foo()). But for any if(x) it is taking the ")" as part of the regularStr and so ...

How to further improve error messages in Scala parser-combinator based parsers?

I've coded a parser based on Scala parser combinators: class SxmlParser extends RegexParsers with ImplicitConversions with PackratParsers { [...] lazy val document: PackratParser[AstNodeDocument] = ((procinst | element | comment | cdata | whitespace | text)*) ^^ { AstNodeDocument(_) } [...] } obje...

Packrat parsing HTTP

Hello could somebody give me a start on how to parse the HTTP-protocol with scala 2.8 packrat-parsing? I need to parse attached examplary HTTP Response into ResponseStatusCode:Int Headers:List[(String,String)] Body: String, Array[Byte], CharBuffer or whatever Short examplary usage of a Packrat-Parser very much appreciated. Thanks! ...

What lexer to build a lexer/parser in Scala

Hi there, I'm currently looking for a lexer/parser that generate Scala code from a BNF grammar (a ocamlyacc file with precedence and associativity) and I'm quite confused to find.. almost nothing: For parsing, I found scala-bison (that I have a lot of trouble to deal with). All the other tools are just Java parser imported into Scala (l...

Filtering tokens from Scala Parser Combinators

How do I filter the sequence of tokens coming from my Lexer to my Parser when using Scala parser combinators? Let me explain - suppose I have the fairly standard pattern of a Lexer (extending StdLexical) and a Parser (extending StdTokenParsers). The lexer turns a sequence of characters to a sequence of tokens, then the parser turns the ...

Scala Parser Combinators tricks for recursive bnf?

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.del...

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

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...