Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need the whole java code for this program Its a parser. write java vode from the information you understood Introduction Consider that real Shank
I need the whole java code for this program
Its a parser. write java vode from the information you understood
Introduction Consider that real Shank code is a collection of functions, much like we could say that a Java program is a collection of classes. We will build a set of Nodes to represent a function. A function has a number of pieces what we need to consider. We are already handling integer and real data types. We need to handle the other data types. Details Node Setup Create BooleanNode, CharacterNode, and StringNode, much like we created IntegerNode and RealNode. BooleanNode holds a bool; CharacterNode holds a char, StringNode holds a string. Variables have a name and a type and, one other key piece - changeable. Constants cannot be changed. Non-var parameters to a function cannot be changed. Constants have a value, as well. Since we don't know what data type this will be, it should be Node. Arrays have a from and to range (type integer). It might seem unintuitive to have local variables, constants and parameters all have the same Node. It turns out to simplify the design quite a bit, though. It's something that you will notice at the end of the project. Make a VariableNode (extending Node) that contains these members. Remember the constructor(s) and ToString(). Make a FunctionNode (extending Node). Functions have a name (string), parameters (collection of VariableNode), constants and variables (a different collection of VariableNode) and statements (a collection of StatementNode). Make a StatementNode - another mostly empty abstract class derived from Node. We will derive from it collection of StatementNode). Make a StatementNode - another mostly empty abstract class derived from Node. We will derive from it later. We need a data structure to hold all of our FunctionNodes. Make ProgramNode (derived from Node, of course) to do this. But instead of ArrayList or LinkedList of FunctionNodes, make a HashMap, using the function name as the key and the FunctionNode as the value. This will make it fast to find FunctionNodes by name later. Parsing Our parser should now expect any number of functions instead of any number of expressions. Let's look at an example of a function and talk about how to parse it: Let's look at an example of a function and talk about how to parse it: We don't have a mechanism to process statements yet. We will look for expressions inside of our functions so that indent and dedent will work (they won't be output on empty lines, remember). The model of recursive descent should suggest some methods, here: function() processes a function. It expects a define token. Then an identifier (the name). Then a left paren. Then a list of 0 or more variable declarations. Then a right paren. Then an endOftine. Then constants and variables. Then an indent. Then statements. Then a dedent. It returns a FunctionNode or null. parameterDeclarations() process the parameters and returns a collection of VariableNode. Remember that a parameter may or may not be var. Remember that there may not be any parameters. You can process constants and variables in one function or two (your decision). function() should call this function / these functions until there are no more. There is no strict ordering - you can have parameterDeclarations() process the parameters and returns a collection of VariableNode. Remember that a parameter may or may not be var. Remember that there may not be any parameters. You can process constants and variables in one function or two (your decision). function() should call this function / these functions until there are no more. There is no strict ordering - you can have variable lines and constants lines intermixed. Constant parsing needs the logic we used in factor for determining negative and integer or float (based on number). It also needs to account for char, string and boolean (by looking for the relevant tokens true, false, characterLiteral, stringLiteral). Making helper functions will be beneficial here. function() should expect indent, then call expression() until it returns null and print the resultant expressions (just to make sure that the parsing is still correct) then expect dedent. Since the expressions are just temporary, we won't store them in our FunctionNode(). parse() should call function() in its loop. Every FunctionNode returned should go into the ProgramNode (there will be only one of these). null should end the parse() loop. parse() should return the ProgramNode. In main, where parse() is called, get the ProgramNode and print every FunctionNode (which will print the parameters, variables, and constants). This should give you a full view of the parse tree so far. The expressions were previously printed and will be out of order, but we will be getting rid of them next assignmentStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started