首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
辅导data留学生编程、c/c++程序设计讲解 解析Java程序|解析C/C++编程
项目预算:
开发周期:
发布时间:
要求地区:
Parsing
The task will be to design and implement a parser and interpreter for a simple programming
language that can be used to control robots for a simple robot game. The RoboGame program is
written already; you need to add the parser and interpreter. RoboGame is a program for a simple game involving two robots moving in a 2D grid based world
that contains barrels of fuel. The goal of the "game" is survival - the winner is the robot that still has
fuel when the other one has run out. RoboGame
The robots start in opposite corners of the grid, and can move around the world. At each
step, a robot may move forward one step, turn left, right or completely around, or remain
where it is. The robots require fuel, and use some up on every step. Their fuel level is displayed by a
coloured arc that gets shorter as the fuel runs down. When the fuel level in one of the
robots reaches zero, the robot stops and the game ends. Barrels of fuel appear at random places in the world. A robot that is on top of a barrel can
take fuel from the barrel. A robot can also steal fuel from the other robot, if it is next to and facing the other, and the
other robot doesn't have its shield up. Using the shield costs extra fuel. Each robot is controlled by a program which determines its behaviour. The program may
be provided by the user --- if no program is provided the robot performs a built-in default
procedure, which constantly chases the closest barrel. Robot language
The robot language has commands directing the robot to perform various basic actions (move,turnL, etc) and test various sensors (fuelLeft, wallDist, etc). It also includes control structures (loops and
conditionals) and operators for calculating and comparing. A grammar for the full language is given
below. Note: In this grammar (and later ones): Uppercase terms are NON-TERMINALS, terminals are
enclosed in double quotes, [...] means optional, * means zero or more occurrences of the preceding
item, + means one or more occurrences of the preceding item, | is used to separate alternatives, ::=
separates the left and right hand sides of a definition, and VAR and NUM are defined by regular
expressions.
Notes: None of the actions require arguments, but move and wait can take an optional
argument. The conditions in if and while statements can involve comparisons of integer valued
expressions, or logical combinations of them using and, or and not.
Expressions specifying values (EXP) can be sensor values, actual numbers, variables, or
arithmetic expressions using add, sub, mul, or div. Expressions are written in a prefix/functional form (e.g. eq(barrelFB, 0) or add(5,1))
for ease of parsing. This will be replaced by infix expressions in the last part. Variable names must start with a $, and variables can have numeric values assigned to
them. The specification is a Java regular expression that matches variable names. Numbers are integers, with an optional -ve sign. The specification is Java regular
expression that matches numbers. The sensors oppLR, oppFB, barrelLR, and barrelFB return the position of the
opponent robot or the closest barrel, relative to the current position and direction of the
robot. LR means the distance to the left (-ve) or right (+ve) , FB means the distance in
front (+ve) or behind (-ve). If there are no barrels at
present, barrelLR and barrellFB will return a very large integer. The sensors barrelLR, and barrelFB both take an optional argument, as
in barrelLR(n) or barrelFB(n), in which case they refer to the nth closest barrel. Any amount of white space (blanks, newlines and tabs) may occur between two adjacent
terminals. Here is a program in this language, along with some comments:
TO DO: complete 4 stages
You need to write a parser and interpreter which can read and parse a robot program from a file, and then execute it. More specifically, you are to complete Parser.java by writing all the parse methods, and to define
classes for the different types of AST node, along with the methods in those classes (e.g. execute)
that define the interpreter.
It lays out a sequence of increasing subsets of the language which you should implement one at a
time. You should not attempt to build the parser for the whole language at once!
Stage 1:
you need to write a parser that can parse and execute a small subset of the language that has
actions and loops without conditions, given by the follow grammar:
The following is an example program for this stage:
You will need to define a node class for each of the non-terminals. It is also sensible to define a
node class for each of the actions (or perhaps use an enum). Each node class should have
an execute(Robot robot) method. The execute method for an action node will call the relevant
method from the Robot class on the given robot. For example, for the TurnLNode class, it might
be:
Note that the method name in the Robot class is not necessarily the same name as the command
in the robot language. The execute method for LoopNode will not call methods on the robot directly, but will repeatedly
call the execute method of the BlockNode that it contains. Similarly, the BlockNode will need to
call the execute method of each of its components in turn. The node classes should also have a toString method which returns a textual representation of
the node. The nodes corresponding to the PROG, STMT, LOOP and BLOCK rules will need to
construct the string out of their components. For example, the LoopNode class might have the
following method (assuming that block is a field containing the BlockNode that is contained in
the LoopNode):
Hint: There will be a lot of node classes. You can put each of them in a separate file, or you can
include them all in the Parser.java file as non-public classes, since they are only accessed by the
parser itself. It depends on your IDE which option is easier to handle. Test your parser on the example program above
1.Run the main method of the Parser class to check whether the parser parses programs
correctly. 2.Once they parse correctly, run the RoboGame and load the programs into the robots to see
whether the programs are executed correctly. The goal in stage 1:
Simple actions work. The robot can loop through a block of multiple actions. The parser throws errors on invalid strings. Stage 2:
you should extend your parser to handle the robot sensors, and IF and WHILE statements, as
shown in the following grammar. The conditions in IF and WHILE statements can be restricted to
simple comparisons of a sensor value with a number, e.g. lt(fuelLeft, 20) to determine whether
there are less than 20 units of fuel left (the robot starts with 100 units).
Here is an example program for this stage :
You will need additional node classes and parse methods for the IF, WHILE, COND, and SEN rules.
It is sensible to have a class for each of the comparisons (less than, greater than, and equal) and
for each of the sensors (fuelLeft, etc) --- again, an alternative would be to use enums for these. The execute methods for the IfNode and WhileNode will need to perform the logic of testing the
value of the condition in the node, and then executing the block in the node. Note that the condition nodes (Cond, LessThan, etc) are a different type
from RobotProgramNode since they do not need an execute method, but instead need
an evaluate method which takes a robot as an argument and returns a boolean value. You will
need to define an interface type for this category of node. The Sensor nodes are different again. Like the condition nodes, they need an evaluate method, but their evaluate method will return an int not a boolean. Their evaluate methods will need to
call the appropriate methods on the
robot: getFuel(), getOpponentLR(), getOpponentFB(), numBarrels(), getClosestBarrelLR()
or getClosestBarrelFB(). The goal in stage 2:
The extended list of actions work. Conditions return the correct value. Sensors return the correct value. If and while loops behave as expected. Stage 3:
you should extend your parser to handle: actions with optional arguments: move and wait can take an argument specifying how
many move or wait steps to take. if statements with optional else clauses
arithmetic expressions that compute values with sensors and numbers. more complex conditions with logical operators and expressions; comparisons between
any expressions, not just a sensor and a number. more restrictive form of integer constant, which does not allow leading zeroes. The grammar for this stage is:
Here is a program for this stage parser:
You will need to: Add node classes and parse methods to handle the expressions. Extend your parse methods for if statements to handle an optional else. After parsing the
condition and the "then" block, the method needs to check whether there is an "=else=" to
determine whether it needs to parse an else block or simply return the IfNode without an
else block. The execute method also needs to be extended. Extend your parse methods for the move and wait actions to check for an optional
argument. They should check for a "(" to determine whether there is an argument or not. The execute methods also need to be extended. Note that the Robot class does not
provide a move or idleWait method with an argument - your execute method needs to
call the move or idlewait method the specified number of times. The goal in stage 3:
The move and wait commands can now take an argument
If statements can take an else clause. Arithmetic expressions and boolean conditions work. Stage 4:
you should extend your parser to handle: variables and assignment statements. a sequence of elif elements in an if statement. optional arguments to barrelLR and barrelFB to access the relative position of barrels
other than the closest one. The grammar for this stage and an example program are given above, under Robot language. Variables are identifiers starting with a $, and can hold integer values. Assignment statements can
assign a value to a variable, and variables can be used inside expressions. Variables do not need
to be declared. If a variable is used in an expression before a value has been assigned to it, it is
assumed to have the value 0. The scope of all variables is the whole program. Evaluating an expression now needs to be able to access a map containing all the current variables
and their values, and an Statement needs to update the value of a variable in the map. If a variable
being accessed which is not in the map should be added and given the value 0. The Robot class provides four methods for accessing relative barrel
position: getClosestBarrelLR(), getClosestBarrelFB(), getBarrelLR(int
n) and getClosestBarrelFB(int n). The last two return the relative position of the nth closest
barrel, allowing the program to identify barrels other than the closest one. With these, you could
write robot programs that determine which barrel to aim for, if the opponent is already closer to the
closest barrel. The goal in stage 4:
Variables and assignments work. Conditionals can have any number of elifs. The barrel sensors can take an argument.
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!