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 p

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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.java and attached is the rubric. Please fix all the errors in the parser.java. I really need it!!!!!!

Parser.java

package mypack;

import java.util.Queue;

public class Parser {
  private Queue<Token> tokens;

  public Parser(Queue<Token> tokens) {
    this.tokens = tokens;
  }
  private Token matchAndRemove(TokenType type) {
    if (tokens.peek() != null && tokens.peek().getType() == type) {
      return tokens.remove();
    }
    return null;
  }


  private void expectEndOfLine() throws SyntaxErrorException {
    if (matchAndRemove(TokenType.ENDOFLINE) == null) {
      throw new SyntaxErrorException();
    }
    while (matchAndRemove(TokenType.ENDOFLINE) != null);
  }

  private Token peek(int n) {
    if (tokens.size() <= n) {
      return null;
    }
    return tokens.peek();
  }


  public Node parse() {
    while (expression() != null) {
      expectEndOfLine();
    }
    return null;
  }


  private Node expression() {
   
  }
}

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY