Getting Started

You can get started to ModelCC in a few very easy steps.

Downloading ModelCC

  1. Download the ModelCC binary distribution package (modelcc-[version].zip) from the ModelCC download page.
  2. Extract it.

Setting up ModelCC in a NetBeans Project

(If you are not planning on using ModelCC in NetBeans, you may skip this section.)

Whenever you want to use ModelCC in a NetBeans project, you have to add the ModelCC library to it.
  1. Right click your project.
  2. Go to Properties.
  3. Go to the Libraries tab.
  4. In the Compile tab (which is open by default), click the Add JAR/Folder... button.
  5. Go to the directory where you extracted modelcc-[version].zip and select ModelCC.jarDo not select ModelCCExamples.jar, which is a stand-alone application for testing example languages.
  6. Accept.

Setting up ModelCC in an Eclipse Project

(If you are not planning on using ModelCC in Eclipse, you may skip this section.)

Whenever you want to use ModelCC in an Eclipse project, you have to add the ModelCC library to it.
  1. Right click your project.
  2. Go to Properties.
  3. Go to the Java Build Path section.
  4. In the Libraries tab, click the Add External JARs... button.
  5. Go to the directory where you extracted modelcc-[version].zip and select ModelCC.jarDo not select ModelCCExamples.jar, which is a stand-alone application for testing example languages.
  6. Accept.

Implementing an Example Language Model

The following class describes a very simple language. The entity MySimpleLanguage is a lexical entity that consists of an integer value, which is deducted from the @Value annotation that is assigned to an int data type. Any integer number pertains to this language.

//
import org.modelcc.*;

public class MySimpleLanguage implements IModel {

    @Value
    private int data;    
    
    public int getData() {
        return data;
    }

}
//

More complex example languages can be found in the Examples section and in the ModelCCExamples library, which is included in the source and binary distribution packages of ModelCC.

Generating and Using a Parser for the Example Language Model

The following example class describes the usage of ModelCC. A model is read from the language class, a parser is generated from the model, and the parser is used to parse an input string.

//
import org.modelcc.io.java.JavaModelReader;
import org.modelcc.metamodel.Model;
import org.modelcc.parser.fence.adapter.FenceParserGenerator;
import org.modelcc.parser.Parser;

public class MySimpleProgram {
    
  public static void main(String args[]) {

    try {

      // Read the language model.
      Model m = JavaModelReader.read(MySimpleLanguage.class);

      // Generate a parser from the model.
      Parser<MySimpleLanguage> parser = FenceParserGenerator.create(m);

      // Parse an input string.
      MySimpleLanguage result = parser.parse("35");

      // Produce output.
      if (result != null)
        System.out.println(result.getData());
      else
        System.out.println("Invalid string.");

    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}
//

For more information, read the complete ModelCC User Manual.