Clafer Parser in Java

Our reference Clafer implementation has been coded in Haskell, which allowed for quick development of the prototype. This page describes how to generate Clafer parser in Java, as it is a more mainstream language. The input grammar file is the same as for Haskell implementation. We use BNF Converter to generate scanner, parser and a skeleton of interpreter. Using a single grammar file, it can generate code for several languages, including C, C++, Java, Haskell.

Limitation

The generated parser uses brackets for establishing hierarchies of features in Clafer models. In the reference implementation of Clafer, the layout resolver inserts brackets automatically if the model uses Off-side Rule (whitespace indentation) [Wikipedia]. To overcome this limitation, one should implement a layout resolver and inject it between lexer and parser. A Stack Overflow discussion explains how to do that.

Generating Clafer Parser in Java

  1. Install BNFC

    Instruction on how to install and use the software is available here. You might find BNFC in some Linux distributions: Debian, Ubuntu, Arch.
  2. Install JLex

    JLex is a lexer generator. You can download it from here. It is a single file that you should put into some directory, e.g. /opt/java/JLex. Compile it with javac Main.java in that directory.
  3. Install Cup

    Cup is a parser generator. You can download it from here. It is a compressed package that contains source and class files. Unpack it into some directory, e.g. /opt/java/Cup.
  4. Obtain Clafer Grammar

    You can download it from the repository [CF]. Go to the directory with downloaded file.
  5. Generate Input Files for JLex and Cup

    bnfc -m -java1.5 clafer.cf

    If the command fails, make sure to that Cup and JLex are visible for java compiler by executing

    export CLASSPATH=.:/opt/java/Cup:/opt/java
  6. Generate the Code

    make
  7. Build the Test Implementation

    javac clafer/Test.java
  8. Test the Implementation

    Download Clafer model of the Linux Kernel [ZIP]. Unpack it to clafer directory by running:

    unzip linux-2.6.32.zip

    Test the implementation

    java clafer/Test < linux.cfr

AttachmentSize
clafer-java.tar.gz51.21 KB