是什么导致 F# 序列重新枚举?

发布于 2024-07-14 00:39:46 字数 491 浏览 7 评论 0原文

我有一个解析器,它实际上是一组对词法分析器标记序列进行操作的递归函数。

我遇到的问题是,该序列似乎在递归函数调用时从头开始重新启动。 给定函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2024-07-21 00:39:46

我认为您可能需要发布一个稍大的片段,因为我不太明白您的意思。

也就是说,序列 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文