Quantcast

Help with my first example to parse sentences?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help with my first example to parse sentences?

philip andrew
Hi!

I'm just trying to get started and I want to parse the following string into words, however its not working. What I get back is List(WordNode(hello there this is a test)).
I want a list of WordNode's, can you tell me what I am doing wrong?

Here is the code.

      val result = parser.parseBee("hello there this is a test")

object BeeParser extends Parser {
  sealed abstract class AstNode
  case class WordNode(value: String) extends AstNode
  case class SentenceNode(particles: List[WordNode]) extends AstNode
}

class BeeParser extends Parser {

  def Bee: Rule1[List[WordNode]] = rule { Sentence ~ EOI }

  def Sentence: Rule1[List[WordNode]] = rule { zeroOrMore(Word, separator = " ") }

  def Word: Rule1[WordNode] = rule { zeroOrMore(Character) ~> WordNode }

  def Character: Rule0 = rule { ANY }

  def parseBee(code: String) = {
    val parsingResult = ReportingParseRunner(Bee).run(code)
    parsingResult.result match {
      case Some(astRoot) => astRoot
      case None => throw new ParsingException("Invalid Bee Language source:\n" +
        ErrorUtils.printParseErrors(parsingResult))
    }
  }

}


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help with my first example to parse sentences?

philip andrew
I solved it myself ! http://pastebin.com/kerD2pc0
Loading...