collection of Tokens. We will be treating the collection of tokens as a queue - taking off the front. It isn't necessary to use a Java Queue, but you may. We will add three helper functions to parser. These should be private: matchAndRemove - accepts a token type. Looks at the next token in the collection: If the passed in token type matches the next token's type, remove that token and return it. If the passed in token type DOES NOT match the next token's type (or there are no more tokens) return null. expectEndsOfLine - uses matchAndRemove to match and discard one or more ENDOFLINE tokens. Throw a SyntaxErrorException if no ENDOFLINE was found. peek - accepts an integer and looks ahead that many tokens and returns that token. Returns null if there aren't enough tokens to fulfill the request. Parser - parse Make sure to make a public parse method. There are no parameters, and it returns Node. This will be called from main once lexing is complete. For now, we are going to only p
Make sure to make a Parser class (does not derive from anything). It must have a constructor that accepts your collection of Tokens. We will be treating the collection of tokens as a queue - taking off the front. It isn't necessary to use a Java Queue, but you may.
We will add three helper functions to parser. These should be private:
matchAndRemove - accepts a token type. Looks at the next token in the collection:
If the passed in token type matches the next token's type, remove that token and return it.
If the passed in token type DOES NOT match the next token's type (or there are no more tokens) return null.
expectEndsOfLine - uses matchAndRemove to match and discard one or more ENDOFLINE tokens. Throw a SyntaxErrorException if no ENDOFLINE was found.
peek - accepts an integer and looks ahead that many tokens and returns that token. Returns null if there aren't enough tokens to fulfill the request.
Parser - parse
Make sure to make a public parse method. There are no parameters, and it returns Node. This will be called from main once lexing is complete. For now, we are going to only parse a subset of Shank V2 - mathematical expressions. Parse should call expression() then expectEndOfLine() in a loop until either returns null. Don't worry about storing the return node but you should print it out (using ToString()) for testing.
Below is the parser.java and attached is the rubric. Please fix all the errors in the parser.java. I really need it!!!!!!
Parser.java
package mypack;
import java.util.Queue;
public class Parser {
private Queue<Token> tokens;
public Parser(Queue<Token> tokens) {
this.tokens = tokens;
}
private Token matchAndRemove(TokenType type) {
if (tokens.peek() != null && tokens.peek().getType() == type) {
return tokens.remove();
}
return null;
}
private void expectEndOfLine() throws SyntaxErrorException {
if (matchAndRemove(TokenType.ENDOFLINE) == null) {
throw new SyntaxErrorException();
}
while (matchAndRemove(TokenType.ENDOFLINE) != null);
}
private Token peek(int n) {
if (tokens.size() <= n) {
return null;
}
return tokens.peek();
}
public Node parse() {
while (expression() != null) {
expectEndOfLine();
}
return null;
}
private Node expression() {
}
}
![Rubric
Comments
Variable/Function
naming
Node
IntegerNode
FloatNode
expectEndsOfLine
peek
parse
expression
matchAndRemove None (0)
term
Poor
factor
None/Excessive (0)
Single letters
everywhere (0)
None (0)
None(0)
None(0)
None (0)
None (0)
None(0)
none (0)
none (0)
none (0)
OK
"What" not "Why",
few (5)
Lots of abbreviations
(5)
handles 2 of parens,
negatives, integers
and floats (10)
Good
Some "what"
comments or missing
some (7)
Full words most of the
time (8)
Loops over
expression() and
expectEndsOfLine()
(5)
calls term, processes
1+/- (5)
calls factor, processes
1* or / or % (5)
handles 3 of parens,
negatives, integers
and floats (15)
Great
Anything not obvious has
reasoning (10)
Full words, descriptive (10)
Has ToString() and is
abstract (5)
Extends Node, has
constructor and private
member (5)
Extends Node, has
constructor and private
member (5)
returns matching next node
or null (5)
Matches multiple ends of
line and throws when it
doesn't (5)
Looks ahead, returns null
when it can't (5)
Loops over expression() and
expectEndsOfLine() and
prints results (10)
calls term, loops over +/-
(10)
calls factor, loops over * or /
or %6 (10)
handles parens, negatives,
integers and floats (20)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe7ddc10c-4670-40fd-b02c-6a60c5fcc2f2%2F2bf3a4fe-99ed-47fd-b417-1926ad9462a5%2F1141ciqq_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)