Installing and running JLex

In this lab you can first try to run simple.lex, mylexer2.lex, and english.lex. After you get familiar with JLex specification and scanner generation process, you can start to work on assignment 2.

In this assignment, it is more concenient to use command line, not IDEs.

Install JLex

  1. Make a directory, say 2140, using mkdir command in unix.
  2. Make a directory called “JLex” inside 2140 using mkdir again. Please notice that “JL” should be capitalized;
  3. Download file. Use right-click then save_as to save the java file in JLex directory.
  4. Compile by running the following inside JLex directory:
  5. There will be some compilation warning messages. Just ignore the warnings. Double check that the compilation is done by listing the files generated using the ls command in unix.
    or in windows using the dir command:
  6. If you see lots of class files are generated, JLex is installed.

Run JLex

You can run the scanner generator by typing the following commands in your 2140 directory. Note that you should not run inside JLex directory.

 java JLex.Main lexfileName 

For example, you can try to generate a scanner from simple.lex sepcification. Before running the following, you should download the simple.lex file from our course web site:

 java JLex.Main simple.lex 
Make sure that when you type the command you are in the directory one layer above JLex.

Notice what file is generated by JLex. You should see that a new file (the scanner) called (e.g., is produced. Now you can compile and run the scanner.

Compile the scanner

take a look at the file We will notice the class name is MyLexer;

Change the file name of to bytyping:


Then you can compile the scanner (e.g. by typing:


Or alternatively you can directly compile the generated code without changing its file name by:


look at the files that are generated. In some cases several classes may be generated;

Run the scanner

now you can run the scanner by:

java MyLexer 
notice that in general the Main method can be in other classes;

input something on the keyboard to see what happens.

Now you have completed the process, you should look the lex spec, the scanner generated, and try to change the lex to produce different scanners.