解释器与代码生成器Xtext [英] Interpreter vs. Code Generator Xtext

查看:116
本文介绍了解释器与代码生成器Xtext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Xtext编写的DSL。我想要执行的是DSL,以使其表现更好。

I've a DSL written using Xtext. What I want is to execute that DSL to perform something good out of it.

我编写了myDslGenerator类,该类在xtend中实现了IGenerator接口,以生成Java代码,并且运行良好。

I wrote myDslGenerator class implementing the interface IGenerator in xtend to generate java code and it's working fine.

我有两个问题;


  1. 解释器和代码生成器?
    都不都是用来执行DSL的吗?

  2. 如何编写解释器?有分步教程链接吗?我发现许多教程使用xtend生成代码,但找不到任何编写解释器的方法。

谢谢,

Salman

推荐答案

基本上,解释器和代码生成器的工作原理完全不同。代码生成器就像一个编译器:它们以另一种语言创建DSL的可执行代码。另一方面,解释器用于遍历您的DSL并在您自己的环境中执行它们。这意味着生成的代码不必(但当然可以)取决于您的DSL,可以更快/更优化。虽然解释器需要理解您的语言的构造,但是可以在开发IDE中执行,而无需运行其他应用程序。

Basically, interpreters and code generators work really differently. Code generators are like a compiler: they create executable code of your DSL in another language; on the other hand, interpreters are used to traverse your DSL and execute them in your own environment. This means, the generated code does not have to (but of course it can) depend on your DSL, can be faster/more optimized; while interpreters need to understand the constructs of your language, but can be executed in your development IDE, not required to run an additional application.

AFAIK Xtext不支持编写解释器,它有些超出范围(并非完全-对于Xbase表达式,有一个XbaseInterpreter实例,可以重用-只要您正确设置其类路径即可),因为它们与语言非常相关。

AFAIK Xtext does not support writing interpreters, its somewhat out of their scope (not entirely - for Xbase expressions there is an XbaseInterpreter instance, that can be reused - provided you set its classpath correctly), as they are extremely language-specific.

我也不知道任何有关解释Xtext DSL的分步教程(甚至不包括XbaseInterpreter),但是它基本上可以归结为AST的遍历,并且遍历一个节点时,相应的语句是动态执行的。为了使这种遍历正常工作,解释器必须维护变量和其他引用的(可能是分层的)上下文。

I also don't know any step-by-step tutorial about interpreting Xtext DSLs (not even for the XbaseInterpreter), but it basically boils down to a traversal of the AST, and as a node is traversed, the corresponding statement is executed dynamically. For this traversal to work, as expected, the interpreter has to maintain a (possibly hierarchic) context of variables and other references.

这篇关于解释器与代码生成器Xtext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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