# First experience with Xtext

In this post I describe my experience with the recent version (2.1) of Xtext, a framework for developing domain specific languages (DSLs).

The main input for Xtext is a grammar file written in an elegant little language. The grammar language uses a variation of Extended Backus–Naur Form to define rules for both terminals and nonterminals. So far this is very similar to parser generators like GNU bison or ANTLR which can create parsers out of similar grammar definitions. I have a lot of experience with parser generators and have become a bit sceptical about them. A hand-written parser is often easier to debug and maintain compared to a generated one. From my experience other parts of the language development process like semantic analysis or code generation are usually more complicated and at the same time these receive much less attention from the developers of automated tools. What makes Xtext different is that it not only generates a parser and a lexer but also a full-blown Eclipse IDE tailored for your language. So I decided to give Xtext a try and develop a toy language with it. I take a shortcut and start from one of the examples that come with Xtext. Choosing File -> New -> Example... and selecting Xtext Simple Arithmetic Example under the Xtext Examples category creates three projects:
• org.eclipse.xtext.example.arithmetics (main)
• org.eclipse.xtext.example.arithmetics.tests (tests)
• org.eclipse.xtext.example.arithmetics.ui (ui)
The main project contains the grammar file Arithmetic.xtext for the language. Following is an example of input that it accepts:

Here is how it looks in Eclipse:

Syntax colouring, content assist, the Outline and Problems views - all work nicely. Let's now extend this language. First I add support for scientific notation, like 6.02e23, in floating-point literals. The following modification of the NUMBER terminal does the job:

Next I add support for string expressions. To check for errors like trying to divide a string by a number I introduce a simple type system that consists of three types - NUMBER, STRING and INVALID with the latter used in case of errors. The type is represented by the following simple enum added to the main project:

Then I add a string concatenation expression which uses the & operator and replace multiple classes for binary expressions with a single class BinaryExpr to make writing validation checks easier:

The next step is to add the type checks to the validator. Unfortunately Xtext validator does pre-order traversal of the AST while to check expression types we need post-order, because the type of an expression depends on the types of its subexpressions. It is easy to implement such traversal manually:

As you can see from the above code the type checks aren't really pretty with all the instanceofs and casts. If I had more control over the generated AST classes I would apply the Visitor pattern and implemented the type checker as a visitor. However this can be mitigated with the help of the poorly documented PolymorphicDispatcher.

Next I modify the Calculator class that implements evaluation to handle string concatenation. Also the evaluation methods should return Object instead of BigDecimal because the value of an expression can be a string or a number:

And finally I change the InterpreterAutoEdit class in the ui project to check types before evaluation:

After implementing the above changes and re-generating Xtext artefacts, I get an Eclipse IDE for the language with simple string expressions and type checking: