6

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?

drakide
  • 1,757
  • 2
  • 15
  • 23
  • Generally ASTs are transient and only used to pass parsed information from the parser to the compiler. At that point, you then generally save bytecode. – Ayush Nov 21 '12 at 07:55
  • @xbonez: I am aware of that fact. What I am searching for is a way to store the output of the parser and use it with some other compiler. – drakide Nov 21 '12 at 08:00
  • 1
    Yes, indeed. It's called LISP. – Aadit M Shah Apr 14 '13 at 07:42

1 Answers1

7

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.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341