为什么这组解析器组合器会溢出堆栈?

时间:2019-03-13 04:18:11

标签: scala stack-overflow parser-combinators ebnf

TL; DR

我的EBNF语法规范的解析器组合器使堆栈溢出。为什么?我该如何解决?

背景

我正在尝试通过scala库中的组合器为EBNF syntax定义解析器。实际上,该代码构建了语法的AST,但是我已经去除了这些位,并内联了一种实用程序方法,以便产生MVCE(如下所示)。

问题

运行时编写的代码会产生堆栈溢出(也在下面)。我无法理解的是,它似乎在解析的跳过空白部分溢出。如何解决此错误?如果无法解析EBNF语法,那真的很不幸,我打算为此开发一些工具。

MVCE

package org.benknoble.ebnf

import scala.util.parsing.combinator._

class EbnfParserSimple extends RegexParsers {

  def nonterminal: Parser[String] = """<[^>]+>""".r

  def goesTo: Parser[String] = """::="""

  def epsilon: Parser[String] = "ε"

  def terminal: Parser[String] = "\"[^\"]+\"".r

  def sequence: Parser[String] =
    exp ~ exp ^^ { case left ~ right => left + right }

  def alternation: Parser[String] =
    exp ~ "|" ~ exp ^^ { case left ~ _ ~ right => left + "|" + right }

  def opt: Parser[String] =
    "[" ~ exp ~ "]" ^^ { case lb ~ e ~ rb => lb + e + rb }

  def repetition: Parser[String] =
    "{" ~ exp ~ "}" ^^ { case lb ~ e ~ rb => lb + e + rb }

  def group: Parser[String] =
    "(" ~ exp ~ ")" ^^ { case lb ~ e ~ rb => lb + e + rb }

  def exp: Parser[String] =
    (epsilon
      | terminal
      | nonterminal
      | sequence
      | alternation
      | opt
      | repetition
      | group)

  def rule: Parser[String] = nonterminal ~ goesTo ~ exp ~ ";" ^^ {
    case nt ~ delim ~ e ~ semi => nt + delim + e + semi
  }

  def join[A](sep: String, list: Seq[A]): String = list match {
    case h :: t => h.toString() + t.foldLeft("")(_.toString() + sep + _.toString())
    case Nil => ""
  }

  def root: Parser[String] = phrase(
    rep(rule) ^^ {
      case rules =>
        val joined = join(" ;\n", rules)
        if (joined.isEmpty)
          joined
        else
          joined + " ;"
    }
  )

}

object Main extends App {
  val parser = new EbnfParserSimple()
  val grammar = """<A> ::= ["a"|ε]"c" ; """
// <B> ::= <A>"b" ;
// <C> ::= {<B>}"$" ;
// <D> ::= "a""b""d" ;
// <E> ::= ("a"|"b")"c" ;
// """
  val rule = parser.root
  println(parser.parse(rule, grammar))
}

错误跟踪

完整日志可以找到as a Gist

[info] Loading project definition from /Users/Knoble/loner/project
[info] Loading settings for project root from build.sbt ...
[info] Set current project to loner (in build file:/Users/Knoble/loner/)
[info] Set current project to ebnf (in build file:/Users/Knoble/loner/)
[info] Running org.benknoble.ebnf.Main 
[error] (run-main-0) java.lang.StackOverflowError
[error] java.lang.StackOverflowError
[error]     at java.util.regex.Pattern.compile(Pattern.java:1673)
[error]     at java.util.regex.Pattern.<init>(Pattern.java:1351)
[error]     at java.util.regex.Pattern.compile(Pattern.java:1028)
[error]     at scala.util.matching.Regex.<init>(Regex.scala:226)
[error]     at scala.collection.immutable.StringLike.r(StringLike.scala:284)
[error]     at scala.collection.immutable.StringLike.r$(StringLike.scala:284)
[error]     at scala.collection.immutable.StringOps.r(StringOps.scala:33)
[error]     at scala.collection.immutable.StringLike.r(StringLike.scala:273)
[error]     at scala.collection.immutable.StringLike.r$(StringLike.scala:273)
[error]     at scala.collection.immutable.StringOps.r(StringOps.scala:33)
[error]     at org.benknoble.ebnf.EbnfParserSimple.terminal(EbnfParser_strings.scala:13)
[error]     at org.benknoble.ebnf.EbnfParserSimple.$anonfun$exp$1(EbnfParser_strings.scala:32)
[error]     at scala.util.parsing.combinator.Parsers$Parser.p$lzycompute$1(Parsers.scala:253)
[error]     at scala.util.parsing.combinator.Parsers$Parser.p$4(Parsers.scala:253)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$2(Parsers.scala:254)
[error]     at scala.util.parsing.combinator.Parsers$Failure.append(Parsers.scala:202)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$1(Parsers.scala:254)
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$1(Parsers.scala:254)
[...]
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$1(Parsers.scala:254)
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$1(Parsers.scala:254)
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$1(Parsers.scala:254)
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$append$1(Parsers.scala:254)
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error]     at scala.util.parsing.combinator.Parsers$Parser.$anonfun$flatMap$1(Parsers.scala:239)
[error]     at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
[error] Nonzero exit code: 1
[error] (Compile / run) Nonzero exit code: 1
[error] Total time: 1 s, completed 12 mars 2019 21:11:15

1 个答案:

答案 0 :(得分:1)

我最终可以通过techniques found in this answer删除左递归来解决我的问题。在下面找到工作代码。

我必须仔细考虑转换:特别是alternation.+ ^^ { _.reduce(_ + _) }sequence.+ ^^ { _.reduce(_ + _) } -将这些转换回AST生成器可能并不容易(因为这些转换的构造函数只需要左右键即可) 。重复操作也让我有些困扰,但是没有提取辅助函数,这是唯一要做的事情。

package org.benknoble.ebnf

import scala.util.parsing.combinator._

class EbnfParserSimple extends RegexParsers {

  def epsilon: Parser[String] = "ε"

  def terminal: Parser[String] = "\"[^\"]+\"".r

  def nonterminal: Parser[String] = """<[^>]+>""".r

  def opt: Parser[String] =
    "[" ~ exp ~ "]" ^^ { case lb ~ e ~ rb => lb + e + rb }

  def repetition: Parser[String] =
    "{" ~ exp ~ "}" ^^ { case lb ~ e ~ rb => lb + e + rb }

  def group: Parser[String] =
    "(" ~ exp ~ ")" ^^ { case lb ~ e ~ rb => lb + e + rb }

  def alternation: Parser[String] =
    chainl1(epsilon
      | terminal
      | nonterminal
      | opt
      | repetition
      | group,
      "|" ^^^ { (lb: String, rb: String) => lb + "|" + rb })
  //   exp ~ "|" ~ exp ^^ { case left ~ _ ~ right => left + "|" + right }

  def sequence: Parser[String] =
    alternation.+ ^^ { _.reduce(_ + _) }
    // alternation ~ alternation ^^ { case lb ~ rb => lb + rb }
  //   exp ~ exp ^^ { case left ~ right => left + right }

  def exp: Parser[String] =
    sequence.+ ^^ { _.reduce(_ + _) }

  def goesTo: Parser[String] = """::="""

  def rule: Parser[String] = nonterminal ~ goesTo ~ exp ~ ";" ^^ {
    case nt ~ delim ~ e ~ semi => nt + delim + e + semi
  }

  def join[A](sep: String, list: Seq[A]): String = list match {
    case h :: t => h.toString() + t.foldLeft("")(_.toString() + sep + _.toString())
    case Nil => ""
  }

  def root: Parser[String] = phrase(
    rep(rule) ^^ {
      case rules =>
        val joined = join(" ;\n", rules)
        if (joined.isEmpty)
          joined
        else
          joined + " ;"
    }
  )

}

object Main extends App {
  val parser = new EbnfParserSimple()
  val grammar = """<A> ::= ["a"|ε]"c" ; """
// <B> ::= <A>"b" ;
// <C> ::= {<B>}"$" ;
// <D> ::= "a""b""d" ;
// <E> ::= ("a"|"b")"c" ;
// """
  val rule = parser.root
  println(parser.parse(rule, grammar))
}