跟踪编译器中 AST 节点的源位置 (ocaml)
我正在使用 ocamllex/yacc 在 ocaml 中编写编译器。一切进展顺利,但我遇到了设计问题。对于我创建的每个 AST 节点,最好能获得有关源代码中该节点的行/字符位置的信息。这对于稍后向用户提供错误消息很有用。
现在,我可以向我的节点添加某种元类型:
type node = Node1 of ... * meta | Node2 of ... * meta
但这似乎是多余的。稍后,当我验证完 AST 后,我必须
match n with
| NodeX(..., _) -> ...
在每个 match
中写入,这浪费了空间。
解决这个问题的最佳方法是什么?
I'm writing a compiler in ocaml, using ocamllex/yacc. Things are going well, but I've a design problem. For each AST node I create, it'd be good to have information about line/character position of that node in the source code. That would be useful for providing error messages to the user later.
Now, I can add some kind of meta type to my nodes:
type node = Node1 of ... * meta | Node2 of ... * meta
but that seems redundant. Later, when I'm done with verifying the AST, I'll have to write
match n with
| NodeX(..., _) -> ...
in every match
which is a waste of space.
What's the best way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题的通常方法是使用记录来保存元信息和节点表达式:
然后:
The usual way to solve this is to use a record to hold the meta-information and the node expression:
and then: