Due: .
Do the programming exercise "Lexical Analysis", from Appel, 2nd, page 34. Turn in phase02.jar containing your Java source code (no class files) and the scanner input written for JavaCC. Create an executable jar file named scan.jar. (See the project overview document.) The lexer or scanner performs lexical analysis of MiniJava programs. Your scanner must not terminate abnormally or raise an exception regardless of the input. Your scanner must print one and only one line of output to the standard output stream. This line should report the number of errors reported (possibly 0) and have exactly the following form:
filename=./path/Error02.java, errors=3
If the number of errors in the input is greater than zero, your scanner must issue error messages in the format below. The message contains the file name, the line number, the character position, and a good error message. Only printable US-ASCII characters are allowed in error messages.
./path/Error02.java:001.011: ERROR -- illegal character & ./path/Error02.java:001.014: ERROR -- illegal character ^ ./path/Error02.java:002.002: ERROR -- illegal character \In this example, three errors were detected and three error message are written to the standard error stream. The number of errors and the errors messages themselves are not specified.
Standard error message formats are useful. Please use the format above for all error messages. If three errors are reported in the standard input stream, then three error messages must be in the standard output stream.
Following the format for this and all the compiler milestones allows me to detect exactly what your compiler determined.
The scanner for MiniJava may detect just one kind of error: illegal character. Other languages might have other lexical errors. For example, scanners for a langauge with strings might detect an unterminated strings. However, an unterminated comment in MiniJava /* ... may just be an illegal slash character. Note that all characters are legal in a comment including those that would otherwise be illegal outside a comment.
We will use JavaCC. You will need to consult the JavaCC documentation. Javacc 5.0 (not 6 ror 7) is installed on andrew.cs.fit.edu.
Futhermore, all output (in the standard output and standard error streams), must be in human-readable (i.e., printable) US-ASCII characters. Even if the input contains non-printable US-ASCII characters, the output must not.
In Java, comments do not nest. You do not have to implement nested comments in MiniJava (despite what Appel says on page 484). You may use nested comments if you wish, as that is an interesting exercise.
A Mini-Java identifier cannot be a Java keyword like: if, goto, or the Java literals: true, false, or null. The definition of an identifier in Java is found here 3.8 Identifiers
Java octal dec hex name HT \t 011 9 0x09 horizontal tab LF \n 012 10 0x0A line feed FF \f 014 12 0x0C form feed CR \r 015 13 0x0D carriage return SP 040 32 0x20 spaceMini-Java does not permit the ASCII, control character "sub" at the end of the program file (except in comments).
Of particular interest is the definition of a newline, because, without it, we cannot agree on line numbers for errors. A line may be determined by the underlying operating system. This is so ugly we might be tempted to write a pre-scanner that takes care of this. Instead we will define a newline to be the line-feed character (the way Unix does). Thus, the number of lines in a program is the number of line-feed characters (if the program ends in a line feed). Since carriage-return characters are ignored, DOS/Windows files will work in the expected fashion. Unlike Java we do not accept CR alone as a line terminator.
Be careful with the regular expression '.' --- it may not match \n or \r.
int,
a 32 bit signed integer.
Mini-Java does not permit binary, octal, or hexadecimal interer literals.
We do not require underscores be permited in integer literals, but it is easy enough to do what Java does.
I will build your project like this:
jar -xf phase02.jar find . -name '*.class' -delete # Assumes Makefile in current directory make # Assume scan.jar created in current directoryYour makefile may assume javacc is in the execute path. And I will test your project like this:
java -jar dir1/scan.jar dir2/test01.java java -jar dir1/scan.jar dir2/test02.java java -jar dir1/scan.jar dir2/test03.java
If test01.java contains no errors, then "errors=0" must appear in the one and only line of output to standard output. If test01.java contains errors, then "errors=x" must appear in the one and only line of output to standard output, where x is a positive number.
JAVAC := javac
JAVACC:= javacc
default : compiler
compiler : scan.jar
# options: c--create; f--name of jar file; e--entry piont
scan.jar :
$(JAVAC) */*.java
jar cfe $@ main.Scan error/*.class parser/*.class main/*.class
clean :
-/bin/rm *~ */*~
-/bin/rm */*.class
Makefiles must have tabs, and must end in \n not \n\r!
PARSER_BEGIN(ExampleScanner)
package parser;
public class ExampleScanner {}
PARSER_END(ExampleScanner)
TOKEN : {
< IF: "if" >
| < ELSE: "else" >
| < GT: ">" >
| < INVALID: ~[" ", "\t", "\n", "\b", "\f", "\r"] >
}
SKIP : {
<"//" (~["\n", "\r"])* ("\n" | "\r" | "\r\n")>
| " "
}
javacc -OUTPUT_DIRECTORY=path/dir file.jj
This is important if you put your parser in a Java package.
final MiniJavaScanner lexer = new MiniJavaScanner (new FileInputStream (filename)); if (trace) lexer.enable_tracing(); else lexer.disable_tracing(); // for parsing only final Token t = lexer.getNextToken ();
import static javacc.[scanner]Constants.EOF; final Token t = lexer.getNextToken (); if (t.kind=EOF) // ...
> java -jar scan.jar ../../minijava/appel/Factorial.java filename=../../minijava/appel/Factorial.java, errors=0Your program must print exactly one line to the standard output. (Error messages get printed to the standard error stream.)
> java -jar scan.jar ../../minijava/appel/Factorial.java filename=../../minijava/appel/Factorial.java, errors=0Note: debug/verbose output should be sent to a file, not the standard output stream nor the standard error stream. We specify that the standard output stream should have exactly one line in it. We specify that the standard error stream should have error messages (if any) and nothing else. So, there is no other place for debug/verbose output to go.
The verbose output for the scanner might look like this:
../../minijava/appel/Factorial.java:001.001: CLASS "class"
../../minijava/appel/Factorial.java:001.007: IDENTIFIER "Factorial"
../../minijava/appel/Factorial.java:001.016: LeftBrace "{"
../../minijava/appel/Factorial.java:002.005: PUBLIC "public"
../../minijava/appel/Factorial.java:002.012: STATIC "static"
* * *
../../minijava/appel/Factorial.java:014.027: MULT "*"
../../minijava/appel/Factorial.java:014.029: LeftParen "("
../../minijava/appel/Factorial.java:014.030: THIS "this"
../../minijava/appel/Factorial.java:014.034: DOT "."
../../minijava/appel/Factorial.java:014.035: IDENTIFIER "ComputeFac"
../../minijava/appel/Factorial.java:014.045: LeftParen "("
../../minijava/appel/Factorial.java:014.046: IDENTIFIER "num"
../../minijava/appel/Factorial.java:014.049: MINUS "-"
../../minijava/appel/Factorial.java:014.050: INTEGER_LITERAL "1"
../../minijava/appel/Factorial.java:014.051: RightParen ")"
../../minijava/appel/Factorial.java:014.052: RightParen ")"
../../minijava/appel/Factorial.java:014.054: SEMICOLON ";"
../../minijava/appel/Factorial.java:015.009: IDENTIFIER "return"
../../minijava/appel/Factorial.java:015.016: IDENTIFIER "num_aux"
../../minijava/appel/Factorial.java:015.024: SEMICOLON ";"
../../minijava/appel/Factorial.java:016.005: RightBrace "}"
../../minijava/appel/Factorial.java:018.001: RightBrace "}"
../../minijava/appel/Factorial.java:018.002: EOF
import java.util.TreeMap; import java.lang.reflect.Field; public final class FieldNames extends TreeMap{ public FieldNames (final String class_name) { try { final Class c = Class.forName (class_name); final Field [] f = c.getDeclaredFields(); for (int i=0; i<f.length; i++) { if (f[i].getType()==int.class) put (f[i].getInt(null), f[i].getName()); } } catch (final Exception e) { e.printStackTrace (System.err); } } }
Please turn in one Java jar file named phase02.jar using the submit server.
For example, I expect you to submit something approximately like the following list of files:
All submitted files must be encoded in US-ASCII characters to avoid unintended misinterpretation of the transmitted files.
The submit project tag for this assignment is asgn3. For your convenience, here is a submission form for this assignment.