Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

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);
    }
}

 

 

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
expand button
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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education