AntlrInNetBeansJavaApplication

How to use Antlr in Netbeans

This article will describe how to tweak NetBeans to provide editor support (code completion, hyperlink, etc.) for Antlr grammar files and how to create NetBeans Java project with automatically generated from Anrlr grammar files parts.

Editor Support

For adding Editor Support it's enough to download and install Antlr Editor Support plugin.

This plugin provides:

  • Coloring,
  • Code folding,
  • Code completion,
  • Hyperlink,
  • Mark Occurrences,
  • Navigator.


After installation Antlr grammar (.g) files will look like:
Image:netbeans_antlr_editor_support_AntlrInNetBeansJavaApplication.jpg

Java Application Project with Antlr grammar

First of all lets create Java Application Project as described in NetBeans IDE Java Quick Start Tutorial
Lets call it MyAntlrProject.

Now lets create grammar file.
There is no special support for Antlr grammar (.g) files in file creation wizards so lets create empty file and call it MyGrammar.g (extension is important for Antlr Editor Support plugin).
Image:new_empty_file_creation_AntlrInNetBeansJavaApplication.jpg

Then put your grammar to MyGrammar.g.
I'm going to use simple grammar example from antlr.org.
Don't forget to rename grammar MyGrammar and add package to @header and @lexer::header:

@header {
    package myantlrproject;
    import java.util.HashMap;
}

@lexer::header{
    package myantlrproject;
}

Attaching Antlr Tool

Firs of all we need Antlr Tool :)
It could be downloaded from antlr.org: ANTLR 3.1.3 source distribution

Unpack it to any directory you want.
For me it is /opt/antlr-3.1.3

Now we have to change build.xml of our project.
It locates in project root directory.

Here are the targets for compiling Antlr grammar:

<target name="-pre-compile" depends="antlr">
</target>
 
<target name="init-antlr">
    <!-- Full path to Antlr jar -->
    <property name="antlr.jar" location="/opt/antlr-3.1.3/lib/antlr-3.1.3.jar"/>
    <!-- Grammar path -->
    <property name="antlr.grammar" location="src/myantlrproject/MyGrammar.g"/>
</target>
    
<target name="antlr" depends="init-antlr" unless="up-to-date">
    <!-- Compiling grammar -->
    <java classname="org.antlr.Tool"  fork="true">
        <arg value="${antlr.grammar}"/>
        <classpath path="${antlr.jar}"/>
    </java>        
</target>

So our build.xml will look like:

<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="MyAntlrProject" default="default" basedir=".">
    <description>Builds, tests, and runs the project MyAntlrProject.</description>
    <import file="nbproject/build-impl.xml"/>
    <!--

    There exist several targets which are by default empty and which can be 
    used for execution of your tasks. These targets are usually executed 
    before and after some main targets. They are: 

      -pre-init:                 called before initialization of project properties
      -post-init:                called after initialization of project properties
      -pre-compile:              called before javac compilation
      -post-compile:             called after javac compilation
      -pre-compile-single:       called before javac compilation of single file
      -post-compile-single:      called after javac compilation of single file
      -pre-compile-test:         called before javac compilation of JUnit tests
      -post-compile-test:        called after javac compilation of JUnit tests
      -pre-compile-test-single:  called before javac compilation of single JUnit test
      -post-compile-test-single: called after javac compilation of single JUunit test
      -pre-jar:                  called before JAR building
      -post-jar:                 called after JAR building
      -post-clean:               called after cleaning build products

    (Targets beginning with '-' are not intended to be called on their own.)

    Example of inserting an obfuscator after compilation could look like this:

        <target name="-post-compile">
            <obfuscate>
                <fileset dir="${build.classes.dir}"/>
            </obfuscate>
        </target>

    For list of available properties check the imported 
    nbproject/build-impl.xml file. 


    Another way to customize the build is by overriding existing main targets.
    The targets of interest are: 

      -init-macrodef-javac:     defines macro for javac compilation
      -init-macrodef-junit:     defines macro for junit execution
      -init-macrodef-debug:     defines macro for class debugging
      -init-macrodef-java:      defines macro for class execution
      -do-jar-with-manifest:    JAR building (if you are using a manifest)
      -do-jar-without-manifest: JAR building (if you are not using a manifest)
      run:                      execution of project 
      -javadoc-build:           Javadoc generation
      test-report:              JUnit report generation

    An example of overriding the target for project execution could look like this:

        <target name="run" depends="MyAntlrProject-impl.jar">
            <exec dir="bin" executable="launcher.exe">
                <arg file="${dist.jar}"/>
            </exec>
        </target>

    Notice that the overridden target depends on the jar target and not only on 
    the compile target as the regular run target does. Again, for a list of available 
    properties which you can use, check the target you are overriding in the
    nbproject/build-impl.xml file. 

    -->

    <target name="-pre-compile" depends="antlr">
    </target>
 
    <target name="init-antlr">
	<!-- Full path to Antlr jar -->
        <property name="antlr.jar" location="/opt/antlr-3.1.3/lib/antlr-3.1.3.jar"/>
	<!-- Grammar path -->
	<property name="antlr.grammar" location="src/myantlrproject/MyGrammar.g"/>
    </target>
    
    <target name="antlr" depends="init-antlr" unless="up-to-date">
	<!-- Compiling grammar -->
        <java classname="org.antlr.Tool"  fork="true">
            <arg value="${antlr.grammar}"/>
            <classpath path="${antlr.jar}"/>
        </java>        
    </target>

</project>

Now we can try to Clean and Build our project.
So MyGrammarLexer.java and MyGrammarParser.java will be generated.
But project won't be compiled successfuly because there is no right dependencies.

Lets add them! :)

Select "Add Jar/Folder" in Libraries context menu and add antlr-3.1.3.jar (for me it locates in /opt/antlr-3.1.3/lib).
Image:adding_jar_AntlrInNetBeansJavaApplication.jpg

Lets try to Clean and Build our project again.
BUILD SUCCESSFUL!

Now we have project with Antlr grammar and automatically generated lexer and parser.

Using of lexer and parser

Now we can use our lexer and parser as we want.
Let's change our Main a little.

Add this code to main method:

try {
    ANTLRInputStream input = new ANTLRInputStream(System.in);
    MyGrammarLexer lexer = new MyGrammarLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    MyGrammarParser parser = new MyGrammarParser(tokens);
    parser.prog();
} catch (RecognitionException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

Add the following imports to Main:

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.antlr.runtime.*;

Clean and Build.

Now we have program for evaluating simple expressions!

In command line from project root run

java -jar dist/MyAntlrProject.jar

, type

1+1<Enter><EOF(Ctrl+D)>

and you'll get 2!

Regards,
Nick Krasilnikov

Alternative approach to the Ant build

An alternative approach to Integrating ANTLR without learning Ant has been described in which customisations to build.xml do not need to be specific for the files in your project.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo