DTU 
 

 

02162: Course on Software Engineering 2 (e14)

The last tutorial of this course discusses some additional technologies that might be needed for the project. Specifically, a technology for parsing some strings into an abstact syntax tree (AST) is discussed. Such a parser will be needed for implementing the language for the rules that handle the incoming events.

 

There exist many parser generators based on Java (see this comparison for an overview). First and foremost, the difference is on the parsing methods and the class of languages that is supported (e.g. LL(k), LALR(k), and LR(k)); but there are also differences in usability, IDE support, and required additional technologies for running the generated parsers.

 

Since the language for formulating the rules in our project is relatively simple, we discuss the JavaCC parser and scanner generator in this tutorial. JavaCC does support LL(k) grammars only and does not have the smoothest IDE integration. But, it easy to get started with and produces a relatively lightweight parser without any additional dependencies; therefore, the generated parser can be easily deployed to different platforms as long as a Java installation is available.

 

If you know other parser generator technolgies such as ANTLR, you can user them for your project, too. In the previous years, we had used Xtext (see last year's assignment 6 for more information), but since Xtext is tightly integrated with EMF that we are not using in this year's project, it is not very suitable for our purposes this year.

 

As an example, we generate a parser for a very simple rule language. Two examples of rules in that simple language are shown below

 

test1:
  when DoorAlarmEvent
  if event.source == 1  // this should better be a String identfying the source
     &&
     event.value >= 50  // better user a float here
     &&
     system.securitylevel == 1
  then actorSound1.play(1,30), // the sound to be palyed should better be a string
                               // the time it should be palyed could be a float
       UserAlert.raise(event.event); // could be event only (my model and grammar
                                     // does bot alow single variables

test2:
  when DoorAlarmEvent
  if system.securitylevel >= 2
     ||
     event.source == 1  // this should better be a String identfying the source
     &&
     event.value >= 50  // better user a float here
  then actorSound1.play(1,30), // the sound to be palyed should better be a string
                               // the time it should be palyed could be a float
       UserAlert.raise(event.event); // could be event only (my model and grammar
                                     // does bot alow single variables

 

The constructs of that language are defined by the following class diagram, which formalizes the abstract syntax of this language (not that contraints are not covered here):

 

The definition of the concrete syntax of this language along with the definition of a parser for that language can be found in the file Rules.jj. This file and the underlying concepts of attribute grammars will be explained and discussed in the tutorial. Note that the file Rules.jj refers to the Java classes of the abstract syntax (UML diagram above); the code for these classes was automatically generated and is contained in the complete project, which is availabe here: rulesparser.zip. But, this code won't compile in your version of Eclipse unless you install EMF on your Eclipse; but you do not need to do that, you can write the classes representing the abstract syntax manually.

 

For generating the parser from file Rules.jj, you need to either install the JavaCC plugin for Eclipse or you need to install JavaCC as standalone software (without any IDE) from here or here.

 

In the tutorial, it will be explained how the code for the parser can be generated for the parser definition and how this code can be used. For more details, have a look at the following tutorials:

 

 

Ekkart Kindler (), October 23, 2014