急学校的作业,用JAVA写一个小编译器,无从下手,请帮忙100分对高手应该不难
8号就教了,那位高手出手相救一下,所需文件我放到http://cn.f2.pg.briefcase.yahoo.com/garyzhang1982
进去点击那个assignment的目录,进去之后还有个assignment,这个就是那个文件,点击下载就行了。下载后请发有件到我的邮箱zhygk@eyou.com
写了
Modify the recursive descent compiler for language J0 provided in ~comp3401/a1 on the student machines to include a null statement and a case statement (similar to a switch statement BUT different). You only need to modify file Parser.java. You should not modify any other files because we will be testing your implementation using the existing other files with your version of the Parser.java.
Read the fine print in detail! And when you think you have finished implementing the assignment, come back and reread the fine print here again.
The non-terminal ``Statement'' in the grammar for J0 is modified to the following
Statement -> WhileStatement | IfStatement | CallStatement |
Assignment | ReadStatement | WriteStatement | CompoundStatement |
NullStatement | CaseStatement
where
NullStatement -> SEMICOLON
CaseStatement -> KW_CASE LPAREN Exp RPAREN BEGIN Branch { Branch } [ DefaultPart ] END
Branch -> Label COLON Statement
DefaultPart -> KW_DEFAULT Statement
Label -> IDENTIFIER | INTEGER
The following lexical tokens have been added to the scanner module of J0 already. Keywords are not case sensitive.
KW_CASE -> "CASE"
KW_DEFAULT -> "DEFAULT"
Static Semantic Restrictions
For the case statement, the type of the expression and all the labels must be integer. The labels are either integer literals or identifiers defined as integer constants (via a CONST definition).
Each label must be unique within a single case statement.
Dynamic Semantics
The null statement does nothing.
The case statement evaluates its (integer) expression and then selects the branch for which the value of the expression is equal to the label on that branch and executes the corresponding statement. If the value of the expression is not equal to any of the labels of any of the branches then the default branch is executed, or if there is no default branch the programs stops (using instruction STOP).
On completion of the execution of the appropriate branch, the next statement executed is the one following the whole case statement. [That is, it is NOT like a switch statement in which execution of one branch flows into the next branch unless a break statement is placed at the end of the first branch -- a crudy language design if ever there was one.]
For example,
case( i ) {
2: y = 1;
3: y = x;
5: y = x*x;
default y = 42;
}
Object Code
The J0 compiler generates code for the Itty Bitty Stack Machine. The code generation is done in Tree.java. We have already added appropriate tree nodes along with their code generation methods. Hence all you have to do is to generate the appropriate tree structures for the null and case statements. The following explains how the supplied code generation works.
The code generation for the null statement is trivial!
For the case statement it is expected that, although there may be a large number of labelled branches, that the set of all the labels is relatively compact. This means that a branch table can be used to implement the case statement. A branch table is an array indexed by labels (integers) of addresses of the start of the code for the corresponding branch. For the above example, the array would have entries corresponding to the labels 2, 3, 4, and 5, and each entry would have the address of the code for the corresponding branch; the entry corresponding to the label 4 would be for the default branch. The value of the expression i minus the minimum label value (2 for the above example) is calculated as the table index. If the index is negative or greater than 3 (i.e., the maximum label minus the minimum label) the default branch should be taken. Otherwise the index is used to access the corresponding table entry and a branch made to the address in that table entry. Note that the table is placed directly within the code.
In generating the code there are situations when one needs to generate forward branches to locations in the code that have not yet been determined (because the size of yet to be generated code is not known). To handle these a list of branches is built up (actually it is embedded in the generated code) for each target branch location, and when that location is finally known all the branch instructions are patched branch to the correct location. The stack machine code generation routines have a facility for this using patchList. The current IfStatement makes simple use of this to patch up the forward branches, and it is also used to patch up a chain of branches as required for the case statement. To handle a list of branches to be patched, the first branch is generated with the address 'code.NULL_ADDR'. The methods genJump* return the location used to store the branch address; this location is passed as the address field for the subsequent branch instruction, and so on, so that the branch instruction addresses form a list through the code. The method patchList -- the two parameter version -- will patch all these branch locations to branch to the current location (the location into which the next instruction generated will be placed).
The stack machine has an instruction DUPE that duplicates the top of stack. A copy of the description of the stack machine (as handed out in week 1) is available on the web. For the details about how instructions work and about instruction-level code generation methods see the file StackMachine.java.
If a compilation is successful, the compiler normally runs the interpreter to execute the generated code. See the compiler switches (documented at the start of J0.java, or by running java J0 -h).
Files
All sources for assignment 1 are in the directory ~comp3401/a1 on the student Unix machines. We (strongly) suggest that you create a new directory for this assignment and copy all files from ~comp3401/a1 to that directory using the provided shell script SETUP (in that directory), i.e., create your assignment directory, cd into it and then execute ~comp3401/a1/SETUP. There is a file called README that contains a brief description of the files. You can then make J0 with the command:
make
and run it with a command of the form:
java J0 testfile
where testfile contains the test program to be compiled. There are a few test files supplied, but you should develop your own set of test cases to thoroughly test your compiler.