3 min. reading time

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 defined grammar rules.

Executing the following JUnit test case

package org.xtext.example.mydsl.tests

import com.google.inject.Inject
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.*

@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class GrammarVisualizationTest {

	@Inject extension IGrammarAccess
	@Inject extension GrammarToDot

	@Test def void visualizeGrammar(){
		grammar.draw.println
	}
}

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:

1_HelloWorldGrammarVisualization-GEF-Eclipse-Xtext


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

2_DotGrammarVisualization-GEF-Eclipse-Xtext


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

3_XtextGrammarVisualization-GEF-Eclipse-Xtext


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

Comments