10
votes

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?

1
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.Gunther
@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.Adam Schmideg
@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: stackoverflow.com/questions/8284919/…Bart Kiers
@BartKiers, +1 thanks for your understanding, and well, I may deserve a stickler badge :)Adam Schmideg
@AdamSchmideg Have you found any tool for achiving the same.Anoob C I

1 Answers

4
votes

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.