Java Reference
In-Depth Information
}
publicvoidtestDivide(){
this.assertEquals(division.divide(0,42),0);
this.assertEquals(division.divide(42,1),42);
this.assertEquals(division.divide(127,3),42);
}
}
Now that we have a test case for the division operator, we must register it with
the j-- test suite by making the following entry in the suite() method of junit.
JMinusMinusTestRunner .
TestSuitesuite=newTestSuite();
...
suite.addTestSuite(DivisionTest.class);
returnsuite;
j-- supports only int as a numeric type, so the division operator can operate only on
ints . The compiler should thus report an error if the operands have incorrect types; to test
this, we add the following fail test Division.java and place it under the $j/j--/tests/
fail folder; fail is a package.
packagefail;
importjava.lang.System;
publicclassDivision{
publicstaticvoidmain(String[]args){
System.out.println('a'/42);
}
}
Changes to Lexical and Syntactic Grammars
Appendix B specifies both the lexical and the syntactic grammars for the j-- language;
the former describes how individual tokens are composed and the latter describes how
these tokens are put together to form language constructs. Chapters 2 and 3 describe such
grammars in great detail.
The lexical and syntactic grammars for j-- are also available in the files $j/j--/
lexicalgrammar and $j/j--/grammar , respectively. For every language construct that is
newly added to j--, we strongly recommend that these files be modified accordingly so that
they accurately describe the modified syntax of the language. Though these files are for
human consumption alone, it is a good practice to keep them up-to-date.
For
the
division
operator,
we
add
a
line
describing
the
operator
to $j/j--/
lexicalgrammar under the operators section.
DIV::="/"
where DIV is the kind of the token and "/" is its image (string representation).
Because the division operator is a multiplicative operator, we add it to the grammar
rule describing multiplicative expressions in the $j/j--/grammar le.
multiplicativeExpression::=unaryExpression//level2
{(STAR|DIV)unaryExpression}
The level number in the above indicates operator precedence. Next, we discuss the
changes in the j-- codebase to get the compiler to support the division operation.
 
Search WWH ::




Custom Search