private List tokens; private int currentTokenIndex; public Parser(List tokens) { this.tokens = tokens; this.currentTokenIndex = 0; } public Node parse() throws SyntaxErrorExceptio

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

Java Programming: Below is the parser,java and there are errors in the getValue(). Please fix those errors. Attached is the rubric and image of the output. 

 

Parser.java

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 = term1();

 

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

 

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

 

Node right = term1();

 

if (right == null) {

 

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

 

}

 

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

 

}

 

return result;

 

}

 

 

private Node term1() {

 

Node result = factor();

 

while (peek() != null && (peek().getTokenType() == TokenType.MULTIPLY || peek().getTokenType() == TokenType.DIVIDE || peek().getTokenType() == TokenType.MODULUS)) {

 

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

 

Node right = factor();

 

if (right == null) {

 

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

 

}

 

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

 

}

 

return result;

 

}

private Node term() {

 

Node result = factor();

 

while (peek() != null && (peek().getTokenType() == TokenType.MULTIPLY || peek().getTokenType() == TokenType.DIVIDE || peek().getTokenType() == TokenType.MODULUS)) {

 

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

 

Node right = factor();

 

if (right == null) {

 

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

 

}

 

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

 

}

 

return result;

 

}

 

 

private Node factor() {

 

Token token = matchAndRemove(TokenType.MINUS);

 

Node result = null;

 

if (peek() != null) {

 

if (peek().getTokenType() == 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().getTokenType() == TokenType.LEFT_PAREN) {

 

matchAndRemove(TokenType.LEFT_PAREN);

 

result = expression();

 

expect(TokenType.RIGHT_PAREN);

 

}

 

}

 

return result;

 

}

 

 

private Token matchAndRemove(TokenType type) {

 

if (peek() != null && peek().getTokenType() == 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();

 

}

 

}

 

 

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)
The output of the above code would be:
Multiplication Node
Left: AdditionNode
Left: NumberNode (value=2)
Right: NumberNode (value=3)
Right: NumberNode(value=4)
EndOfLineNode
This output corresponds to the following abstract syntax tree:
2 3
Transcribed Image Text:The output of the above code would be: Multiplication Node Left: AdditionNode Left: NumberNode (value=2) Right: NumberNode (value=3) Right: NumberNode(value=4) EndOfLineNode This output corresponds to the following abstract syntax tree: 2 3
Expert Solution
Step 1

here is a modified version of the Parser.java code that fixes some syntax errors and provides a working implementation:

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

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
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