Is there a standard way to store abstract syntax tree files?

I'm looking for a way to dump abstract syntax trees into files so that the code can be parsed using the compiler and then saved in a language and compiler independent language. However, I could not find a well-known way to do this. Is there such a way?

+4
source share
1 answer

There are no standards for storing AST, or, more importantly, from your point of view, exchanging them between tools. The reason is that ASTs are grammar dependent (which vary: C has a lot, depending on the particular compiler and version) and parsing technology.

There were many attempts to define universal forms of AST in several languages, but none of them worked; semantics of operators are changing a lot. (Consider only the “+”: what does this mean? In Fortran, you can add arrays, in Java, you can “add” strings).

However, specific ASTs can be easily recorded. A simple means is to use some kind of notation, in which the node is identified along with its recursive children, using some kind of enclosed "parentheses".

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

People also used XML for this, but it is rather cumbersome. Here you can see the XML output example .

+5
source

Source: https://habr.com/ru/post/1447419/


All Articles