10

我正在尝试为以下命令定义语法。

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")
    }
}

第二个命令应标记为:

action = todo
message = link todo to database
properties = [deadline: next tuesday, context: app.model]

当我在下面定义的语法上运行此输入时,我收到以下错误消息:

[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
                                   ^

据我所见,它失败了,因为匹配消息单词的模式与属性键:值对的键模式几乎相同,因此解析器无法判断消息在哪里结束以及属性从哪里开始。我可以通过坚持为每个属性使用开始令牌来解决这个问题,如下所示:

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

但我更愿意让命令尽可能接近自然语言。我有两个问题:

错误消息的实际含义是什么?我将如何修改现有语法以适用于给定的输入字符串?

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 = """[\w\d\s\.]+""".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] = """[\w\d\s\.]+""".r
}
4

1 回答 1

22

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.

所以,回到parseAll. command解析器成功返回,消耗了冒号之前的所有内容。然后它会问一个问题:我们是否在输入的末尾(\z)?不,因为旁边有一个冒号。因此,它期望输入结束,但得到一个冒号。

您必须更改正则表达式,以便它不会使用冒号之前的最后一个标识符。例如:

def message = """[\w\d\s\.]+(?![:\w])""".r

顺便说一句,当您使用时,您def会强制重新评估表达式。换句话说,这些 def 中的每一个在每次被调用时都会创建一个解析器。每次处理它们所属的解析器时,都会实例化正则表达式。如果您将所有内容更改为val,您将获得更好的性能。

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

于 2009-11-25T20:10:31.853 回答