Unparser- & Lang Model Generator for ANTLR4 Grammars

124 views
Skip to first unread message

Michael Hoffer

unread,
Jan 5, 2018, 8:10:58 PM1/5/18
to antlr-discussion
Hi there,

for the last month I worked on an unparser and language model generator for ANTLR4. I didn't find any existing project for ANTLR4 that provides such an infrastructure (I know that there are projects like xtext that support unparsing for ANTLR3). Are there other similar frameworks and tools for ANTLR4?

My question is whether there is interest in the ANTLR community for such a project. If so, I'd do a public release of VMF-Text under an open source license in the near future.

Currently, I use the unparser for my experimental bidirectional visual editing tools (VRL-Studio, VWorkflows etc.). So far, I have only created editors and IDEs for subsets of Java and Groovy. But now I'd like to apply the bidirectional binding to other languages as well. That's why I needed something that could derive an unparser and language model from a grammar file.

What does VMF-Text do?

Basically, it gives you an API that corresponds to a given ANTLR4 grammar. It allows you to write "programs/text" in the specified grammar by using the generated API. 

ANTLR4 turns sequential text into a data structure. VMF-Text turns a data structure into text.

You can also parse text into an object graph, modify it via the generated API and unparse the result into a text file. This is particularly useful for transforming source code algorithmically.

A Very Basic Example:

ArrayLang.g4:

array:  '(' values+=DOUBLE (',' values+=DOUBLE)* ')';

For this grammar you get an API like this:

ArrayLangModel model = ArrayLangModel.newInstance();
model.setRoot(
    Array.newBuilder().
      withValues(Arrays.asList(1.0, 2.1, 100.7)
    ).build()
);

Now you can unparse the array via

String s = new ArrayLangModelUnparser().unparse(model);
System.out.println("Output: "+s);

The result looks like this:

Output: ( 1.0 , 2.1 , 100.7 )

This example isn't really exciting but shows the basics.

I did more complex experiments with MiniJava from Cambridge. I just grabed one of the many MiniJava.g4 grammars and was able to make it work with VMF-Text by adding labels to rule references.

Here is an example:

MiniJava.g4:

expression:
...
|   left=expression TIMES right=expression
# mulExpression
...

To programmatically create a MulExpression this API can be used:

// create a multiply expression
IntLitExpression left = IntLitExpression.newBuilder().withValue(20).build();
IntLitExpression right = IntLitExpression.newBuilder().withValue(30).build();
MulExpression mulExpression = MulExpression.newBuilder().
        withLeft(left).
        withRight(right).
build();


I will extend my tests to some of the grammars-v4 in the near future.

Anyone interested in collaborating?

Regards,
Michael

Reply all
Reply to author
Forward
0 new messages