Convert simple Antlr grammar to Xtext
Asked Answered
U

1

10

I want to convert a very simple Antlr grammar to Xtext, so no syntactic predicates, no fancy features of Antlr not provided by Xtext. Consider this grammar

grammar simple; // Antlr3

foo: number+;
number: NUMBER;
NUMBER: '0'..'9'+;

and its Xtext counterpart

grammar Simple; // Xtext
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate Simple "http://www.example.org/Simple"

Foo: dummy=Number+;
Number: NUMBER_TOKEN;
terminal NUMBER_TOKEN: '0'..'9'+;

Xtext uses Antlr behind the scenes, but the two format are not exactly the same. There are quite a few annoying (and partly understandable) things I have to modify, including:

  • Prefix terminals with the terminal keyword
  • Include import "http://www.eclipse.org/emf/2002/Ecore" as ecore to make terminals work
  • Add a feature to the top-level rule, e.g. foo: dummy=number+
  • Keep in mind that rule and terminal names have to be unique even case-insensitive.
  • Optionally, capitalize the first letter of rule names to follow Java convention.

Is there a tool to make this conversion automatically at least for simple cases? If not, is there a more complete checklist of such required modifications?

Upsilon answered 26/11, 2011 at 16:22 Comment(5)
Don't know of any tool that does exactly what you want, but you could convert the grammar to XML and then run some XQuery or XSLT on the result to transform to the desired target format. The converter understands only the basic structure of an ANTLR grammar, but that's what you asked for.Fortification
@BartKiers and Gunther, I'm grateful to both of you for your help, but this conversation seems to get off-topic. Gunther, you may have an issue tracker for your converter (which I use, too, with much delight), so it can be continued over there.Upsilon
@AdamSchmideg, you're right, it wasn't entirely on-topic, but it started out with a comment that was on topic: so to be honest, I think you're a bit of a stickler. But fair enough, its your question, so I'll remove my noise. My apology. @Gunther, I tried to explain the use of the ~ char in this Q&A: #8285419Minister
@BartKiers, +1 thanks for your understanding, and well, I may deserve a stickler badge :)Upsilon
@AdamSchmideg Have you found any tool for achiving the same.Carmacarmack
A
4

It's basically not possible to do this conversion automatically since the Antlr grammar lacks information that is required in the Xtext grammar. The rule names in Xtext will be used to create classes from them. There are assignments in Xtext that will become getters and setters in those classes. However, these assignments should not be used for every rule call since there are special patterns in Xtext that allow to reduce the noise in the resulting AST. Stuff like that makes it hardly possible to do this transformation automatically. However, it's usually straight forward to copy the Antlr grammar into the Xtext editor and fix the issues manually.

Alberic answered 28/11, 2011 at 6:22 Comment(1)
Fixing the issue manually can mean a lot of work, because Xtext forces me to think about the AST while Antlr is happy with a sloppy, "just parse and don't care about the AST" approach.Upsilon

© 2022 - 2024 — McMap. All rights reserved.