Java Code: In this assignment, we are going to start working on the parser. The lexer’s job is to make “words”, the parser’s job is to make sure that those tokens are in an order that makes sense and create a data structure in memory that represents the program. The Tokens are in a 1-dimensional structure (array or list). The parser’s output will be a 2-dimensional tree. The parser works using recursive descent. We are encoding the rules into the structure of the program. There are a few simple rules that we will follow in writing our parser: Each function represents some “phrase” in our language, like “if statement” or “assignment statement”. Each function must either succeed or fail: On success, remove Tokens from the list and output a tree node On failure, leave the list unchanged and return null. When there are alternatives, the function must call each alternative’s function until it finds one that is not null. Create 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. 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. Make sure to use various test cases and provide scree
Java Code:
In this assignment, we are going to start working on the parser. The lexer’s job is to make “words”, the parser’s job is to make sure that those tokens are in an order that makes sense and create a data structure in memory that represents the program. The Tokens are in a 1-dimensional structure (array or list). The parser’s output will be a 2-dimensional tree.
The parser works using recursive descent. We are encoding the rules into the structure of the program. There are a few simple rules that we will follow in writing our parser:
- Each function represents some “phrase” in our language, like “if statement” or “assignment statement”.
- Each function must either succeed or fail:
- On success, remove Tokens from the list and output a tree node
- On failure, leave the list unchanged and return null.
- When there are alternatives, the function must call each alternative’s function until it finds one that is not null.
Create 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.
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.
Make sure to use various test cases and provide screenshots of the test cases being tested for the code. There must be no error in the code at all.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
I ran the code and got an error. I even created a main.java file to run the test cases. Please fix the error and provide me the correct code for all parts. Make sure to give the screenshot of the output as well.
I ran the code and got an error. Please fix the error and provide me the correct code for all parts. Make sure to give the screenshot of the output as well.