Using JFlex

Written by Dan on 15.01.2010 | University & Education, Programing

The following is a tutorial on using JFlex for the project 1 assignment in CS4413/CS5413 at Lakehead University




Step 1: Download and Setup JFlex


The first step is to download and setup jflex (assuming you have java installed and setup correctly).

The link to download it is: http://jflex.de/jflex-1.4.3.zip
Once you have the zip file downloaded unzip it into a folder where you would like to install it. This can be any folder as long as your user account has permission to run the programs in it. In my case (using linux) I have installed it to /home/dan/compilers/jflex-1.4.3.



Next you need to add the bin directory in jflex-1.4.3 to the system’s path. How to do this is different on each OS.



For windows XP:
1. Start -> Control Panel -> System -> Advanced
2. Click on Environment Variables, under System Variables, find PATH, and click on it.
3. In the Variable Value text box add ;my_path_to_jflex\jflex-1.4.3\bin\ to the end  where “my_path_to_jflex” is your path to the jflex folder you installed. For example if you installed jflex to C:\Users\Dan\jflex-1.4.3 the text that you would add to the path would be ;C:\Users\Dan\jflex-1.4.3\bin\  make sure to not to delete the contents of the text box and only add to them.
4. Click OK -> OK -> OK untill you are back to the desktop.

See http://support.microsoft.com/kb/310519 for more information on system variables in xp.



For windows Vista and 7:
1. Click on the start menu and type “edit the system environment variables” into the search box in till you see the control panel short cut come up.
2. Click “edit the system environment variables” and then click Environment Variables on the window that pops up.
3. From the System variables list find Path and double click on it.
4. In the Variable Value text box add ;my_path_to_jflex\jflex-1.4.3\bin\ to the end where “my_path_to_jflex” is your path to the jflex folder you installed. For example if you installed jflex to C:\Users\Dan\jflex-1.4.3 the text that you would add to the path would be ;C:\Users\Dan\jflex-1.4.3\bin\ make sure to not to delete the contents of the text box and only add to them.
5. Click OK -> OK -> OK untill you are back to the desktop.



For Linux/Unix using the bash shell:
1. Open ~/.bashrc in your favorite editor (ex. nano ~/.bashrc).
2. At the end of the file add:

PATH=$PATH:your_path_to_jflex/jflex-1.4.3/bin
export PATH

Where your_path_to_jflex is your path to the folder where you installed jflex. For example my PATH line is PATH=$PATH:/home/dan/compilers/jflex-1.4.3/bin
3. Save the file.



Once you have updated the path on your system you may need to logout and back in or restart your shell before the changes take effect.



Windows users will also have to edit the jflex.bat file in jflex-1.4.3/bin and set JFLEX_HOME in the file to their correct value.

JFLEX_HOME should point to the directory where jflex is installed. For example if jflex was installed to C:\Users\Dan\jflex-1.4.3\ the line should be: set JFLEX_HOME = C:\Users\Dan\jflex-1.4.3\

Next the lines following the “only needed for JDK 1.1.x” and “for JDK 1.1.x” comments should be removed or commented out. And finally the “rem” on the last line should be removed. The file should look something like this:

@echo off
REM Please adjust the paths JFLEX_HOME and JAVA_HOME to suit your needs
REM (please do not add a trailing backslash)

set JFLEX_HOME=C:UsersDanjflex-1.4.3

REM -------------------------------------------------------------------

set CLPATH=%JAVA_HOME%libclasses.zip;%JFLEX_HOME%libJFlex.jar

REM for JDK 1.2
java -Xmx128m -jar %JFLEX_HOME%libJFlex.jar %1 %2 %3 %4 %5 %6 %7 %8 %9




Linux users may want to take a look at the jflex-1.4.3/bin/jflex script however, it should be fine by default.




Step 2: Using JFlex



Jflex can be used via it’s GUI or via the command line. To use the GUI simply type jflex on the command line/shell and the GUI should run. To pass an input file to jflex via the command line simply type jflex input_file where input_file is the name of the lexical specification file (in our case lexer.flex).



GUI
1. Click the browse button for the Lexical specification field and select your Lexical Specification file (lexer.flex).
2. If you want to output the java class in a different directory then listed in the output directory filed, click the browse button by the output field and select the new output directory.
3. Click generate.
4. Look at the Messages text box. If there are warnings or errors there is likely something wrong with the contents of your lexer.flex file, go through it and fix any errors and try to generate it again. If there are no errors or warnings your lexical analyzer should be created in the output directory as a .java file (in our case Lexer.java).



Command Line
1. Use the command line or shell to navigate to the directory with your lexical specification (lexer.flex).
2. Type in jflex input_file where input_file is the name of your specification (lexer.flex).
3. If there are warnings or errors there is likely something wrong with the contents of your lexer.flex file, go through it and fix any errors and try to generate it again.
4. If there were no errors or warnings your lexical analyzer should now be output in the same folder as a .java file (in our case Lexer.java).

For more details on install and running jflex see http://jflex.de/installing.html




Step 3: Using the Lexical Analyzer



For this assignment a Driver.java and Token.java file were given to you that contain the code to use the lexical analyzer generated as Lexer.java. To use them on the C– code provided you must first compile the java code using javac. Since Driver, Token and Lexer are all in the same package they must be in the same directory named lexer. To compile the code simple issue the following commands in your shell/command line:


javac Token.java
javac Lexer.java
javac Driver.java

If there was an error comping the Lexer.java file, there likely was a problem with your lexical specification.



If all the code complied with no errors or warnings you should be able to run the Driver program on some C– code by issuing the fallowing command:


java Driver cmm_file

Where cmm_file is the text file contain the C– code. For example if the code file is test.cmm the line should be java Driver test.cmm.

Ideally there should be not errors and the output should match the example output given (assuming you used the example input in test.cmm).




Assignment Tips


  • Make sure all your class files are in the same folder named lexer and that this folder is in the java class path.
  • There should be no need to make any changes to any of the .java files. All the work should be done in the lexer.flex file.
  • Take a look at the contents of the Token.java file, the tokens that are returned by nextToken in the scanner should be of this type.
  • Make sure to test your scanner on the given sample input/output.
  • You can find the jflex documentation here: http://jflex.de/manual.html which will help you in creating the lexer.flex specification.
  • http://www.cs.auckland.ac.nz/~bruce-h/lectures/330ChaptersPDF/ is also a good resource for using JFlex and CUP.

RMS at Lakehead University

Written by Dan on 01.06.2009 | Uncategorized

On May 30th, 2009 Richard Stallman (RNS) was awarded an honorary degree from Lakehead University in Thunder Bay Ontario and gave the convocation address to the Lakehead’s 2009 graduates. RMS’s address focused on the need for educational institutions to switch to free and open software rather then addicting there students to proprietary products from the software giants. Lakehead’s use of windows media format for there streaming video of the convocation ceremony and use of proprietary software for teaching was heavily cirtisesd provoking Dr. Gilbert, president of the university, to offer a small rebuttal after the address and agreed to consider Stallman’s challenge to adopt free software in the university.

A recording of the convocation address can be found below:

Download MPEG Copy

Screen Cast

Written by Dan on 18.09.2008 | Moodle, GSoC

After having tones of trouble with sound drivers and getting my mic to work i have finally been able to get sound in my screen cast and it uploaded.

You can view the flash version of the screen cast here: http://compsci.ca/~dan/screencast.swf

This screen cast is a demo of the first version of the Moodle Grade Book stats and visual reports witch i worked on for Google Summer of Code.

End of My Summer of Code

Written by Dan on 18.08.2008 | Moodle, GSoC

Today is the official pencil down date for the 2008 Google Summer of Code and is the last day code can be submitted to be counted in the final evaluation. This is also the week i move back to Thunder Bay to start a new term at Lakehead University, so it has been a busy week of finishing up code and documentation, getting packed up for the move and setting up the CompSci.ca server to host a new project i will be a part of this fall.

I have committed the latest code for the project and considering this to be a first release of the report/stats and report/visual plug-ins for Moodle 2.0 dev. I have also update my test site with the newest code so any one can take a look at the plug-ins running in a Moodle install (you can login as guest). I had also hoped to have a screen cast demoing the plug-ins for today however due to some trouble getting sound working right it will probably be ready late Tuesday or early Wednesday.

Anthony Borrow has requested that i get an entry for the plug-ins added to the Moodle Modules and Plugins Database and some pages up about it on the Moodle Wiki instructing users how to use the plug-ins and install them, witch i plan to also get done this week, time and internet permitting (i may not have internet access for a few days well moving back to Thunder Bay).

You can download the plug-ins at:

report/visual: http://download.moodle.org/plugins/grade/report/visual.zip
report/stats: http://download.moodle.org/plugins/grade/report/stats.zip

Daily Love Letters