How do I use AntlrWorks 2 in Netbeans?

1,856 views
Skip to first unread message

Gregory K

unread,
Feb 3, 2015, 1:02:43 AM2/3/15
to antlr-di...@googlegroups.com
Everything seems to have installed correctly. I am running Netbeans 8.0.2 and the AntlrWorks 2.4.1 Editor plug-in seemed to install without problems.

But how do I begin to build a project? Do I create a regular Java project? Is there a package structure I should adhere to? How do I run the ANTLR tool on the grammars I create? Where do the corresponding *.java files go when I run it?

So far, I created a Java project, created a package a put a *.g4 grammar in it. The editor works beautifully, but I can't even figure out how to run the ANTLR tool on the grammar. Right-clicking on the file does not seem to give any reasonable options.

Sorry for such basic questions, but I am having a hard time finding even a simple example or tutorial regarding this on-line. If someone can point me to a simple example of using AntlrWorks 2 in Netbeans I would be very grateful.

Cheers,
Greg

Gregory K

unread,
Feb 3, 2015, 1:47:30 PM2/3/15
to antlr-di...@googlegroups.com

Okay, I figured *most* of it out. If anyone else sees this post, here is a quick tutorial to get you started. Others can feel free to correct this.

++++++++++ ++++++++++ ++++++++++

# AntlrWorks in NetBeans Tutorial

(1) Download the latest version of ANTLR (for example, antlr-4.5-complete.jar) and store it on your machine (for example, in /usr/local/lib).

(2) Download and install the latest version of Netbeans

(3) From within NetBeans, go to Tools -> Plugins. Click on Available Plugins and search for 'Antlr'. Select ANTLRWorks Editor and install it.

(4) Create a new Java project called SimpleCalc.

(5) Add the ANTLR jar file (from step 1) to your Libraries folder. Right-click on the Libraries folder and choose 'Add JAR/Folder...", navigate to the ANTLR jar file, and select Choose.

(6) In your Source Packages folder, create a new package called simplecalc.grammar.

(7) In your simplecalc.grammar package, create a new ANTLR file named SimpleCalc.g4. Right click on simplecalc.grammar and choose New -> Other... Find the ANTLR folder in Categories and choose ANTLR 4 Combined Grammar. Name the file SimpleCalc.g4 and click Finish. 

(8) Paste the following into SimpleCalc.g4 (see Programming Language Pragmatics, 3rd ed. by M. Scott).

grammar SimpleCalc;

program : stmt* '$$' ;

stmt    : ID ':=' expr          # AssignStmt
        | 'read' ID             # ReadStmt
        | 'write' expr          # WriteStmt
        ;

expr    : expr ('*' | '/') expr # MulDivExpr
        | expr ('+' | '-') expr # AddSubExpr
        | '(' expr ')'          # ParenExpr
        | ID                    # IdentExpr
        | NUMBER                # NumberExpr
        ;

// tokens

READ    : 'read' ;
WRITE   : 'write' ;
LPAREN  : '(' ;
RPAREN  : ')' ;
ASSIGN  : ':=' ;
ADD     : '+' ;
SUB     : '-' ;
MUL     : '*' ;
DIV     : '/' ;
STOP    : '$$' ;

ID      : [a-z]+ ;
NUMBER  : [0-9]+ ;

(9) Highlight the grammar file and select Run -> Generate Recognizer... -> . For Location, choose the default, for Features, choose both Generate listener and Generate visitor, and make sure the package is simplecalc.grammar. Ignore the Advances settings and click Finish.

++++++++++ ++++++++++ ++++++++++

Ultimately, I would like to add steps to create a pretty printer using the listener and an evaluator using the visitor. Also, I am still not certain whether it is possible to use the TestRig via AntlrWorks.

Cheers,
Greg
Reply all
Reply to author
Forward
0 new messages