We have used Xtext to define a concrete syntax for a domain-specific language
called Simple Language of Communicating Objects (SLCO). This language is.
Generic Language Technology Working with Xtext
Introduction We have used Xtext to define a concrete syntax for a domain-specific language called Simple Language of Communicating Objects (SLCO). This language is used to describe systems consisting of objects operating concurrently that communicate with each other using signals. The behavior of these objects is defined by state machines. You will be presented with a small part of the grammar that defines the concrete syntax of SLCO, and are asked to complete and fine-tune this grammar.
Setup For this assignment you will need to install and use Eclipse. Eclipse is available for Windows, Mac and Linux. We will explain how to install Eclipse for Windows, but the procedure for Mac and Linux is similar. 1. Navigate to http://www.eclipse.org/downloads/ and download Eclipse. You should download the following version: Eclipse Modeling Tools (271 MB) 2. Unpack the downloaded file. 3. Start Eclipse by running eclipse.exe from the eclipse folder. 4. Eclipse will ask you which workbench you want to use. You can use the one Eclipse suggests or browse to a different folder and select that folder as your workbench. It doesn’t matter which option you choose for this exercise. 5. Click on the arrow to go to the workbench.
6. In the Help menu, select Install New Software. . . . 7. Select –All Available Sites– from the drop-down menu after Work with:.
1
8. Select EMF - Eclipse Modeling Framework SDK listed under Modeling.
9. Install this SDK. 10. In the Help menu, select Install Modeling Components. 11. Select and install Xtext, Xpand and ATL.
Completing an Xtext Grammar In the first part of this exercise, you will complete an unfinished Xtext grammar. More information about Xtext can be found at http://www.xtext.org.
Creating a New Xtext Project Create a new XText project as follows. 2
• In the package explorer, right click and select New → Project. . . • In the pop-up window, select Xtext → Xtext Project and click Next.
• In the next pop-up window, enter the properties shown in the following screen shot and click Finish.
3
There are two more steps you need to take before you can start working on our grammar. • Inside the archive http://www.win.tue.nl/~lengelen/gltooti2011/xtext.zip, you will find a partial grammar TextualSlco.xtext. Replace the file org.xtext.textualSlco/src/org.xtext/TextualSlco.xtext with the file from the archive. • Comment out the line composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator" in the file org.xtext.textualSlco/src/org.xtext/GenerateTextualSlco.mwe2. Your first task is to complete the grammar we have provided such that it can parse the files Simple.slcotxt and Complex.slcotxt. You can safely ignore the third file, Expressions.slcotxt. This file is only relevant for the second part of this assignment.
Using an Xtext Grammar As soon as your grammar is free of errors, you can generate a parser for it by running the workflow org.xtext.textualSlco/src/org.xtext/GenerateTextualSlco.mwe2. You run workflows by right-clicking on them and selecting Run As → MWE2 Workflow. The first time you run this workflow, you will be asked whether you want
4
to use ANTLR 3. Indicate that you do want to use it by typing ”y” and pressing Enter. To use the parser you have just generated, you need to export it as a plug-in by completing the following steps: • In the File menu, select Export. . . . • In the pop-up window, select Plug-in Development → Deployable plug-ins and fragments and click Next.
• In the next pop-up window, click Select All and set the directory to your Eclipse folder, e.g., c:\eclipse. Finally, click Finish.
5
• Now restart Eclipse. Once you have restarted Eclipse, create a new plain project in the same way you created the Xtext project before: • In the package explorer, right click and select New → Project. . . • In the pop-up window, select General → Project and click Next. • Give this project any name you want and click Finish. Inside this new project, you can test whether the parser you have generated is able to parse the files mentioned above. • Right-click on the project and select Import. . . . • In the pop-up window, select General → File System and click Next. • Click Browse, browse to the folder where you have unpacked the archive and click OK. • Select the files you want to import and click Finish. Open the files by double-clicking them to see if your parser is able to parse them. If Eclipse asks you whether you want to add the Xtext nature to this project, click Yes.
6
Fine-Tuning an Xtext Grammar As you may have noticed, we have defined our grammar in such a way that it can only parse expressions containing lots of brackets. Simply removing the brackets from the definition of binary expressions would result in a left-recursive grammar, which Xtext cannot handle. Try to fine-tune the grammar by using Assigned Actions to eliminate the left-recursion. If you have done so correctly, you will be able to parse the file Expressions.slcotxt as well.
7