Concept explainers
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 nowThis is a popular solution!
Step by stepSolved 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.
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.
- Please answer the question in the screenshot. The language used here is in Java.arrow_forwardplease complete the following in JAVA Implement the graph ADT using the adjacency list structure. thanks! also posting a similar question for adjacency matrix. have a good day!arrow_forwardWrite a c++ program to simulate a coffee shop or a Mcdonald's drive-thru where different customers give an order and they receive a unique ID then you use Queue Data Structure to arrange the order and use a sorting algorithm to sort identical orders so that you can make your entire servicing faster at the end the customer is given a receipt with his order, price, and ID on it. Make sure to use classes.arrow_forward
- Use Python for this question. Also please comment what each line of code means for this question as well: Inheritance (based on 8.38) You MUST use inheritance for this problem. A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack. A stack is often referred to as a last-in first-out (LIFO) container because the last item inserted is the first removed. Implement a Stack class using Note that this means you may be able to inherit some of the methods below. Which ones? (Try not writing those and see if it works!) Constructor/_init__ - Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top. push() – take an item as input and push it on the top of the stack pop() – remove and return the item at the top of the stack isEmpty() – returns True if the stack is empty,…arrow_forwardHi, would you be able to assist me with problem in java code. ......Four new students have arrived at Hogwarts School of Witchcraft and Wizardry. The students will join one of the four houses, Gryffindor, Hufflepuff, Ravenclaw, and Slytherin. The sorting hat has gone missing so in the meantime students will provide a list of the houses they wish to join and the Houses will rank the students in the order they wish to choose. You have been tasked with creating a program to determine a stable match between the students and Houses. The students will get to select their house of choice one at a time. If a student selects a house that is already chosen but rates higher on the list for that house, they are paired to that house and the student who is now un paired will need to select a new house form their ranked list. The program will need each student's name and list of house preferences. Additionally, it will need each house’s name and ranked list of students. How you implement that…arrow_forwardJava design a Queue with O(1) lookup time of the Maximum element. You will implement this design using the ArrayDeque Class in Java. solve the problem as stated below:- (1) Here you will Maintain two Queues - a Main Queue and a Queue holding the Maximum value(s) from the Main Queue (AKA Max Queue). The Main Queue contains the elements. The Max Queue contains the elements with Maximum value. The Max Queue would have to be a double ended Queue as you would like to be able to remove elements from both ends. Example : Let’s say we have the following: We add an integer 1 into our Main Queue and I hope it is really obvious that when the Main Queue contains a single element, the Max Queue can be populated without confusion :) Main Queue: 1 << front of Queue Max Queue : 1 << front of Queue Now, let’s say we insert a 4 into the Main Queue. the Main Queue will look as follows: Main Queue: 4 → 1 << front of Queue In the Max Queue, we don’t need 1 anymore, since 1 can never…arrow_forward
- Please answer in C++ and give explanation LAB 17.1-B Linked List Read the internal documentation for class ItemList carefully. Fill in the missing code in the implementation file, being careful to adhere to the preconditions and postconditions. Compile your program. ------------------------------------------------------------------------------------------------------------------------------- class ItemList { private: struct ListNode { int value; ListNode * next; }; ListNode * head; public: ItemList(); // Post: List is the empty list. bool IsThere(int item) const; // Post: If item is in the list IsThere is // True; False, otherwise. void Insert(int item); // Pre: item is not already in the list. // Post: item is in the list. void Delete(int item); // Pre: item is in the list. // Post: item is no…arrow_forwardin java pls and thank you!arrow_forwardYou will create two programs. The first one will use the data structure Stack and the other program will use the data structure Queue. Keep in mind that you should already know from your video and free textbook that Java uses a LinkedList integration for Queue. Stack Program Create a deck of cards using an array (Array size 15). Each card is an object. So you will have to create a Card class that has a value (1 - 10, Jack, Queen, King, Ace) and suit (clubs, diamonds, heart, spade). You will create a stack and randomly pick a card from the deck to put be pushed onto the stack. You will repeat this 5 times. Then you will take cards off the top of the stack (pop) and reveal the values of the cards in the output. As a challenge, you may have the user guess the value and suit of the card at the bottom of the stack. Queue Program There is a new concert coming to town. This concert is popular and has a long line. The line uses the data structure Queue. The people in the line are objects…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education