Homework 2
Lexical Analysis
|
|
The text examines two programs that generate Java lexical analyzers:
JavaCC and SableCC. Examples from the text for each generator will be
given with instructions on their use.
Complete the lexical-analysis part of the JavaCC and SableCC specifications for MiniJava
grammar; that is define the tokens for MiniJava. The tokens are
bold-faced in the grammar of section A.2 along with the Lexical Issues
described in section A.1 of the appendix. One simiplication we'll make is that
/* */ comments cannot be nested.
For JavaCC do only those parts necessary for the Sample.java test program.
For SableCC do the complete MiniJava grammar.
- Appendix A of the text describes MiniJava syntax, which can be compiled
by Java. The lexical part is the bold-face keywords, identifiers, integers,
comments, binary operators and special characters (e.g. '{', '!', '.',
etc.). For example, given the string "a=123;" the analysis would produce
something similar to: <IDENTIFIER><EQUAL><INTEGER_LITERAL><SEMICOLON>
in JavaCC while SableCC would echo back the characters of the tokens.
- Test using the sample
program.
Download the following files:
- Program2.9.jj - JavaCC definitions from text
p. 31. Note that this is not MiniJava but can serve as an example and
starting point.
- Program2.10.js - SableCC definitions from text p.
32. Note that this is not MiniJava but can serve as an example and starting
point.
- Main.java - Calls the SableCC
lexical analyzer.
- Lexical 1, 2,
3 -
lexical test programs. Lexical1 and Lexical2 are correct for the
definitions, Lexical3 has errors.
- Sample.java - Sample MiniJava program.
- Home:
IUS:
- Enter in the DOS Command window:
v:\common\user\C431\CC
v:\common\user\C311\forjava
- JavaCC
- Download Program2.9.jj and
Lexical1.txt.
- Generates lexical analyzer named MyParser.java, assumes
JavaCC is a root-level directory.
JavaCC Program2.9.jj
- Compile all Java files in directory.
javac -classpath . *.java
- Analyze the contents of file Lexical1.
java -cp . MyParser < Lexical1.txt
- SableCC
- Download Program2.10.js and
Lexical1.txt.
- To generate the SableCC lexical analyzer:
Home: java -jar \sablecc-2.18.2\lib\sablecc.jar Program2.10.js
IUS: java -jar
v:\common\user\C431\sablecc.jar Program2.10.js
- Download Main.java to the
sub-directory named Program2.
- To delete an older version of the analyzer and compile the Java program named
Program2\Main.java enter:
del Program2\Main.class
javac -classpath . Program2\Main.java
- To perform the lexical analysis of Lexical1:
java -cp . Program2.Main < Lexical1.txt
Turn In
- Cover sheet with your name, C431, date, and Homework 2.
- Print out of JavaCC and SableCC specification file for MiniJava.
- Screen shot listing of Command prompt window showing generation and analysis
of Sample.java program for JavaCC and SableCC.
Resources
SableCC
JavaCC