Scala packrat parser
I have some questions about Packrat parser combina开发者_StackOverflow社区tor presented in Scala 2.8.
Unfortunatelly I wasn't able to find any tutorials of how to use this new feature except of Scaladoc PackratParsers trait description, which is rather short. Could it be possible to receive an example of using it?
Actually, I have no experiance in Scala. And the first problem I have found when trying to use packrat parser is implementing PackratReader class. Scaladoc said that a programmer should create this object manually like new PackratReader(new lexical.Scanner("input")). Therefore I wrote the following:
package proj import scala.util.parsing.combinator._ class MyParser extends JavaTokenParsers with PackratParsers { lazy val expr: PackratParser[Any] = ident ~ "+" ~ ident } object TestApplication { def main(arguments: Array[String]) { val myParser = new MyParser println(myParser.parse( myParser.expr, new PackratReader(new lexical.Scanner("x+y")) )) } }
And the compiler gave the error:
error: not found: type PackratReader new PackratReader(new lexical.Scanner("x+y"))
Therefore my second question is about how to implement(or instantiate if possible) PackratReader. Besides I'm also interested how to instantiate trait's inner classes like PackratReader mentiond in the prevous question?
My last question is how to use together Packrat parser and regular Scala's parser combinators. Scaladoс said: "They can be free mixed with regular Parsers in a single grammar". Could you also explain how regular parsing rules which don't provide memoization will infuence packrat rules supporting it?
Thanks.
class PackratReader
is nested within trait PackratParsers
. You'll need to instantiate it in the context of a specific instance of the PackratParsers
trait. I'd pass the "plain" Reader
to the MyParser
constructor so the PackratReader
can be instantiated there.
精彩评论