Scala 解析器组合器和 Reader 无限循环
我对 Scala 解析器组合器有点困惑。 我正在使用 Reader 的自定义实现来直接读取标记列表:
private class Token_Reader(tokens: List[Token], val pos: Token_Pos) extends Reader
{
def first = if(atEnd) null else tokens.head
def rest = if(atEnd) this else new Token_Reader(tokens.tail, new Token_Pos(pos.p + 1))
def atEnd = tokens.isEmpty
}
令我困惑的是 atEnd
似乎被实际解析器完全忽略,导致使用时出现无限循环/无限递归*
/代表
。
I am a little confused by the Scala parser combinators.
I'm using a custom implementation of Reader to directly read a list of tokens:
private class Token_Reader(tokens: List[Token], val pos: Token_Pos) extends Reader
{
def first = if(atEnd) null else tokens.head
def rest = if(atEnd) this else new Token_Reader(tokens.tail, new Token_Pos(pos.p + 1))
def atEnd = tokens.isEmpty
}
What puzzles me is that atEnd
seems to be completely ignored by the actual parsers, resulting in an infinite loop / infinite recursion when using *
/rep
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道它会解决这个问题,但是在我在 Scala 源代码中看到的 Reader 实现中,
first
方法在末尾返回文件结束字符而不是 null。我相信避免空值通常是好的...例如,在
CharSequenceReader
中,它看起来像这个字符是在伴生对象中定义的:
I don't know that it will fix this issue, but in the Reader implementations I see in the Scala source, the
first
method returns an end of file character rather than null when at the end. And I believe it's generally good to avoid nulls...For example, in
CharSequenceReader
it looks likeAnd this character is defined in the companion object: