[ 02162: Course on Software Engineering 2 (e13) ]

 
Assignment 6
 

Task

 

In this assignment, we will use Xtext for generating a simple parser for animation expressions, which then can be used in the PNVis nets from Assignment 5 for the structured labels for animations.

 

The parser should be able to parse and build animations which could look as follows (where l1 and p1 are supposed to be identifieres of lines or points which are defined in the geometry -- but your parser does not need to check whether they exist in the geometry; your parser needs to check only that they follow the syntax for identifiers):

  • move(l1,100)
  • trigger(p1)
  • appear(p1)
  • move(l1,100); trigger(p1)
Note that, in addition to the animation primitives appear, move, and trigger, an animation can also be a sequence of primitive animations — as shown in the last example.

 

The following steps will guide you through the process (assuming that you have installed Eclipse together with Xtext as explained in the Eclipse installation guidelines for this course). For a detailed documentation of Xtext, see the Xtext User Guide (http://www.eclipse.org/Xtext/documentation.html) .

 

Steps

  1. Create an Ecore model for the animation constructs (for example, the one discussed during the practical project sessions of this course). For simplicity, it is recommended to do that in a new EMF plugin project. Create the generator model and generate the model and edit code from that (as discussed in Assignment 2).

     

    Also change the PNVis type to use the Animations from this model as the "structure" feature in the structured labels for Animations (see Assignment 5).

     

  2. Create a new Xtext project (reminder: New->Project->Other) in your workspace by selecting "Xtext Project from Existing Ecore Models" from the category "Xtext". In the "Select EPackages" dialog, select the package (resp. its genmodel file) for animations that you created in step 1. Then, select a reasonable name and file extension for the language for this project.

     

    Based on this information, the Xtext wizard will create three projects: one that defines the actual language with the parser, one for tests (with extension "test") and one with the infrastructure of the textual editor for this language (with extension "ui").

     

    The one without an additional extension is the main project: The main project that was created contains several source code folders: "src", "src-gen" and "xtend-gen". This is due to the code generation philosophy of Xtext, which is different from EMF. The parser generator will delete code in the "src-gen" "xtend-gen" and create everything again. This implies that all manually created code and the code generated by other tools like EMF should NOT be placed in "src-gen" or "xtend-src" folder, but in "src" folder!

     

    Initially, there will be one package in the src folder of the main project, which contains two files. The one with file extension "xtext" is the grammar for the language, which the Xtext wizard generated from the Ecore model; the other with extension "mwe2" is a Modelling Engine Workflow (MWE) file which defines the process for automatically generating the parser. It is used to generate the code (see below).

     

    The grammar file might not be correct (depending on how your model looks like), but it will give an idea of how the grammer file should look like and of how to properly set up the connection to your model. But, you need to change this grammer, so that it fits the examples given above.

     

  3. Change the Xtext grammar so that it fits the above syntax for the animations. Basically, the grammar is a BNF; but it is equipped with attributes that create the model out of the parsed text (attribut grammar). See Xtext Documentation for examples and more information.

     

  4. If the grammar is syntcatically correct, you can generate the parser from that grammar. To this end, right-click on the "MWE" file and select "Run as" > "MWE2 Workflow".

     

    The first time, you do this, you will be asked in the console whether you want to install ANTLR; answer this question with "y" (for "yes") and press enter.

     

    Check whether the parser generation has run correctly (no red console output). If there are errors, fix the grammar; typically, the error is that the grammar is not LL(1).

     

  5. If the generated code does not contain errors, you can start the run-time workbench and play around with the text editor: just create a text file with the extension you had chosen in the wizard when creating the Xtext project and start editing this file with the text editor. See how the editor behaves. Once the animations are syntactically correct, all problem markers should be gone.

     

  6. If this textual editor works properly, you can start integrating the parser as a parser for the structural label for Animations in your new Petri net type from Assignment 5. To this end, you can create a parser as follows, were XXX should be replaced by the name that you had chosen for your language. First, you create an instance of the parser by: Injector guiceInjector = new XXXStandaloneSetup().createInjector(); XXXParser parser = guiceInjector.getInstance(XXXParser.class); Then, you can use this parser to parse the input string, were YYY should be replaced by the name of the axiom of your grammar and Animation is the class from which all Animation classes inherit: Animation result = null; ParserRule rule = parser.getGrammarAccess().getYYYRule(); IParseResult parseResult = parser.parse(rule, new StringReader(input)); Iterable<INode> errors = parseResult.getSyntaxErrors(); if (!errors.iterator().hasNext()) { EObject object = parseResult.getRootASTElement(); if (object instanceof Animation) { result = (Animation) object; } } If the String input is parsable, the variable result would contain the result of parsing the input string in the end.

     

    NOTE: It would be a good idea not to create a new parser every time you need to parse a label. The reason is that the creation of the parser is quite computation intensive due to the use of the Guice framework.

     

  7. Check this implementation in the run-time workbench with the ePNK and your plugged in type. Check in the tree editor of the ePNK whether the animation labels that you added in the graphical editor of the ePNK where parsed correctly and gave the right result. You can enter the text of the animation in the graphical editor; for checking the parsed structure, have a look at the label in the ePNK tree editor.

     

 

Ekkart Kindler (), Oct. 10, 2013.