ANTLR序列化

发布于 2024-09-15 09:26:53 字数 34 浏览 3 评论 0原文

使抽象语法树可序列化为 XML 文件的最佳策略是什么?

What is the best strategy of making Abstract Syntax Trees serializable to an XML File?

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

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

发布评论

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

评论(2

浅沫记忆 2024-09-22 09:26:53

此网页上的“ANTLR 树与自定义树”一章显示了一个示例antlr 语法的一部分,一些输入以及给定输入的 AST 的格式化输出。本章包含用于生成此格式化输出的 C++ 程序的链接。

它不是 xml,但非常接近,可以作为开始的良好基础。

Chapter "ANTLR Trees vs. Custom Trees" on this web page shows an example of a antlr grammar, some input and the formatted output of the AST for the given input. The chapter contains a link to a C++ program to produce this formatted output.

It is not xml but pretty close and could be a good basis to start with.

憧憬巴黎街头的黎明 2024-09-22 09:26:53

从根开始递归地遍历 AST。

当下降到 X 类型的节点时,打印开始标记:

 <X>

然后从左到右下降到子节点并打印其内容。
处理完所有子节点后,打印结束标记:

 </X>

在类型 L 的叶节点处,打印

 <L value="abc"/>

可能感兴趣的属性值。

完毕。

如果您跟踪递归嵌套,则可以打印出标记开始和结束
前导 recursion_depth 空格,后跟换行符。
然后您的 XML 将被很好地嵌套。

Walk the AST recursively starting at the root.

When descending to a node of type X, print an opening tag:

 <X>

then descend into children left-to-right and print their content.
After processing all the childen, print a closing tag:

 </X>

At a leaf node of type L, print

 <L value="abc"/>

with possibly attribute values of interest.

Done.

If you keep track of recursion nesting, you can print out the tag start and ends
with leading recursion_depth spaces, and follow with a newline.
Then your XML will be nicely nested.

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