student
Learning Goal: I’m working on a computer science project and need support to help me learn.
You must submit buildable .java files for credit.
This assignment must have six new source files (Parser.java, Node.java, IntegerNode.java, FloatNode.java, MathOpNode.java, Interpreter.java) as well as your first three source files (Shank.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 NUMBER TIMES NUMBER PLUS NUMBER. 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) and then matchAndRemove() a newLine. You must create 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 (NUMBER with a decimal point) or an IntegerNode (NUMBER without a decimal point) 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.
Finally, we will build (the beginning of) our interpreter. Create a new class called Interpreter. Add a method called “Resolve”. It will take a Node as a parameter and return a float. For now, we will do all math as floating point. The parser handles the order of operations, so this function is very simple. It should check to see what type the Node is:
For FloatNode, return the value.
For IntNode, return the value, cast as float.
For MathOpNode, it should call Resolve() on the left and right sides. That will give you two floats. Then look at the operation (plus, minus, times, divide) and perform the math.
Make sure that you test your code. Change your main to instantiate a parser (passing in the tokens from the lexer) and then 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. Then add a call to Resolve() and check the math.
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 (15)
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)
Resolve Method
None (0)
Correct(5)
ATTACHMENTS
attachment_1
attachment_2
attachment_3
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.