Using JavaCC in the Wentworth Labs

  1. Install JavaCC (if not already there). 
  2. Open a DOS Command Prompt. 
  3. Change the PATH (for this command window only) to include java, javac and javacc as follows:

    set PATH=C:\VisualCafeXE\java2\bin;C:\JavaCC-3.0\bin;%PATH%

  4. Define CLASSPATH as an Environmental variable.  You can do this through the control panel or for the current command window.

    CLASSPATH=C:\VisualCafeXE\java2\bin;

  5. Download the zip file with simple calculator examples: SimpleCalculators.zip .
  6. Try out JavaCC with the simplecalculator example: Calc1i.jj
  7. Copy Calc1i.jj to a temp folder, such as C:\Temp\Mw .
  8. In your DOS window, cd C:\Temp\Mw
  9. type: JavaCC Calc1i.jj
  10. This creates several .java files
  11. to compile, you can use javac as in Javac *.java to compile all java files in your folder
  12. To run, type: java Calc1i, then enter an arithmetic expression to evaluate.
  13. To view the .jj and .java files, you can use Visual Cafe, Visual Studio, or any text editor.

Notes on using JJTree

  1. Open a command window
  2. Make sure the JavaCC\bin directory is added to the path
  3. Try typing JJTree  to see the usage message
  4. Copy eg1.jjt found in javacc-3.0\examples\JJTreeExamples to a clean directory
  5. Change to the clean directory.  Run jjtree on eg1.jjt
  6. This creates several files including a prepared .jj file
  7. Run javacc on eg1.jj
  8. Compile all the java files using javac
  9. You have built a program capable of constructing a parse tree out of integer arithmetic expressions
  10. To test, run: java eg1.  It will read from std input.  Type your expression ended by a semicolon.
  11. i.e. java eg1 34*10 + 10/3 -5;
  12. You will get an output like:
  13. 34*10 + 10/3 -5;
    Start
    Expression
    AdditiveExpression
    MultiplicativeExpression
    UnaryExpression
    Integer
    UnaryExpression
    Integer
    MultiplicativeExpression
    UnaryExpression
    Integer
    UnaryExpression
    Integer
    MultiplicativeExpression
    UnaryExpression
    Integer
    Thank you.
  14. You have just parsed the input expression building a parse tree out of SimpleNode's.  The root of the tree is n.  The SimpleNode class has a dump function which does a DFT of the parse tree printing out the node's name after indenting with a prefix.
  15. You may modify the dump function to perform other actions while traversing the parse tree.
  16. Try doing the same thing with the eg2.jjt file.
  17. Add some actions to the .jjt file.  Capture the value of the integers as follows:
  18. void Integer() : {Token t;}
    {
    t = <INTEGER_LITERAL>
    {int i = Integer.parseInt(t.image);jjtThis.setValue(i);}
    }
  19. jjtThis refers to the current SimpleNode.  You need to add a value field to SimpleNode as well as public getters and setters
  20. Over-ride the inherited dump method in ASTInteger.java so that integers are printed along with their values.
  21. Now change your parser into a calculator.  
  22. You will need to store operands as they are parsed (possibly in a stack)
  23. Recognize operators when parsed, and add methods to evaluate expressions.

-M Werner

Last Updated: 07/10/2002