You will submit your existing files (Parser.java, Node.java, IntegerNode.java, FloatNode.java, MathOpNode.java,Basic.java, Token.java, Lexer.java) as well as your new files (VariableNode.java, PrintNode.java, AssignmentNode.java,StatementNode.java, StatementsNode.java).
We are going to build on the parser that we created last assignment to add some more useful capabilities and start the process of turning it into a programming language parser.
Create some new AST nodes:
StatementNode – derives from Node. All statements (like Print, Assignment) should derive from StatementNode.
VariableNode – holds a variable name
PrintNode – holds a list of Node (the things to print)
AssignmentNode – holds a variable node and a Node (which will be the assigned value)
StatementsNode – holds a list of StatementNode (the statements, right now either a print or an assignment)
For all of these, follow the usual rules: private read-only members, appropriate constructor(s), accessors, ToString().
Start by changing Factor() to accept an IDENTIFIER (i.e. a variable) as well as a number or an expression in parenthesis. When the identifier is found, create a VariableNode and return it.
Parse() right now only works on expressions. We need to introduce more methods to make our parser able to accept a programming language. Change Parse() to call Statements() instead of Expression().
Create a Statements() method; it should accept any number of statements. Statements() should call Statement() until Statement() fails (returns NULL). Create a Statement() method to handle a single statement and return its node or NULL. A single statement, for now, can consist of EITHER a print statement or an assignment. Your Statements() method must take the Node generated by Statement (if it is not NULL) and add it to the Statements AST node.
Create a PrintStatement method which accepts a print statement and creates a PrintNode or returns NULL:
PRINT printList
Create a PrintList method which accepts a print list. A print list consists of a comma separated list of expressions. You will need to add comma to your lexer.
Finally, an assignment statement is of the form:
VARIABLE EQUALS expression
You have already written the Expression parser. Create an Assignment() parser method that accepts an assignment and returns an AssignmentNode or returns NULL. You will need to add the equals sign to parts of your lexer (you already have it for >= and <=).


0 comments