Java Programming: Show the output and there must be no errors at all.  Create a Parser class that 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. Create 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. The tricky part of mathematical expressions is order of operations. Fortunately, this is fairly easily solved in recursive descent by using a few functions. The intuition is this: accept tokens from the highest priority first. When you can’t find any that fit the pattern, fall back to lower priority. The thing that makes this “weird” is that we will call the lowest level first. But it calls the next highest first thing. Let’s take a look: EXPRESSION = TERM { (plus or minus) TERM} TERM = FACTOR { (times or divide or mod) FACTOR} FACTOR = {-} number or lparen EXPRESSION rparen Notice the curly braces – these indicate 0 or more. expression(), term() and factor() will all be methods of our recursive descent parser. All of them must use matchOrRemove(). The formulae above are very literal. Consider the first one: EXPRESSION = TERM { (plus or minus) TERM} expression() calls term(). Then it (in a loop) looks for a + or -. If it finds one, it calls term again and constructs a MathOpNode using the two term() returns as the left and right. That MathOpNode becomes the left if there are more +/- sign. So 1+2+3 becomes: MathOpNode(+,IntegerNode(1),(MathOpNode(+,IntegerNode(2),IntegerNode(3)))

Fundamentals of Information Systems
8th Edition
ISBN:9781305082168
Author:Ralph Stair, George Reynolds
Publisher:Ralph Stair, George Reynolds
Chapter7: Knowledge Management And Specialized Information Systems
Section: Chapter Questions
Problem 8SAT
icon
Related questions
Question

Java Programming: Show the output and there must be no errors at all. 

Create a Parser class that 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.

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

The tricky part of mathematical expressions is order of operations. Fortunately, this is fairly easily solved in recursive descent by using a few functions. The intuition is this: accept tokens from the highest priority first. When you can’t find any that fit the pattern, fall back to lower priority. The thing that makes this “weird” is that we will call the lowest level first. But it calls the next highest first thing. Let’s take a look:

EXPRESSION = TERM { (plus or minus) TERM}

TERM = FACTOR { (times or divide or mod) FACTOR}

FACTOR = {-} number or lparen EXPRESSION rparen

Notice the curly braces – these indicate 0 or more.

expression(), term() and factor() will all be methods of our recursive descent parser. All of them must use matchOrRemove(). The formulae above are very literal. Consider the first one:

EXPRESSION = TERM { (plus or minus) TERM}

expression() calls term(). Then it (in a loop) looks for a + or -. If it finds one, it calls term again and constructs a MathOpNode using the two term() returns as the left and right. That MathOpNode becomes the left if there are more +/- sign. So 1+2+3 becomes:

MathOpNode(+,IntegerNode(1),(MathOpNode(+,IntegerNode(2),IntegerNode(3)))

 

Let’s look at a complete example: 3*-4+5: our tokens will look something like this:

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE– your token types might differ a little

We will call expression(). The first thing it does is look for a term by calling term(). term() calls factor() first thing. factor() will matchAndRemove MINUS and not find it. It then tries NUMBER succeeds.

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

It looks at the NUMBER node, sees no decimal point and makes an IntegerNode (3) and returns it. term() gets the IntegerNode from factor() and now should loop looking for TIMES or DIVIDE. It finds a TIMES.

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

It should then call factor(). factor() looks for the MINUS. It finds it. It internally notes that we are now in “negative” mode. It then finds the NUMBER. It sees no decimal point and makes an IntegerNode (-4) and returns it.

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

term() gets the IntegerNode. It constructs a MathOpNode of type multiply with a left and right side of the two IntegerNodes. It now looks for multiply and divide and doesn’t find them. It returns the MathOpNode that it has.

Now expression() has a MathOpNode. It looks for plus or minus and finds plus.

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

Now expression() calls term() which calls factor() which looks for NUMBER, finds it and constructs an IntegerNode (5).

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

term() won’t find a times or divide, so it returns the IntegerNode. expression() now has a + with a left and a right side, so it creates that MathOpNode. It looks for plus/minus, doesn’t find it and returns the + MathOpNode. parse() should print the returned Node (the MathOpNode).

expectEndsOfLine() is called and finds an ENDOFLINE.

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

Assuming that was all that was in the input file, the next call to Expression() will return null and we will exit.

Your output can vary a lot. My only requirement is that it shows unambiguously that the tree was constructed correctly. One example:

MathOpNode(+, MathOpNode(*,IntegerNode(3),IntegerNode(-4)),IntegerNode(5))

expectEndsOfLine() is called and finds an ENDOFLINE.

NUMBER TIMES MINUS NUMBER PLUS NUMBER ENDOFLINE

Assuming that was all that was in the input file, the next call to Expression() will return null and we will exit.

Output:

MathOpNode(+, MathOpNode(*,IntegerNode(3),IntegerNode(-4)),IntegerNode(5))

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)
Input code: 3 + 4 * (5-2)
Token sequence: NUMBER (3) PLUS NUMBER (4) TIMES LPAREN NUMBER (5) MINUS NUMBER (2)
RPAREN
Then we can create a Lexer object to tokenize the input code and a Parser object to parse the
resulting token sequence:
1 String input = "3 + 4 * (5 - 2)";
2 Lexer lexer = new Lexer (input);
List<Token> tokens
lexer.tokenize();
4 Parser parser = new Parser(tokens);
5 Node ast = parser.parse();
SAWNP
3
Transcribed Image Text:Input code: 3 + 4 * (5-2) Token sequence: NUMBER (3) PLUS NUMBER (4) TIMES LPAREN NUMBER (5) MINUS NUMBER (2) RPAREN Then we can create a Lexer object to tokenize the input code and a Parser object to parse the resulting token sequence: 1 String input = "3 + 4 * (5 - 2)"; 2 Lexer lexer = new Lexer (input); List<Token> tokens lexer.tokenize(); 4 Parser parser = new Parser(tokens); 5 Node ast = parser.parse(); SAWNP 3
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Fundamentals of Information Systems
Fundamentals of Information Systems
Computer Science
ISBN:
9781305082168
Author:
Ralph Stair, George Reynolds
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT