问题描述
我想将一个非常简单的 Antlr 语法转换为 Xtext,所以没有 句法谓词,没有Xtext 不提供的Antlr 的花哨功能.考虑这个语法
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'+;
及其对应的 Xtext
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 在幕后使用 Antlr,但两种格式并不完全相同.我必须修改很多烦人(部分可以理解)的东西,包括:
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:
- 使用
terminal
关键字前缀终端 - 包括
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
使终端工作 - 向顶级规则添加功能,例如
foo: dummy=number+
- 请记住,即使不区分大小写,规则和终端名称也必须是唯一的.
- (可选)将规则名称的首字母大写以遵循 Java 约定.
- 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?
推荐答案
基本上不可能自动进行这种转换,因为 Antlr 语法缺少 Xtext 语法中所需的信息.Xtext 中的规则名称将用于从它们创建类.Xtext 中的赋值将成为这些类中的 getter 和 setter.但是,这些分配不应该用于每个规则调用,因为 Xtext 中有特殊模式可以减少生成的 AST 中的噪音.像这样的东西很难自动进行这种转换.但是,通常直接将 Antlr 语法复制到 Xtext 编辑器并手动修复问题.
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.
这篇关于将简单的 Antlr 语法转换为 Xtext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!