Objectives.
1. Support multiline comments.
Supportadditional tokens (reserved words, operators, and separators).
Support long and double
In this project, you will only be updating the hand-crafted scanner, which means that the only program files you will be modifying under $j/j–/src/jminusminus are TokenInfo.java and Scanner.java.
Run the following command inside the $j directory to compile the j– compiler with your changes.
$ ant clean com pile jar
Run the following command to compile (just scan for now) a j– program P.java using the j– compiler.
$ sh $j /j - -/ bin /j - - -t P . java
which only scans P.java and prints the tokens in the program along with the line number where each token appears.
Problem 1. (Multiline Comment ) Add support for multiline comment, where all the text from the ASCII characters /* to the ASCII characters */ is ignored.
$j /j - -/ bin /j - - -t tests / M ultiL ineC om m ent . java
5 : public = public
5 : class = class
5 : < IDENTIFIER > = M ultiL ineC om m ent 5 : { = {
9 : public = public
9 : static = static
9 : void = void
9 : < IDENTIFIER > = main
9 : ( = (
9 : < IDENTIFIER > = S tring
9 : [ = [
9 : ] = ]
9 : < IDENTIFIER > = args
9 : ) = )
9 : { = {
13 : } = }
14 : } = }
15 : < EOF > = < EOF >
Problem 2. (Reserved Words) Add support for the following reserved words.
break case catch
continue default do
double final finally
for im plem ents interface
long sw itch throw
throw s try
$j /j - -/ bin /j - - -t tests / R eserved W ords . java
1 : break = break
1 : case = case
1: catch =catch
2: continue =continue
2 : default = default
2: do =do
3: double =double
3 : final = final
3: finally =finally
4: for =for
4 : im plem ents = im plem ents
4 : interface = interface
Problem 3. (Operators) Add support for the following operators.
? ~ != / /=
-= — *= =
>> > >= > > > > > >= >=
<< < <= < ^ ^=
| |= || & &=
$ $j /j - -/ bin /j - - -t tests / O perators . java 1 : ? = ?
1 : ~ = ~
1 : != = !=
1 : / = /
1 : /= = /=
2 : -= = -=
2 : -- = --
2 : *= = *=
2 : =
2 : = = =
3 : >> = >>
3 : > >= = > >=
3 : > > > = > > >
3 : > > >= = > > >=
3 : >= = >=
4 : << = << 4 : < <= = < <= 4 : < = < 4 : ^ = ^ 4 : ^= = ^= 5 : | = | 5 : |= = |= 5 : || = || 5 : & = & 5 : &= = &= 6 : < EOF > = < EOF >
Problem 4. (Separators) Add support for the separator : (colon).
$ $j /j - -/ bin /j - - -t tests / S eparators . java 1 : ; = ;
2 : : = :
3 : , = ,
4 : . = .
5 : [ = [
5 : { = {
5 : ( = (
5 : ) = )
5 : } = }
5 : ] = ]
6 : < EOF > = < EOF >
Problem 5. (Literals) Add support for (just decimal for now) long and double literals.
< int_literal > = 0 | (1 -9) {0 -9} // decim al
< long_literal > = < int_literal > ( l | L )
< digits > = (0 -9) {0 -9}
< exponent > = ( e | E ) [(+ | -)] < digits >
< suffix > = d | D
< double_literal > = < digits > . [< digits >] [< exponent >] [< suffix >]
| . < digits > [< exponent >] [< suffix >]
| < digits > < exponent > [< suffix >]
| < digits > [< exponent >] < suffix >
$ $j /j - -/ bin /j - - -t tests / L iterals . java
1 : < IN T _LIT E R A L > = 0
1 : < IN T _LIT E R A L > = 2
1: < IN T _LIT E R A L > = 372
2:< L O N G _L IT E R A L > = 1996 l
2 : < L O N G _L IT E R A L > = 777 L
2:< D O U B L E _L IT E R A L > = .3 D
3:< D O U B L E _L IT E R A L > = 14
3 : < D O U B L E _L IT E R A L > = 6.022137 e +23
3:< D O U B L E _L IT E R A L > = 1e -9 d
4: < EOF > = < EOF>
Files to Submit
j–.tar.gz(j– source tree as a single gzip file)
txt (projectreport)
Before you submit:
Make sure you create the gzip file j–.tar.gzsuch that it only includes the source files and not the binaries, which
$ cd $j /j --
$ ant clean
$ cd ..
$ tar - cvf j - -. tar j - -/*
$ gzip j - -. tar
Makesure your report isn’t too verbose, doesn’t contain lines that exceed 80 characters, and doesn’t contain
1. Hours to complete the project: 24 hours
2. Provide a short description of how you approached each problem, issues you
encountered, and how you resolved those issues.
Report text:
1. Hours to complete the project: 24 hours
2. Provide a short description of how you approached each problem, issues you
encountered, and how you resolved those issues.
Part I (Additions to JavaCC Scanner)
Problem 1 (Multiline Comment):
Problem 1 just add a skip comment as single-line skip comment to skip the
multiline comment in j--.jj.
(SKIP: {})
Problem 2 (Reserved Words):
Problem 2 just add some reserved words in the Token at j--.jj. Please pay
attention to the "default", you can just use DEFAULT to identify it,
because lexical token name "DEFAULT" is the same as that of a lexical
state. So I just change "DEFAULT" to "DEFAULt" (lower-case T).
Problem 3 (Operators):
In Problem 3, I define the operators in Token first, then define them in
different expression method, for instance, "/=" should be define in
project expression, and "||" should be define in Conditional-Or-
Expression. Just like what we did in Parser.
Problem 4 (Separators):
In Problem 4, I define the colon separator in Token at j--.jj first, and
then define it in conditionalExpression method.
Problem 5 (Literals):
In Problem 5, I define the double and long literals in Token at j--.jj
according to the format that problem 5 provided.
Part II (Additions to JavaCC Parser)
Problem 6 (Long and Double Basic Types):
For problem 6, I wrote the java code for double and long literals. Then,
I defined their types in basictype().
Problem 7 (Operators):
In j--.jj, I defined the operators in different expression method. For
instance, "/=" should be define in project expression, and "||" should
be define in ConditionalOrExpression. Just like what we did in Parser.
Problem 8 (Conditional Expression):
In j--.jj, I created a new method called conditionalExpression and
defined them in this method. In conditionalExpression method, project-
Expression() will be called and check the colon token is there, then call
conditionalExpression to get the conditional expression and return JCon-
ditionalExpression object.
Problem 9 (Switch Statement):
In j--.jj, I created a JSwitchBlockStatement J-class to represent the
switch block, and also created the J-class JSwitchStatement for the switch
statement. I defined the "switch" in the statement(). Then I created the
switchBlockStatementGroup() for JSwitchBlockStatement and switchLabel() to
check the expression.
Problem 10 (Do-While Statement):
In problem 10, I added a do statement with while statement in statement()
at j--.jj.
Problem 11 (For Statement):
In problem 11, I followed what I did in parser to added the for statement
in statement(). Please be careful that you should update the statement
expression so that an expression can be an instanceOf a pre-decrement or
post-increment object.
Problem 12 (Exception Handlers):
I wrote some J-class. JThrowStatement is for the throw statement. JCatch is
for the try statement and JCatch is for the catch statement. And in the
statement(), they all represent exception handlers so I do not separated
them. Moreover, I also defined throw in member declaration and interface-
member-declaration.
Problem 13 (Interface Type Declaration):
In j--.jj, I defined the interface in typeDeclaration method. Then I
created the interfaceDeclaration() and interfaceBody() method as what
I did in parser. And I also defined the implement in class declaration
and interface declaration.
3. Did you receive help from anyone? List their names, status (classmate,
CS451/651 grad, TA, other), and the nature of help received.
Name Status Help Received
---- ------ -------------
... ... ...
4. List any other comments here. Feel free to provide any feedback on how
much you learned from doing the project, and whether you enjoyed
doing it.
The 4 is tough for me. It is based on the completed 3,
and do the same things in j--.jj as what you did in parser. However,
I did not finish the 3 very well so I spent a lot of time to do
this project and fixed the works.
北美cs案例:java案例 编译原理Principle of Compiler 网课案例hand-c
2018-10-12