Parser - setup 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 file. There are errors in the parser.java code. Please fix those errors in the parser.java and make sure the parser works correctly. Attached is the rubric.   package mypack; import java.util.List; import mypack.Token.TokenType;   public class Parser { private List tokens; private int currentTokenIndex;   public Parser(List tokens) { this.tokens = tokens; this.currentTokenIndex = 0; } public Node parse() throws SyntaxErrorException { Node result = null; while (peek() != null) { Node expr = expression(); expectEndOfLine(); System.out.println(expr.toString()); } return result; } private Node expression() { Node result = term(); while (peek() != null && (peek().getType() == TokenType.PLUS || peek().getType() == TokenType.MINUS)) { Token token = matchAndRemove(peek().getType()); Node right = term(); if (right == null) { throw new SyntaxErrorException("Expected right operand after " + token.getValue()); } result = new MathOpNode(token.getType(), result, right); } return result; } private Node term() { Node result = factor(); while (peek() != null && (peek().getType() == TokenType.TIMES || peek().getType() == TokenType.DIVIDE || peek().getType() == TokenType.MOD)) { Token token = matchAndRemove(peek().getType()); Node right = factor(); if (right == null) { throw new SyntaxErrorException("Expected right operand after " + token.getValue()); } result = new MathOpNode(token.getType(), result, right); } return result; } private Node factor() { Token token = matchAndRemove(TokenType.MINUS); Node result = null; if (peek() != null) { if (peek().getType() == TokenType.NUMBER) { Token numberToken = matchAndRemove(TokenType.NUMBER); if (token != null) { result = new IntegerNode(-Integer.parseInt(numberToken.getValue())); } else { result = new IntegerNode(Integer.parseInt(numberToken.getValue())); } } else if (peek().getType() == TokenType.IPAR) { matchAndRemove(TokenType.IPAR); result = expression(); expect(TokenType.RPAR); } } return result; } private Token matchAndRemove(TokenType type) { if (peek() != null && peek().getType() == type) { currentTokenIndex++; return tokens.get(currentTokenIndex - 1); } return null; } private void expect(TokenType type) throws SyntaxErrorException { Token token = matchAndRemove(type); if (token == null) { throw new SyntaxErrorException("Expected " + type.toString()); } } private void expectEndOfLine() throws SyntaxErrorException { expect(TokenType.ENDOFLINE); } private Token peek() { return currentTokenIndex < tokens.size() ? tokens.get(currentTokenIndex) : null; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

Parser - setup

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 file. There are errors in the parser.java code. Please fix those errors in the parser.java and make sure the parser works correctly. Attached is the rubric.

 

package mypack;


import java.util.List;

import mypack.Token.TokenType;

 

public class Parser {

private List<Token> tokens;

private int currentTokenIndex;

 

public Parser(List<Token> tokens) {

this.tokens = tokens;

this.currentTokenIndex = 0;

}

public Node parse() throws SyntaxErrorException {

Node result = null;

while (peek() != null) {

Node expr = expression();

expectEndOfLine();

System.out.println(expr.toString());

}

return result;

}

private Node expression() {

Node result = term();

while (peek() != null && (peek().getType() == TokenType.PLUS || peek().getType() == TokenType.MINUS)) {

Token token = matchAndRemove(peek().getType());

Node right = term();

if (right == null) {

throw new SyntaxErrorException("Expected right operand after " + token.getValue());

}

result = new MathOpNode(token.getType(), result, right);

}

return result;

}

private Node term() {

Node result = factor();

while (peek() != null && (peek().getType() == TokenType.TIMES || peek().getType() == TokenType.DIVIDE || peek().getType() == TokenType.MOD)) {

Token token = matchAndRemove(peek().getType());

Node right = factor();

if (right == null) {

throw new SyntaxErrorException("Expected right operand after " + token.getValue());

}

result = new MathOpNode(token.getType(), result, right);

}

return result;

}

private Node factor() {

Token token = matchAndRemove(TokenType.MINUS);

Node result = null;

if (peek() != null) {

if (peek().getType() == TokenType.NUMBER) {

Token numberToken = matchAndRemove(TokenType.NUMBER);

if (token != null) {

result = new IntegerNode(-Integer.parseInt(numberToken.getValue()));

} else {

result = new IntegerNode(Integer.parseInt(numberToken.getValue()));

}

} else if (peek().getType() == TokenType.IPAR) {

matchAndRemove(TokenType.IPAR);

result = expression();

expect(TokenType.RPAR);

}

}

return result;

}

private Token matchAndRemove(TokenType type) {

if (peek() != null && peek().getType() == type) {

currentTokenIndex++;

return tokens.get(currentTokenIndex - 1);

}

return null;

}

private void expect(TokenType type) throws SyntaxErrorException {

Token token = matchAndRemove(type);

if (token == null) {

throw new SyntaxErrorException("Expected " + type.toString());

}

}

private void expectEndOfLine() throws SyntaxErrorException {

expect(TokenType.ENDOFLINE);

}

private Token peek() {

return currentTokenIndex < tokens.size() ? tokens.get(currentTokenIndex) : null;

}

}

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)
Transcribed Image Text: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)
Expert Solution
Step 1Introduction

The Parser class in Java is an abstract class that provides the basic methods for processing input from a stream of characters. It is used for reading and manipulating text in applications. The Parser class provides methods for parsing data from a stream of characters, and for transforming that data into objects. It can be used to parse text from XML documents and from HTML documents, as well as from other sources. The most commonly used implementation of the Parser class is the org.xml.sax.Parser class, which is used to parse data from an XML document.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education