JCup & JFlex With Ant

I’ve been bogged down in work and school lately.  So, it has been a while since I’ve written anything.  This semester I’m taking an Instruction Set Architecture course at Wash U–basically, the final project is to design a processor with VHDL in ModelSim.  I think I’m going to stick to software.  Anyway, part of the project involved building an assembler that would convert our simple assembly language into a hex representation of our machine instructions.

Using the tools and code base from my Compiler Design class, I built a fancy assembler.  I used Java 1.5 to write the assembler, JFlex to write a scanner, and JCup to write a parser, and Ant for build control.  I spent a lot of time trying to get JCup, JFlex, and my code to compile correctly with Ant.  Figured this might be something worth throwing on here.

I’m going to try and outline the steps I took to use JFlex & JCup with Java 1.5 & Ant.  I was never one for IDEs; so, this is making the assumption that it is all going to be done from the command line.  I used Cygwin on Windows XP to do this; so, what I describe here should also be valid on just about any Unix platform.

You can download the most recent version of Sun Java here.  I used 1.5.0_08.  With Cygwin, the javac binary is not in the path; so, it must be added.  I used the following script to setup the environment for compiling and execution:

JAVA_HOME="/cygdrive/c/Program Files/Java/jdk1.5.0_08"
PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME PATH

At this point, you should be able to call javac to compile and java to run a class file.

Here is the Ant build.xml file I used:

<project name="project assembler" default="compile" basedir=".">
    <description>
     Project Assembler
    </description>
  <!– set global properties for this build –>
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <taskdef classname="JFlex.anttask.JFlexTask" name="jflex">
  <classpath>
    <pathelement path="/tmp/assembler/jflex-1.4.1/lib/JFlex.jar"/>
  </classpath>
  </taskdef>
  <taskdef name="cup" classname="java_cup.anttask.CUPTask"/>
  <target name="init">
    <!– Create the time stamp –>
    <tstamp/>
    <!– Create the build directory structure used by compile –>
    <mkdir dir="${build}"/>
  </target>
  <target name="compile" depends="init,jflex,cup"
        description="compile the source " >
    <!– Compile the java code from ${src} into ${build} –>
    <javac      srcdir="${src}"
                destdir="${build}"
                debug="on"/>
  </target>
  <!– Create the Lexer.java file –>
  <target name="jflex" depends="init"
        description="do the jflex thing">
    <jflex
      file="${src}/cse560/scanner/Lexer.jflex"
      destdir="${src}"
    />
  </target>
  <target name="cup">
  <cup srcfile="${src}/cse560/parser/Parser.cup"
    destdir="${src}"
    interface="true"
  />
  </target>
  <target name="clean"
        description="clean up" >
    <!– Delete the ${build} directory trees –>
    <delete dir="${build}"/>
    <delete file="${src}/cse431s/scanner/Lexer.java"/>
  </target>
</project>

You can download the most recent version of Ant from here.

You can download the most recent version of JFlex from here.

You can download the most recent version of JCup from here.