ICSI 311 Assignment 3 – Start the Parser
This assignment is extremely important – (nearly) every assignment after this one uses this one!
If you have bugs or missing features in this, you will need to fix them before you can continue on to new assignments. This is very typical in software development outside of school.
You must submit .java files. Any other file type will be ignored. Especially “.class” files.
You must not zip or otherwise compress your assignment. Blackboard will allow you to submit multiple files.
You must submit buildable .java files for credit.
This assignment must have five new source files (Parser.java, Node.java, IntegerNode.java, FloatNode.java, MathOpNode.java) as well as your first three source files (Basic.java, Token.java, Lexer.java).
The parser will take the collection of tokens from the lexer and build them into a tree of AST nodes. To start this process, make an abstract Node class. Add an abstract ToString override. Now create an IntegerNode class that derives from Node. It must hold an integer number in a private member and have a read-only accessor. Create a similar class for floating point numbers called FloatNode.java. Both of these classes should have appropriate constructors and ToString() overrides.
Create a new class called MathOpNode that also derives from Node. MathOpNode must have an enum indicating which math operation (add, subtract, multiply, divide) the class represents. The enum must be read-only. The class must have two references (left and right) to the Nodes that will represent the operands. These references must also be read-only and an appropriate constructor and ToString() must be created.
Reading all of this, you might think that we can just transform the tokens into these nodes. This would work, to some degree, but the order of operations would be incorrect. Consider 3 * 5 + 2. The tokens would be INTEGER TIMES INTEGER PLUS INTEGER. That would give us MathNode(*,3,MathNode(+,5,2)) which would yield 21, not 17.
Create a Parser class (does not derive from anything). It must have a constructor that accepts your collection of Tokens. Create a public parse method (no parameters, returns “Node”). Parse must call expression (it will do more later). You must creating some helper methods as matchAndRemove() as described in lecture.
The classic grammar for mathematical expressions (to handle order of operations) looks like this:
EXPRESSION = TERM { (plus or minus) TERM}
TERM = FACTOR { (times or divide) FACTOR}
FACTOR = {-} number or lparen EXPRESSION rparen
Turn each of these (expression, term, factor) into a method of Parser. Use matchAndRemove to test for the presence of a token. Each of these methods should return a class derived from Node. Factor will return a FloatNode or an IntegerNode OR the return value from the EXPRESSION. Note the unary minus in factor – that is important to bind the negative sign more tightly than the minus operation. Also note that the curly braces are “0 or more times”. Think about how 3*4*5 should be processed with these rules. Hint – use recursion and a helper method. Also think carefully about how to process “number”, since we have two different possible nodes (FloatNode or IntegerNode). Depending on how you implemented your lexer, factor may or may not need to deal with negating the number.
Make sure that you test your code. Change your main to call parse on the parser. Right now, it will only process a single line. Print your AST by using the “ToString” that you created. Use several different mathematical expressions and be sure that order of operations is respected. Your lexer can create tokens that your parser cannot handle yet. That is OK.
他的任务非常重要–(几乎)此任务之后的每一项任务都使用此任务!
如果您有错误或缺少功能,则需要对其进行修复,然后才能继续执行新任务。这在校外软件开发中非常典型。
您必须提交.java文件。任何其他文件类型将被忽略。特别是“ .class”文件。
您不得压缩或以其他方式压缩您的作业。黑板将允许您提交多个文件。
您必须提交可构建的.java文件以获取信用。
此分配必须具有五个新的源文件(Parser.java,Node.java,IntegerNode.java,FloatNode.java,MathOpNode.java)以及前三个源文件(Basic.java,Token.java,Lexer.java) 。
解析器将从词法分析器中获取令牌集合,并将其构建到AST节点树中。要开始此过程,请创建一个抽象的Node类。添加一个抽象的ToString覆盖。现在创建一个从Node派生的IntegerNode类。它必须在私有成员中包含一个整数,并且具有只读访问器。为浮点数创建一个类似的类,称为FloatNode.java。这两个类都应具有适当的构造函数和ToString()重写。代写ICSI 311编程、代做java程序实验
创建一个新的名为MathOpNode的类,该类也从Node派生。 MathOpNode必须具有一个枚举,指示该类表示哪个数学运算(加,减,乘,除)。枚举必须是只读的。该类必须对表示操作数的节点有两个引用(左右两个)。这些引用也必须是只读的,并且必须创建适当的构造函数和ToString()。
阅读所有这些内容,您可能会认为我们可以将令牌转换为这些节点。在某种程度上,这是可行的,但是操作顺序将是不正确的。考虑3 * 5 +2。令牌将是INTEGER TIMES INTEGER PLUS INTEGER。这将使我们得到MathNode(*,3,MathNode(+,5,2)),其结果为21,而不是17。
创建一个Parser类(不派生任何东西)。它必须具有一个接受令牌收集的构造函数。创建一个公共解析方法(无参数,返回“ Node”)。解析必须调用表达式(稍后将执行更多操作)。您必须按照讲义中的说明,将一些辅助方法创建为matchAndRemove()。
数学表达式(处理操作的顺序)的经典语法如下所示:
EXPRESSION = TERM {(加号或减号)TERM}
TERM = FACTOR {(倍数或除数)FACTOR}
FACTOR = {-}数字或lparen EXPRESSION rparen
将这些(表达式,项,因子)中的每一个都转换为解析器方法。使用matchAndRemove测试令牌是否存在。这些方法中的每一个都应返回派生自Node的类。 Factor将返回FloatNode或IntegerNode或EXPRESSION的返回值。请注意因数中的一元减号–与减号操作相比,更牢固地绑定负号非常重要。另请注意,花括号为“ 0次或多次”。考虑一下如何使用这些规则处理3 * 4 * 5。提示–使用递归和辅助方法。还要仔细考虑如何处理“数字”,因为我们有两个可能的节点(FloatNode或IntegerNode)。根据您实施词法分析器的方式,因素可能需要或可能不需要处理负数。
Rubric Poor OK Good Great
Comments None/Excessive (0) “What” not “Why”, few (5) Some “what” comments or missing some (7) Anything not obvious has reasoning (10)
Variable/Function naming Single letters everywhere (0) Lots of abbreviations (5) Full words most of the time (8) Full words, descriptive (10)
Create the AST classes None (0) Classes missing (5) All classes present, some methods missing (10) All classes and methods (20)
Parser class None (0) Constructor or private member (5) Constructor and private member (10)
Helper Method(s) None(0) At least 1 (5)
Factor Method None (0) Significantly Attempted (10) Correct (15)
Expression Method None (0) Significantly Attempted (10) Correct (15)
Term Method None (0) Significantly Attempted (10) Correct (15)
如有需要,请加QQ:99515681 或WX:codehelp
原文:https://www.cnblogs.com/liptython/p/14497008.html