Convert simple Antlr grammar to Xtext

元气小坏坏 提交于 2019-12-05 16:56:15

问题


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:


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.



来源:https://stackoverflow.com/questions/8279790/convert-simple-antlr-grammar-to-xtext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!