SableCC - Lexical Analysis Tutorial v2 PDF
SableCC - Lexical Analysis Tutorial v2 PDF
Follow the steps below to see how a scanner for lexical analysis can be automatically generated
by SableCC.
1. Check where JAVA JDK is installed in your computer. Copy the path for the bin folder.
Example: C:\Program Files (x86)\Java\jdk1.7.0_51\bin
2. Right-click on “My Computer” and choose Properties. Click “Advanced System Settings”.
3. Click on “Environment Variables” button
4. Under “System Variables” frame, scroll to “Path” entry and click “Edit” button.
5. Copy the whole “Variable value” onto Notepad. At the end of the value, add a semicolon
(;) and paste your Java bin folder to the value.
6. Copy the edited value and paste on the Variable value on Path. Save everything and
close.
7. Open command prompt. Type “javac” and enter. If the path has been successfully set,
Java options will appear.
8. Download SableCC from iLearn. Unzip the folder to any location on your computer. Copy
the path to “bin” folder of SableCC. Example: C:\Users\User\Google
Drive\Documents\CSC569 - Principle of Compilers\SableCC\sablecc-3.7\bin
9. Create the environment variable for SableCC using the same instructions as above.
B. Create the Java class definition from Grammar
1. Turn to section 2.4.1.4 : An Example of a SableCC Input File on your textbook. Copy the
text from Package lexing.. to ..// of one of the above tokens. Paste the copied text to
Notepad and name the file as “lexing.grammar”. NOTE: Make sure the “Save as
type” is chosen as “All Files”. Save the file in the Sablecc folder of your computer.
NOTE: For SableCC compliance, at the very end of the file you must put a new line
before saving.
2. Browse to the sablecc folder in your computer. Use the “cd” command.
3. Type this command on your command prompt: sablecc lexing.grammar. Press Enter.
(After Enter is pressed)
4. If your compilation is successful, a new folder “lexing” will be created in the sablecc
folder.
C. Generate Scanner to compile Java classes
1. Turn to section 2.4.2: Running SableCC on your textbook. Copy the whole java
command under the section and save it as “Lexing.java”. Save the file in the newly
created lexing folder.
2. Compile the source files from the top directory by using this command: javac
lexing/*.java. If the compilation is successful, under the lexing folder there should be a
newly created file: Lexing.class
1. To use the scanner, first run it using this command: java lexing.Lexing
2. Type any line of Java example: y = y+3; to the command line and press Enter. The
scanner will do a lexical analysis of your lines and produce as output a stream of
tokens.