Want all the instructions in one code block without the explanations? Check out the appendix.
Before Starting
Prerequesite Software
These instructions are for Linux machines. If you’re a Windows user, I recommend using the Windows Subsystem for Linux (WSL). The only prerequesite software required is Java. If you’re on a Debian-based system and don’t have Java installed, you can install it via sudo apt install default-jre
and sudo apt install default-jdk
.
Organization
You may want to create a separate “2140” directory for this class:
mkdir -p 2140
cd 2140
Make sure you download the following two files:
You can download files on the CS servers by running curl url -o filename
, where url
is replaced with the url of the file you want to download, and filename
is the filename of the file you’re downloading.
So, to download these two files, you can run:
curl https://blog.bornais.ca/2140/lex/Main.java -o Main.java
curl https://blog.bornais.ca/2140/lex/simple.lex -o simple.lex
Instructions
- Create a “JLex” directory (case sensitive!):
mkdir -p JLex
- Move “Main.java” (attached) into the JLex directory
mv Main.java JLex
- cd into the JLex directory
cd JLex
- Compile “Main.java”
javac Main.java
- Move back to the parent directory
cd ..
- JLex is now installed! You can now run the scanner generator.
The general method to do this is to run:java JLex.Main lexFileName
, replacinglexFileName
with the name of your lex file.
We’ll use the sample lex filesimple.lex
, courtesy of Dr. Jianguo Lu, which we downloaded earlier.
To run this file, simply type:
java JLex.Main simple.lex
- You should now see a new file generated, called
lexFileName.java
, or in this casesimple.lex.java
. We must now compile it, by typing:
javac simple.lex.java
- A new file,
MyLexer.class
should have been created (note, if using a lex file other thansimple.lex
, this file may be named something different). We can now run the lexer by running:
java MyLexer
That’s it!
Now try typing stuff on your keyboard and see how the lexer responds. Try typing “int” or typing some words, like “hello” and “world”.
Appendix
All the commands in one block
mkdir -p 2140
cd 2140
curl https://blog.bornais.ca/2140/lex/Main.java -o Main.java
curl https://blog.bornais.ca/2140/lex/simple.lex -o simple.lex
mkdir -p JLex
mv Main.java JLex
cd JLex
javac Main.java
cd ..
java JLex.Main simple.lex
javac simple.lex.java
java MyLexer