我已经编写了以下解析器:
class LogParser extends JavaTokenParsers {
def invertedIndex: Parser[Array[Array[(Int, Int)]]] = {
num ~> num ~> num ~> rep(postingsList) ^^ {
_.toArray
}
}
def postingsList: Parser[Array[(Int, Int)]] = {
num ~> rep(entry) ^^ {
_.toArray
}
}
def entry = {
num ~ "," ~ num ^^ {
case docID ~ "," ~ count => (docID.toInt, count.toInt)
}
}
def num = wholeNumber ^^ (_.toInt)
}
如果我通过FileReader以如下方式从一个大小为270MB的文件中解析:
val index = parseAll(invertedIndex, new FileReader("path/to/file")).get
我收到了一个Exception in thread "main" java.lang.StackOverflowError
的错误(我也尝试过使用BufferedReader
),但我可以通过先将文件读入字符串中来解决:
val input = io.Source.fromFile("path/to/file")
val str = input.mkString
input.close()
val index = parseAll(invertedIndex, str).get
为什么会这样?有没有办法避免先将其读入为字符串,这似乎很浪费?
scala -J-Xss16M
) - DaoWenPagedSeq
的一个 bug,所以最好将其读入字符串中。 - DaoWen