Diolor
Diolor

Reputation: 13450

Create language with Antlr

Let's say I have created a new Awesome language which is like java in syntax but instead of package it uses pack. So let's say that this new language is a template language for Java.

I have created the awesome.g4 and compiled it, so Antlr gave me the BaseListener,Listener,Lexer & Parser.

I want to create a program with this new Awesome language as a source code, so I can think two options:

  1. Convert it first into Java and then compile the java into binary (I hope I'm correct, I mean the .class files)

  2. Compile the Awesome program directly into binary.

The only relevant repo/code I have found is antlr4-csharp.

Since I want to save humanity and code in my new Awesome language from now on, is it possible to create executable code given that language as the source code, how?

Upvotes: 4

Views: 1235

Answers (1)

ahoffer
ahoffer

Reputation: 6526

Damn! I want you to invent that Awesome language that will save the world. I like to see passionate language designers. Groovy did something like what you are talking about:

ANTLR takes the Groovy grammar file "Groovy.g" to create the Groovy parser. When the parser is fed with the source code of a Groovy script, it produces the AST (= Abstract Syntax Tree) that represents that code as a run-time structure. Byte Code Generation From the AST, it is possible to create Java Byte Code: either for making it persistent as .class files or for making it directly available as Class objects through the GroovyClassLoader. This ClassGeneration is done with the help of objectweb's ASM tool. (The ASM name does not mean anything: it is just a reference to the "asm" keyword in C, which allows some functions to be implemented in assembly language.) ASM provides a Java API to construct or modify Byte Code on a given AST.

ANTLR only gets you so far. There are a couple of strategies to get past that. (1) Translate Awesome language to C and then compile the C code. (2) Compile Awesome language to Java byte codes and run it on the JVM. (3) Compile it to LLVM byte codes.

For approach number (2), check out: http://groovy.codehaus.org/Groovy+Backstage.

Upvotes: 4

Related Questions