簡體   English   中英

Scala解析器和組合器:java.lang.RuntimeException:字符串匹配正則表達式“ \\ z”

[英]Scala Parser and combinators: java.lang.RuntimeException: string matching regex `\z' expected

作為我的碩士論文的一部分,我正在嘗試使用Scala的RegexParser解析動態認知邏輯的語法之后的一些文本。 但是我在簡單的邏輯連接上仍然遇到相同的錯誤。 我了解失敗的原因和原因,但不明白為什么它與最初的情況相匹配。

我的代碼(經過仔細研究以找出問題所在):

import scala.util.parsing.combinator._

class Formula() {
      def and(q:Formula) = Conjunction(this, q) // ∧
}

abstract class Literal extends Formula
abstract class Constant extends Formula

case class Atom(symbol:String) extends Literal 
case class NotAtom(p:Atom) extends Literal

case class Conjunction(p:Formula, q:Formula) extends Formula

class mapParser extends RegexParsers {
    val conjOp = "&"
    val negOp = "~"

    val listseparator = ","

    val leftparen = "("
    val rightparen = ")"

    def id:Parser[String] = "[a-z_]+".r // fluents are never capitalized. but may have underscore.
    def litargs: Parser[String] = repsep("[a-zA-Z]+".r,listseparator) ^^ {case list => "(" + list.toString.stripPrefix("List") + ")"}

    def atom: Parser[Atom] = id~leftparen~litargs~rightparen ^^ {case head~_~tail~_ => Atom(head+tail)}
    def negAtom: Parser[NotAtom] = negOp~>atom ^^ (NotAtom(_))
    def literal: Parser[Literal] = negAtom | atom 

    def and: Parser[Formula] = formula~conjOp~formula ^^ {case p1~_~p2 => Conjunction(p1,p2)}  

    def formula: Parser[Formula] = literal | and
};

object DomainParser extends mapParser {
  def test() =  {
    val domainDesc ="present(A) & ~present(B)";

    println("input: " + domainDesc)
    println("result: " + apply(domainDesc))
  }

  def apply(domainDesc: String) = parseAll(formula, domainDesc) match {
    case Success(result, _) => result
    case failure : NoSuccess => scala.sys.error(failure.msg)
  }
}

我從Java外部調用DomainParser.test()函數。 輸入是

present(A) & ~present(B)

應該產生:

Conjunction(Atom(present((A))),NotAtom(Atom(present((B)))))

但是給了我錯誤:

Exception in thread "main" java.lang.RuntimeException: string matching regex `\z' expected but `&' found
    at scala.sys.package$.error(package.scala:27)
    at mAp.DomainParser$.apply(DEL.scala:48)
    at mAp.DomainParser$.test(DEL.scala:43)
    at mAp.DomainParser.test(DEL.scala)
at ma.MA.main(MA.java:8)

此外,如果我直接調用“和”解析器而不是“公式”解析器,則它可以正常工作。 因此,問題似乎出在這一行:

def formula: Parser[Formula] = literal | and

因為它試圖將整行解析為單個文字。 然后,它可以正確地解析present(A),但不會失敗於“&”(不是文字解析器的一部分)並返回解析為“和”項,但會失敗,並帶有異常。

我不能因為...的熱愛而已。明白為什么它會嘗試完全匹配任何'\\ z'。 它沒有包含在我的語法中,即使是-也不應該失敗並嘗試將其解析為下一個術語,而不是異常退出嗎? 我認為存在一些我不知道的針對字符串結尾術語的內置功能,而認為存在某些顯而易見的東西讓我感到困惑。

迫切需要任何幫助,非常感謝,並在此先感謝您。

丹·特魯(Dan True)

我將為命題公式添加一個類似的解析器。 也許這可能對您有幫助。

'+'=頂/真

'-'=底/假

'!' =否定

'&'=連詞

'|' =析取

'>'=含義

'<'=等價

object FormulaParser extends StandardTokenParsers with PackratParsers {
  //Symbols for all connectives
  private val parseSymbols = List("(", ")", "+", "-", "!", "&", "|", ">", "<")
  lexical.delimiters ++= parseSymbols

  private lazy val formula: PackratParser[Formula] = implication | equivalence | conjunction | disjunction | term
  private lazy val formulaWithoutBrackets: PackratParser[Formula] = implication | equivalence | conjunction | disjunction | termWithoutBrackets

  private lazy val term: PackratParser[Formula] = top | bottom | variable | parens | negation
  private lazy val termWithoutBrackets = top | bottom | variable | negation

  private lazy val top: PackratParser[Formula] = "+" ^^^ { Top() }
  private lazy val bottom: PackratParser[Formula] = "-" ^^^ { Bottom() }
  private lazy val variable: PackratParser[Formula] = ident ^^ { Variable(_) }
  private lazy val parens: PackratParser[Formula] = "(" ~> formulaWithoutBrackets <~ ")"
  private lazy val negation: PackratParser[Formula] = "!" ~> term ^^ { Negation(_) }

  private lazy val conjunction: PackratParser[Formula] = term ~ "&" ~ term ~ rep("&" ~> term) ^^ {
    case p ~ "&" ~ q ~ conj => conj.foldLeft(Conjunction(p,q))((con, elem) => Conjunction(con, elem))
  }

  private lazy val disjunction: PackratParser[Formula] = term ~ "|" ~ term ~ rep("|" ~> term) ^^ {
    case p ~ "|" ~ q ~ disj => disj.foldLeft(Disjunction(p,q))((dis, elem) => Disjunction(dis, elem))
  }

  private lazy val implication: PackratParser[Formula] = (conjunction | disjunction | term) ~ ">" ~ (conjunction | disjunction | term) ^^ { case p ~ ">" ~ q => Implication(p, q) }

  private lazy val equivalence: PackratParser[Formula] = (conjunction | disjunction | term) ~ "<" ~ (conjunction | disjunction | term) ^^ { case p ~ "<" ~ q => Equivalence(p, q) }
}

這樣,您可以解析輸入: (p & q) | (!q > (r & s)) (p & q) | (!q > (r & s))

在這里,合取和析取也比蘊涵和等效具有更強的約束力。

p & q > r | s p & q > r | s將導致Implication(Conjunction(Variable(p), Variable(q)), Disjunction(Variable(r), Variable(s)))

好。 如果僅僅是由於左遞歸,我有一個類似的解析器,在這里我解決了。

您必須更改以下內容:

def and: Parser[Formula] = literal~conjOp~literal~rep(conjOp ~> literal) ^^ {
  case p ~ conjOp ~ q ~ conj => conj.foldLeft(Conjunction(p,q))(Conjunction(_, _))
}
def formula: Parser[Formula] = and | literal

由於最后只有通過Conjunction連接的Literals ,您可以重寫and按此進行操作。

代碼稍微復雜一點的示例:

input: p(A,B) & ~p(B) & p(C)
result: Conjunction(Conjunction(Atom(p(A,B)),NotAtom(Atom(p(B)))),Atom(p(C)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM