Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreter vs. Code Generator Xtext

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

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

I've two questions;

  1. What is the difference between Interpreter and Code Generator? Aren't both for executing DSL?
  2. How to write an interpreter? Any step by step tutorial link? I found many tutorial to generate code using xtend but couldn't find any for writing an interpreter.

Thank you,

Salman

like image 694
nommyravian Avatar asked Jan 12 '13 18:01

nommyravian


1 Answers

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 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.

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.

like image 56
Zoltán Ujhelyi Avatar answered Oct 12 '22 07:10

Zoltán Ujhelyi