Question: complete java code needed get me the code from what you understood. Introduction When we wrote expression, term and factor, there were a few details

 complete java code needed get me the code from what you

understood. Introduction When we wrote expression, term and factor, there were a

few details that we left out. This is common in language design

complete java code needed

get me the code from what you understood.

Introduction When we wrote expression, term and factor, there were a few details that we left out. This is common in language design - going back and adding pieces. In this case, we only considered math operations. But there are other operations that can occur in an expression. For example - comparison operations like >. Strings can be concatenated with +. And, of course, we can have variables in expression. With these things in place, we have all of the pieces necessary to handle our first statements assignments. You will recall, we started with: EXPRESSION = TERM { (plus or minus) TERM } TERM = FACTOR { (times or divide or mod) FACTOR } FACTOR ={} number or Iparen EXPRESSION rparen We need to extend that: BOOLCOMPARE = EXPRESSION [(,=,=,) EXPRESSION] - note this is 0 or 1 , not 0 or more EXPRESSION = TERM { (plus or minus) TERM } TERM = FACTOR { (times or divide or mod) FACTOR } FACTOR ={} number or Iparen EXPRESSION rparen or variableReference or variableReference [ EXPRESSION ] A variable reference is a variable name or a variable name with an array index (x[3+3] for example) Details Let's start by making a VariableReferenceNode. This is used any time in our code where we reference a variable (not define it). variable x :integer { THIS IS A VARIABLE NODE } x:=x+1{ THESE ARE VARIABLE REFERENCE NODES } A variable reference node extends Node and has a name and an optional Node which is the array index expression. Remember the constructor(s) and ToString() method. Add variable reference node as a possibility in factor(). We also need a BooleanCompareNode which works a lot like MathOpNode - an enum for what type of comparison and a left and right side. Now we can create the boolCompare() method and update the factor() method in the parser. Notice that we could call boolCompare() where a boolean comparison is reasonable and expression() where boolean comparison cannot happen. This is very convenient as we see, for example in factor() where the integer indexing calls expression(). With this complete, we can start on assignment statements. Create an AssignmentNode that derives from StatementNode (which derives from Node). It needs a variableReferenceNode for the target of the assignment and a Node for the value. We don't know what it will be - it could be anything from a BooleanCompareNode to an IntegerNode. We need to create assignment() to parse the assignments. An assignment is an identifier (with possible array index) followed by := followed by a boolCompare. This can sound very simple, but has some complexitv. Consider this: We need to create assignment() to parse the assignments. An assignment is an identifier (with possible array index) followed by := followed by a boolCompare. This can sound very simple, but has some complexity. Consider this: a[b[c[0]]]:=5 Some careful application of peek() and maybe some recursion will be necessary to parse the target side correctly. Don't try to take a clever shortcut (like looking for a := anywhere on the line) - that can easily bite you, if not now, then later. If something is not right in the assignment statement, you must throw our SyntaxError exception. Some examples include: right side missing, right side is not a valid expression Don't try to do type checking - we will handle this later. Finally, our function() should call a new function, statements(). statements() expects indent, calls statement() repeatedly (until it returns null) and then expects dedent. statements() returns a collection of StatementNode. statement() will process any single statement. Right now, it should just call assignment () and return what it returns. function() should call statements() and put the returned collection of StatementNode into the FunctionNode

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!