
Concept explainers
Java Programming: Below is my parser with the different nodes.java. There are errors in the parser. Please fix those errors and attached is the image of the output the parser must produce.
Parser.java
import java.util.List;
public class Parser {
private final List<Token> tokens;
private int current = 0;
public Parser(List<Token> tokens) {
this.tokens = tokens;
}
private Token matchAndRemove(TokenType type) {
if (isAtEnd()) return null;
Token token = tokens.get(current);
if (token.getType() == type) {
current++;
return token;
}
return null;
}
private void expectEndsOfLine() {
while (matchAndRemove(TokenType.END_OF_LINE) != null);
if (current >= tokens.size()) {
throw new SyntaxErrorException("Unexpected end of input");
}
}
private Token peek(int n) {
if (current + n >= tokens.size()) {
return null;
}
return tokens.get(current + n);
}
private boolean isAtEnd() {
return peek(0) == null;
}
public Node parse() {
Node expr = expression();
expectEndsOfLine();
while (!isAtEnd()) {
System.out.println(expr.toString());
expr = expression();
expectEndsOfLine();
}
return expr;
}
private Node expression() {
Node left = term();
while (matchAndRemove(TokenType.PLUS, TokenType.MINUS) != null) {
Token operator = tokens.get(current - 1);
Node right = term();
left = new BinaryExprNode(left, operator, right);
}
return left;
}
private Node term() {
Node left = factor();
while (matchAndRemove(TokenType.MULTIPLY, TokenType.DIVIDE) != null) {
Token operator = tokens.get(current - 1);
Node right = factor();
left = new BinaryExprNode(left, operator, right);
}
return left;
}
private Node factor() {
if (matchAndRemove(TokenType.NUMBER) != null) {
return new NumberNode(tokens.get(current - 1));
}
if (matchAndRemove(TokenType.LEFT_PAREN) != null) {
Node expr = expression();
matchAndRemove(TokenType.RIGHT_PAREN);
return expr;
}
throw new SyntaxErrorException("Expected a number or parentheses");
}
}
IntegerNode.java
package mypack;
public class IntegerNode extends Node{
private int value;
public IntegerNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String ToString() {
return Integer.toString(value);
}
}
MathOpNode.java
package mypack;
public class MathOpNode extends Node {
private Node left;
private Node right;
private MathOp operation;
public MathOpNode(Node left, Node right, MathOp operation) {
this.left = left;
this.right = right;
this.operation = operation;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public MathOp getOperation() {
return operation;
}
public String ToString() {
return "(" + left.ToString() + " " + operation + " " + right.ToString() + ")";
}
public enum MathOp {
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE,
MODULUS
}
}
Node.java
package mypack;
public abstract class Node {
public abstract String ToString();
}
RealNode.java
package mypack;
public class RealNode extends Node {
private float value;
public RealNode(float value) {
this.value = value;
}
public float getValue() {
return value;
}
public String ToString() {
return Float.toString(value);
}
}


Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 2 images

- please complete the following in JAVA Implement the graph ADT using the adjacency list structure. thanks! also posting a similar question for adjacency matrix. have a good day!arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forwardModify the Java class for the abstract stack type shown belowto use a linked list representation and test it with the same code thatappears in this chapter. class StackClass {private int [] stackRef;private int maxLen,topIndex;public StackClass() { // A constructorstackRef = new int [100];maxLen = 99;topIndex = -1;}public void push(int number) {if (topIndex == maxLen)System.out.println("Error in push–stack is full");else stackRef[++topIndex] = number;}public void pop() {if (empty())System.out.println("Error in pop–stack is empty"); else --topIndex;}public int top() {if (empty()) {System.out.println("Error in top–stack is empty");return 9999;}elsereturn (stackRef[topIndex]);}public boolean empty() {return (topIndex == -1);}}An example class that uses StackClass follows:public class TstStack {public static void main(String[] args) {StackClass myStack = new StackClass();myStack.push(42);myStack.push(29);System.out.println("29 is: " + myStack.top());myStack.pop();System.out.println("42 is:…arrow_forward
- Java Code: Create a Parser class. Much like the Lexer, it has a constructor that accepts a LinkedList of Token and creates a TokenManager that is a private member. The next thing that we will build is a helper method – boolean AcceptSeperators(). One thing that is always tricky in parsing languages is that people can put empty lines anywhere they want in their code. Since the parser expects specific tokens in specific places, it happens frequently that we want to say, “there HAS to be a “;” or a new line, but there can be more than one”. That’s what this function does – it accepts any number of separators (newline or semi-colon) and returns true if it finds at least one. Create a Parse method that returns a ProgramNode. While there are more tokens in the TokenManager, it should loop calling two other methods – ParseFunction() and ParseAction(). If neither one is true, it should throw an exception. bool ParseFunction(ProgramNode) bool ParseAction(ProgramNode) -Creates ProgramNode,…arrow_forwardWrite a Java program for a matrix class that can add and multiply arbitrary two dimensional arrays of integers. Textbook Project P-3.36, pp. 147 Implement Singly Linked List - use textbook Chapter 3.2 as an exaple. Write a main driver to test basic list implementations. Textbook reference : Data structures and algorithms in Java Micheal Goodricharrow_forwardWrite an abstract data type for a queue whose elements include both a 20-character string and an integer priority. This queue must have the following methods: enqueue, which takes a string and an integer as parameters; dequeue, which returns the string from the queue that has the highest priority; and empty. The queue is not to be maintained in priority order of its elements, so the dequeue operation must always search the whole queue.arrow_forward
- Please help with this Java program, and include explanations and commentsarrow_forwardConsider the instance variables and constructors. Given Instance Variables and Constructors: public class SimpleLinkedList<E> implements SimpleList<E>, Iterable<E>, Serializable { // First Node of the List private Node<E> first; // Last Node of the List private Node<E> last; // Number of List Elements private int count; // Total Number of Modifications (Add and Remove Calls) private int modCount; /** * Creates an empty SimpleLinkedList. */ publicSimpleLinkedList(){ first = null; last = null; count = 0; modCount = 0; } ... Assume the class contains the following methods that work correctly: public boolean isEmpty() public int size() public boolean add(E e) public E get(int index) private void validateIndex(int index, int end) Complete the following methods based on the given information from above. /** * Adds an element to the list at the…arrow_forwardAssume we have an IntBST class, which implements a binary search tree of integers. The field of the class is a Node variable called root that refers to the root element of the tree. 1) Write a recursive method for this class that computes and returns the sum of all integers less than the root element. Assume the tree is not empty and there is at least one element less than the root. 2) Write a recursive method that prints all of the leaves, and only the leaves, of a binary search tree. 3) Write a method, using recursion or a loop, that returns the smallest element in the tree.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





