急学校的作业,用JAVA写一个小编译器,无从下手,请帮忙100分对高手应该不难

zlly20 2004-04-03 02:07:15
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.

...全文
51 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wojiaoliufeng 2004-04-05
  • 打赏
  • 举报
回复
大家还是不要骂他
他不是懒只是想大家帮帮忙啊,也是一个好的学习机会啊
其实人人都有遇到困难时候
前天我在csdn上的一个朋友说在delphi做一个数据库的时候遇到问题
我利用一个星期仅有的昨天下午休息时间帮他写了个简单例子
其实我们在写程序的时候或多或少都有很多麻烦的
现在我改学java了,在学struts的时候我好希望有一个helloworld的程序作为我 的指引
有个不认识的网友给我发了一个。我终于完成我的作业了
很感谢他
所以说,不要总是批评人嘛?给人一个helloword就等于给了自己一个world啊
cctvnet 2004-04-05
  • 打赏
  • 举报
回复
自己动手,丰衣足食
chrisjen 2004-04-05
  • 打赏
  • 举报
回复
upppppppppppppppppppppppppp
runnersun 2004-04-05
  • 打赏
  • 举报
回复
gz
goldenhua 2004-04-05
  • 打赏
  • 举报
回复
javacc
fjirie 2004-04-05
  • 打赏
  • 举报
回复
easter 怎么过啊?
fjirie 2004-04-05
  • 打赏
  • 举报
回复
楼主老大 在 Queensland啊

我在Macqurie uni, 这学期都快死掉了~~~~~555

我们这里好难啊

anika 2004-04-05
  • 打赏
  • 举报
回复
wojiaoliufeng(木喜)
同志说的很好
不过这个太难了,不会
outrace 2004-04-05
  • 打赏
  • 举报
回复
我要学习!!!!!!!!!!!!!!!!!!!!!!
jonathan_yun 2004-04-04
  • 打赏
  • 举报
回复
帮你顶!!!
进一步学习!!!
zlly20 2004-04-04
  • 打赏
  • 举报
回复
我已做完了。发了这么个贴子,被骂的狗血喷头
fruitking 2004-04-04
  • 打赏
  • 举报
回复
自己写吧

多问问同学
zhang21cnboy 2004-04-04
  • 打赏
  • 举报
回复
我从来不给垃圾帮忙
vongood 2004-04-04
  • 打赏
  • 举报
回复
gz
zlly20 2004-04-03
  • 打赏
  • 举报
回复
老兄还说风凉话!能帮忙吗
NewTypeQ 2004-04-03
  • 打赏
  • 举报
回复
现在的大学生
sigh~~

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧