How do I feed input to a packrat parser?

304 views
Skip to first unread message

Cay Horstmann

unread,
Nov 5, 2011, 12:47:15 AM11/5/11
to scala-user
For the life of me, I can't figure out how to give input to a packrat parser.

  class OnesParser extends RegexParsers with PackratParsers {   
    def ones: PackratParser[Any] = ones ~ "1" | "1"

    def parse(input: String) = ...
  }

I tried a dozen things, such as

    def parse(input: String) = phrase(ones)(new PackratReader(new CharSequenceReader(input)))

and I got unilluminating exceptions for everything I tried.

This should be a simple thing, I am sure, but I can't seem to hit upon the magic incantation.

I'd appreciate any hints.

Thanks,

Cay


Pasturel

unread,
Nov 5, 2011, 5:57:41 AM11/5/11
to scala...@googlegroups.com
Le 05/11/2011 05:47, Cay Horstmann a �crit :
did you try with :
def ones: PackratParser[Any] = "1" | ones ~"1"
?

Tony Sloane

unread,
Nov 5, 2011, 7:15:01 AM11/5/11
to Cay Horstmann, scala-user
I usually use the parse or parseAll methods of RegexParsers.  There are variants that take
Readers (e.g., if you are reading from a file) and CharSequences (if your input comes from
a string).

cheers,
Tony


Cay Horstmann

unread,
Nov 5, 2011, 10:49:46 AM11/5/11
to Pasturel, scala...@googlegroups.com


On Sat, Nov 5, 2011 at 2:57 AM, Pasturel <jean-loui...@orange.fr> wrote:

Well, yes, but I thought that the Scala packrat parser can deal with left recursion. That's at least the impression I got from Jonnalagedda's report and from the scaladoc:

"Packrat Parsing is a technique for implementing backtracking, recursive-descent parsers, with the advantage that it guarantees unlimited lookahead and a linear parse time. Using this technique, left recursive grammars can also be accepted."

So, why do I still get a stack overflow?

Thanks,

Cay

Cay Horstmann

unread,
Nov 5, 2011, 10:53:35 AM11/5/11
to Tony Sloane, scala-user
Yes, but what do you do about turning the reader into a PackratReader? The scaladoc offer this guidance:

Programmers can create PackratReader objects either manually, as in production(new PackratReader(new lexical.Scanner("input"))), but the common way should be to rely on the combinator phrase to wrap a given input with a PackratReader if the input is not one itself.

Thanks,

Cay

Cay Horstmann

unread,
Nov 5, 2011, 11:22:49 AM11/5/11
to Pasturel, scala...@googlegroups.com
Never mind, I (or rather, Anonymous in SO @ http://stackoverflow.com/questions/4615588/backtracking-in-scala-parser-combinators) figured it out. You have to use lazy val, not def:

lazy val ones: PackratParser[Any] = ones ~ "1" | "1"

Sanjay Dasgupta

unread,
Nov 6, 2011, 8:51:36 AM11/6/11
to scala-user
I created a short tutorial (http://vll.java.net/examples/how-input-
packrat.html) that demonstrates use of a visual parser-generator
(VisualLangLab) for rapid prototyping and testing a parser.

I hope this is useful,

- Sanjay

On Nov 5, 8:22 pm, Cay Horstmann <cay.horstm...@gmail.com> wrote:
> On Sat, Nov 5, 2011 at 7:49 AM, Cay Horstmann <cay.horstm...@gmail.com>wrote:
> Never mind, I (or rather, Anonymous in SO @http://stackoverflow.com/questions/4615588/backtracking-in-scala-pars...)

Sanjay Dasgupta

unread,
Nov 6, 2011, 9:00:30 AM11/6/11
to scala-user
The link in my previous post was split across two lines, so am
providing the link again:

http://vll.java.net/examples/how-input-packrat.html

Thanks,

- Sanjay
Reply all
Reply to author
Forward
0 new messages