Xtext Interpreter and Generator

I have a DSL written using Xtext. I want to execute DSL in order to accomplish something good out of this.

I wrote the myDslGenerator class that implements the xtend IGenerator interface to generate java code, and it works great.

I have two questions:

  • What is the difference between an interpreter and a code generator? Are they designed to run DSL?
  • How to write a translator? Any step-by-step link to the tutorial? I found many tutorials for creating code using xtend, but could not find it to write an interpreter.

Thanks,

Salman

+4
source share
1 answer

Basically, interpreters and code generators work differently. Code generators are like a compiler: they create the executable code of your DSL in another language; translators, on the other hand, are used to go through your DSL and execute them in your own environment. This means that the generated code should not (but, of course, it can) depend on your DSL, it can be faster / optimized; while translators need to understand the design of your language, but can be done in your development IDE without requiring an additional application to run.

AFAIK Xtext does not support written interpreters, it is somewhat beyond (incompletely) - for Xbase expressions there is an instance of XbaseInterpreter that can be reused - if you set its class path correctly), since they are extremely language specific.

I also don’t know the step-by-step guide for interpreting the Xtext DSL (even for XbaseInterpreter), but it basically comes down to bypassing the AST, and as the node goes through, the corresponding statement is executed dynamically. For this workaround to work, as expected, the interpreter must maintain a (possibly hierarchical) context of variables and other references.

+4
source

All Articles