user22092
user22092

Reputation: 19

Can a Abstract Syntax Tree be Compile by multiple Compiler or Interpreter?

I know no two programming languages are perfectly match but I want to ask if I have a simple program like hello world and I run compilation translation phases Such as lex, parse then get the AST tree can I send it to another environment say Some c AST tree and interpret it with Java

Upvotes: 1

Views: 1835

Answers (3)

Ira Baxter
Ira Baxter

Reputation: 95306

Echoing Rici's response: Short answer, no.

This idea has been tried more than once. Usually it fails at least because you cannot define a single AST node for "add" that means one thing for all languages. Semantics just plain differ, and you have to be able to differentiate the meaning of the operator in the specific langauge context in which it is found. There are lots of other troubles, like agreeing on the details of the representation (tree? DAG? graph?) and how much information is carried (AST? Symbol tables? Control flow? ...)

People keep trying.

The Object Management Group has a specification of an Abstract Syntax Tree Model, which attempts to define universal ASTs. What the OMG discovered was, to make this practical, alongside their nirvana-style "General AST Model" (ick, "GASTM"), they needed to also have so-called "Specific AST Models" ("SASTM"), e.g,. ASTs that are specific to the language, nay, even a specific parser for that language, in order to be able to interpret the meaning of the operators and the operands accurately, as produced by that parser.

[I build a tool that handles multiple languages at the same time. It resolves the issue of the meaning of a node by essentially tagging each node with both the operator, e.g., "+", and the "domain" (notational system) in which the operator should be interpreted. In effect, this is the same as the SASTM solution. We dont believe in the GASTM and so don't bother with it.].

Upvotes: 0

xmojmr
xmojmr

Reputation: 8145

I'm not aware of any standardized AST representation formats which would enable such sharing (assuming we're talking about languages with similar semantics), but for instance in the Clang+LLVM architecture it seems that the AST output can be fed into multiple code generators (compilers).

As far as if there's an universal Java any-language interpreter reading AST I guess such thing does not exist and I doubt if it would be even possible to build it as the meaning of words in different programming languages is different.

EDIT 2015-03-30 after clarifying comments

Let's say I serialize the AST into a stream of bytes, send it over a socket, and deserialize it back into a tree of objects in a program written in another language. Using JSON, YAML, XML which are simple, fairly standard languages for serializing and deserializing arbitrary data, Then find parsers for them in the desired language. I think it is technically possible

Having a concrete simple subset of a concrete programming language, let's say a concrete procedural language, e.g. Tiny C, you can on one computer built it's parse trees and send them to another computer for "interpreting". Google query ast intermediate representation can give you some hints like http://icps.u-strasbg.fr/~pop/gcc-ast.html or http://lambda-the-ultimate.org/node/716, but it's different problem then your original any language with AST and universal interpreter in Java

I'm working on an experiment

asm.js is a modern version of "parse program in a language on one machine and send it to another machine for interpreting" problem. Where the another machine is any modern web browser and the serialization format is subset of JavaScript. With several billions of web browser over the planet experiments using this can be both commercially beneficial and useful as this project welcomes some further support or research from guys like you (?)

See also:

Upvotes: 2

rici
rici

Reputation: 241671

The short answer: No.

The longer version:

If you had two different language implementations which documented and exported their AST interfaces, and the two interfaces were sufficiently similar that you could translate between them, then you could compile to an AST and then try to pass the AST to one of those implementations.

I can only speak hypothetically here, because it is pretty uncommon for language implementations to include a externally-accessible AST interface. (One exception is Python, which allows you to compile to an AST, create or modify ASTs, and then compile from an AST. Here, "compile" means "compile to VM code". See the Python docs for more information.)

In particular, I don't know of a Java implementation which that. Both GCC and clang can output something resembling an AST, but neither of them accept one, and the output might not be sufficiently complete to define all aspects of the translation units.

Upvotes: 3

Related Questions