在 ocamllex 中返回多个标记

发布于 2024-09-14 04:11:07 字数 316 浏览 3 评论 0原文

有没有办法在 OCamlLex 中返回多个标记?

我正在尝试为基于缩进的语言编写一个词法分析器和解析器,并且我希望我的词法分析器在注意到缩进级别低于以前的水平时返回多个 DEDENT 标记。这将允许它在多个块结束时通知解析器。

通过遵循此方法,我将能够使用 INDENTDEDENT 作为 BEGINEND 的直接替代品>,因为这两个标记将由 INDENTDEDENT 标记隐含。

Is there any way to return multiple tokens in OCamlLex?

I'm trying to write a lexer and parser for an indentation based language, and I would like my lexer to return multiple DEDENT tokens when it notices that the indentation level is less than it previously was. This will allow it to notify the parser when multiple blocks have ended.

By following this method, I would be able to use INDENT and DEDENT as drop-in replacements for BEGIN and END, as these two tokens would be implied by the INDENT and DEDENT tokens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

向地狱狂奔 2024-09-21 04:11:07

返回令牌列表。如果解析器无法本机处理该问题(例如 ocamlyacc) - 只需在两者之间插入缓存:

let cache =
  let l = ref [] in
  fun lexbuf ->
    match !l with
    | x::xs -> l := xs; x
    | [] -> match Lexer.tokens lexbuf with
            | [] -> failwith "oops"
            | x::xs -> l := xs; x

或者您可以在完整文档上运行词法分析器,然后在完整令牌流上运行解析器。

顺便说一句,您看到 ocaml+twt 了吗?

Return the list of tokens. If the parser cannot natively handle that (say ocamlyacc) - just insert a cache in between :

let cache =
  let l = ref [] in
  fun lexbuf ->
    match !l with
    | x::xs -> l := xs; x
    | [] -> match Lexer.tokens lexbuf with
            | [] -> failwith "oops"
            | x::xs -> l := xs; x

Or you can run the lexer on the full document and then run the parser on the full token stream.

BTW did you see ocaml+twt?

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