13
我写了一个解析器如下:Scala的解析器组合,大文件的问题
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)
}
如果我从(270MB)解析文件用的FileReader如下:
val index = parseAll(invertedIndex, new FileReader("path/to/file")).get
我得到一个Exception in thread "main" java.lang.StackOverflowError
(我也尝试包裹在BufferedReader
),但我可以通过先读文件到像这样一个String修复:
val input = io.Source.fromFile("path/to/file")
val str = input.mkString
input.close()
val index = parseAll(invertedIndex, str).get
这是为什么?有没有什么办法可以避免首先将它作为字符串读取,这似乎是一种浪费?
什么是你堆的电流的大小,以及你有多少大,使你的筹码,以避免StackOverflowException?堆栈需要多少才能使String版本溢出? (你可以通过启动如下设置你的堆栈为16MB:'scala -J-Xss16M') – DaoWen
我只是使用默认堆栈大小,但是当我将它设置为16M时,程序仍然在30分钟后运行... – Robert
This可能与Scala 2.9.2错误[SI-6520](https://issues.scala-lang.org/browse/SI-6520)有关。 –