Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert simple Antlr grammar to Xtext

Tags:

antlr

xtext

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?

like image 644
Adam Schmideg Avatar asked Nov 26 '11 16:11

Adam Schmideg


1 Answers

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.

like image 160
Sebastian Zarnekow Avatar answered Oct 24 '22 11:10

Sebastian Zarnekow