English 中文(简体)
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 = link todo to database
properties = [deadline: next tuesday, context: app.model]

When I run this input on the grammar defined below, I receive the following error message:

[1.27] parsed: Command(todo,link todo to database,List())
[1.36] failure: string matching regex `z  expected but `:  found

todo link todo to database deadline: next tuesday context: app.model
                                   ^

As far as I can see it fails because the pattern for matching the words of the message is nearly identical to the pattern for the key of the property key:value pair, so the parser cannot tell where the message ends and the property starts. I can solve this by insisting that start token be used for each property like so:

todo link todo to database :deadline: next tuesday :context: app.model

But i would prefer to keep the command as close natural language as possible. I have two questions:

What does the error message actually mean? And how would I modify the existing grammar to work for the given input strings?

import scala.util.parsing.combinator._

case class Command(action: String, message: String, properties: List[Property])
case class Property(name: String, value: String)

object ChoiceParser extends JavaTokenParsers {
    def apply(input: String) = println(parseAll(command, input))

    def command = action~message~properties ^^ {case a~m~p => new Command(a, m, p)}

    def action = ident

    def message = """[wds.]+""".r

    def properties = rep(property)

    def property = propertyName~":"~propertyValue ^^ {
        case n~":"~v => new Property(n, v)
    }

    def propertyName: Parser[String] = ident

    def propertyValue: Parser[String] = """[wds.]+""".r
}
最佳回答

It is really simple. When you use ~, you have to understand that there s no backtracking on individual parsers which have completed succesfully.

So, for instance, message got everything up to before the colon, as all of that is an acceptable pattern. Next, properties is a rep of property, which requires propertyName, but it only finds the colon (the first char not gobbled by message). So propertyName fails, and property fails. Now, properties, as mentioned, is a rep, so it finishes succesfully with 0 repetitions, which then makes command finish succesfully.

So, back to parseAll. The command parser returned succesfully, having consumed everything before the colon. It then asks the question: are we at the end of the input (z)? No, because there is a colon right next. So, it expected end-of-input, but got a colon.

You ll have to change the regex so it won t consume the last identifier before a colon. For example:

def message = """[wds.]+(?![:w])""".r

By the way, when you use def you force the expression to be reevaluated. In other words, each of these defs create a parser every time each one is called. The regular expressions are instantiated every time the parsers they belong to are processed. If you change everything to val, you ll get much better performance.

Remember, these things define the parser, they do not run it. It is parseAll which runs a parser.

问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签