Pro Tip: Visualizing Xtext Grammar with Eclipse GEF

The Xtext grammar is the central component when developing DSL workbenches based on the Xtext framework. In case of complex DSLs, analyzing the graphical representation of *.xtext files can be useful to understand the structure of the defined grammar rules.

Executing the following JUnit test case

package org.xtext.example.mydsl.tests

import org.eclipse.xtext.GrammarToDot
import org.eclipse.xtext.IGrammarAccess
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.junit.Test
import org.junit.runner.RunWith

import static extension org.eclipse.xtext.xbase.lib.InputOutput.*

class GrammarVisualizationTest {

	@Inject extension IGrammarAccess
	@Inject extension GrammarToDot

	@Test def void visualizeGrammar(){

on the Xtext 'Hello World' example converts the MyDsl.xtext grammar into a Graphviz dot representation and prints the following content into the console:

digraph G {
	assignment525968792 [shape=record,label="Model:\ngreetings+= *"];
	rulecall504858437 [label=Greeting];
	group422330142 [shape=record,label="Greeting:\n( )"];
	keyword649329985 [label="'Hello'"];
	assignment1955920234 [label="name= "];
	rulecall1444635922 [label=ID];
	keyword775386112 [label="'!'"];
	assignment525968792->rulecall504858437 [];
	group422330142->keyword649329985 [];
	assignment1955920234->rulecall1444635922 [];
	group422330142->assignment1955920234 [];
	group422330142->keyword775386112 [];

The Eclipse GEF DOT Graph view is able to visualize such Graphviz *.dot files within Eclipse's Workbench UI:


Visualization of the Eclipse GEF DOT Xtext grammar (Dot.xtext) looks like this:


Visualization of the Xtext grammar itself (Xtext.xtext) is as follows:


How does the visualization of your Xtext grammar looks like? Feel free to share your solution with us! We are also open to any suggestions/contributions to improve Xtext grammar visualization in the future!

If you would like to read more about the Xtext framework, check out "1001 tips & tricks for Xtext"!

1001 tips and tricks for Xtext

About Tamas Miklossy

Tamas works as an IT consultant at itemis AG in Lünen. He is interested in model driven software development, especially in graphical and textual modeling solutions.