将简单的Antlr语法转换为Xtext [英] Convert simple Antlr grammar to Xtext

查看:77
本文介绍了将简单的Antlr语法转换为Xtext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个非常简单的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中有一些分配,这些分配将成为这些类中的获取器和设置器.但是,这些分配不应用于每个规则调用,因为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆