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

Question

Java Programming: Below is parser.java and attached is circled all the methods that must be implemented in parser.java. Make sure to show a full working code & the screenshot of the output. 

Parser.java

package mypack;


import java.util.List;
import java.util.Queue;


public class Parser {
    
    private Queue<Token> tokens;
    
    public Parser(List<Token> tokens) {
        this.tokens = (Queue<Token>) tokens;
    }
    
    private Token matchAndRemove(Token.TokenType... types) {
        if (tokens.isEmpty()) {
            return null;
        }
      
        Token token = tokens.peek();
        for (Token.TokenType type : types) {
            if (token.getTokenType() == type) {
                tokens.remove();
                return token;
            }
        }
        return null;
    }
 
    private void expectEndsOfLine() throws SyntaxErrorException {
        while (matchAndRemove(Token.TokenType.ENDOFLINE) != null);
        if (tokens.isEmpty()) {
            throw new SyntaxErrorException();
        }
    }
    
   
    private Token peek(int i) {
        if (tokens.size() < i) {
            return null;
        }
        return (Token) tokens.toArray()[i-1];
    }

    public Node parse() throws SyntaxErrorException {
        Node result = null;
        do {
            result = expression();
            if (result != null) {
                System.out.println(result.toString());
                expectEndsOfLine();
            }
        } while (result != null);
        return null;
    }
    
 
    private Node expression() throws SyntaxErrorException {
        Node left = term();
        if (left == null) {
            return null;
        }
   
        while (true) {
            Token op = matchAndRemove(Token.TokenType.PLUS, Token.TokenType.MINUS);
            if (op == null) {
                return left;
            }
          
            Node right = term();
            if (right == null) {
                throw new SyntaxErrorException();
            }
            left = new MathOpNode(op.getTokenType(), left, right);
        }
    }
  
    private Node term() throws SyntaxErrorException {
        Node left = factor();
        if (left == null) {
            return null;
        }
       
        while (true) {
            Token op = matchAndRemove(Token.TokenType.TIMES, Token.TokenType.DIVIDE, Token.TokenType.MODULUS);
            if (op == null) {
                return left;
            }
          
            Node right = factor();
            if (right == null) {
                throw new SyntaxErrorException();
            }
            left = new MathOpNode(op.getTokenType(), left, right);
        }
    }

private Node factor() throws SyntaxErrorException {
    Token token = matchAndRemove(Token.TokenType.NUMBER, Token.TokenType.MINUS, Token.TokenType.LEFT_PAREN, Token.TokenType.IDENTIFIER);
    if (token == null) {
        return null;
    }

    switch (token.getTokenType()) {
        case NUMBER:
            return new IntegerNode(Integer.parseInt(token.getLexeme()));
        case MINUS:
            Node inner = factor();
            if (inner == null) {
                throw new SyntaxErrorException();
            }
            return new MathOpNode(Token.TokenType.MINUS, new IntegerNode(0), inner);
        case LEFT_PAREN:
            Node expression = expression();
            if (expression == null) {
                throw new SyntaxErrorException();
            }
            if (matchAndRemove(Token.TokenType.RIGHT_PAREN) == null) {
                throw new SyntaxErrorException();
            }
            return expression;
       
        case IDENTIFIER:
            VariableReferenceNode varRefNode = new VariableReferenceNode(token.getLexeme());
            if (matchAndRemove(Token.TokenType.LEFT_BRACE) != null) {
                Node indexExpr = expression();
                if (indexExpr == null) {
                    throw new SyntaxErrorException();
                }
                varRefNode.setIndex(indexExpr);
                if (matchAndRemove(Token.TokenType.RIGHT_BRACE) == null) {
                    throw new SyntaxErrorException();
                }
            }
            return varRefNode;
        default:
            return null;
}
}
}

Rubric
Comments
Variable/Function
naming
IfNode
WhileNode
ForNode
RepeatNode
FunctionCallNode
ParameterNode
parseFor()
parseWhile()
parself()
parseFunctionCalls()
Ranges
Poor
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)
None (0)
None (0)
OK
"What" not
"Why", few (5)
Lots of
abbreviations (5)
Good
Some "what" comments
or missing some (7)
Full words most of the
time (8)
Parses simple for
statements correctly (5)
Parses simple while
statements correctly (5)
Parses simple if
statements correctly (5)
Parses simple function
calls correctly (5)
Implemented for
integers, strings (5)
Great
Anything not obvious has reasoning
(10)
Full words, descriptive (10)
Has constructor(s), ToString,
Conditions, Statements, Nextlf (5)
Has constructor(s), ToString,
Conditions, Statements(5)
Has constructor(s), ToString, from, to,
Statements(5)
Has constructor(s), ToString,
Conditions, Statements (5)
Has constructor(s), ToString, name,
collection of parameters(5)
Has VariableReferenceNode, Node,
constructor(s), ToString() (5)
Parses complex for statements
correctly (10)
Parses complex while statements
correctly (10)
Parses complex, chained if statements
correctly (10)
Parses complex function calls with
expressions and vars correctly (10)
Implemented for integers, strings,
reals (10)
expand button
Transcribed Image Text:Rubric Comments Variable/Function naming IfNode WhileNode ForNode RepeatNode FunctionCallNode ParameterNode parseFor() parseWhile() parself() parseFunctionCalls() Ranges Poor 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) None (0) None (0) OK "What" not "Why", few (5) Lots of abbreviations (5) Good Some "what" comments or missing some (7) Full words most of the time (8) Parses simple for statements correctly (5) Parses simple while statements correctly (5) Parses simple if statements correctly (5) Parses simple function calls correctly (5) Implemented for integers, strings (5) Great Anything not obvious has reasoning (10) Full words, descriptive (10) Has constructor(s), ToString, Conditions, Statements, Nextlf (5) Has constructor(s), ToString, Conditions, Statements(5) Has constructor(s), ToString, from, to, Statements(5) Has constructor(s), ToString, Conditions, Statements (5) Has constructor(s), ToString, name, collection of parameters(5) Has VariableReferenceNode, Node, constructor(s), ToString() (5) Parses complex for statements correctly (10) Parses complex while statements correctly (10) Parses complex, chained if statements correctly (10) Parses complex function calls with expressions and vars correctly (10) Implemented for integers, strings, reals (10)
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