是什么导致 F# 序列重新枚举?
我有一个解析器,它实际上是一组对词法分析器标记序列进行操作的递归函数。
我遇到的问题是,该序列似乎在递归函数调用时从头开始重新启动。 给定函数 Parse
的以下骨架定义,
let restricted = Seq.take_while token_search tokens
let compiled_nodes = Seq.fold (fun list (next: Lexer.Token) -> list @ parse_token this restricted next) [] restricted
函数 parse_token
可能会导致调用 Parse
。
然而,当这种情况发生时,参数tokens
最终会定位在序列的开头。
关于如何将序列保持在需要的位置有什么想法吗?
蒂亚
I have a parser that is effectively a set of recursive functions operating on a sequence of lexer tokens.
The problem I'm running into is that the sequence seems to restart from the beginning on recursive function calls. Given the following skeleton definition for function Parse
let restricted = Seq.take_while token_search tokens
let compiled_nodes = Seq.fold (fun list (next: Lexer.Token) -> list @ parse_token this restricted next) [] restricted
The function parse_token
may result in a call into Parse
.
However, when that happens, the parameter tokens
ends up positioned at the beginning of the sequence.
Any ideas on how to keep the sequence positioned where it needs to be?
tia
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能需要发布一个稍大的片段,因为我不太明白您的意思。
也就是说,序列 (IEnumerable) 就是一个序列,每次使用 for (foreach) 或 Seq.Whatever 时,它都会“重新迭代”该序列。 我不清楚您想要做什么,以及您期望发生什么,但是对于解析,将“令牌”表示为序列可能是“错误的”,因为您通常将令牌划分为消耗/提交区域和前瞻区域。
另请注意,您通常不希望“迭代序列”产生副作用。
I think you may need to post a slightly bigger snippet, as I am not quite following you.
That said, a sequence (IEnumerable) is just that - a sequence, and each time you for (foreach) or Seq.Whatever over it, it will 're-iterate' the sequence. I am unclear what you want to do, and what you expect to happen, but for a parse, representing 'tokens' as a sequence may be 'wrong', as you typically partition tokens into a consumed/committed region and a lookahead region.
Note also that you typically do not want 'iterating over a sequence' to have side-effects.