是否有任何标准方法来存储抽象语法树文件?(Is there any standard way to store abstract syntax trees files?)

编程入门 行业动态 更新时间:2024-10-17 13:35:34
是否有任何标准方法来存储抽象语法树文件?(Is there any standard way to store abstract syntax trees files?)

我正在寻找一种方法将抽象语法树“转储”到文件中,以便可以使用编译器解析代码,然后以独立于语言和编译器的方式存储。 然而,我无法找到任何广泛认可的方式来做到这一点。 这样的方式存在吗?

I am searching for a way to "dump" abstract syntax trees into files so that code can be parsed with a compiler and then stored in a language- and compiler independent way. Yet I was unable to find any widely recognized way for doing this. Does such a way exist?

最满意答案

没有存储AST的标准,或者更重要的是从您的角度来看,在工具之间共享它们。 原因是AST依赖于语法(它有所不同; C有很多“取决于具体的编译器和版本)和解析技术。

已经有很多尝试在多种语言中定义通用AST表单,但它们都没有真正起作用; 运营商的语义变化太大。 (仅考虑“+”:它的真正含义是什么?在Fortran中,您可以添加数组,在Java中,您可以“添加”字符串)。

但是,人们可以很容易地写出特定的AST。 一种简单的方法是使用某种表示法,其中使用某种嵌套的“括号”来识别节点及其递归子节点。

Lisp S表达式是执行此操作的常用方法。 您可以看到我们的工具生成的S表达式样式的示例 。

人们也使用XML,但它非常笨重。 您可以在此处查看XML输出示例 。

There are no standards for storing ASTs, or more importantly from your point of view, sharing them among tools. The reason is that ASTs are dependent on grammars (which vary; C has "many" depending on which specific compiler and version) and parsing technology.

There have been lots of attempts to define universal AST forms across multiple languages but none of them have really worked; the semantics of the operators varies too much. (Consider just "+": what does it really mean? In Fortran, you can add arrays, in Java, you can "add" strings).

However, one can write out specific ASTs rather easily. A simple means is to use some kind of notation in which a node is identified along with its recursive children using some kind of nested "parentheses".

Lisp S-expressions are a common way to do this. You can see an example of the S-expression style generated by our tools.

People have used XML for this, too, but it is pretty bulky. You can see an XML output example here.

更多推荐

本文发布于:2023-08-07 11:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法来   抽象   语法   有任何   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!