Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to evaluate expressions in this tree?
    text
    copied!<p>Here is an example of a parsed xml file I'm working with that tabs it into a tree form</p> <pre><code>commandList assign variable #text[a] expression-int #text[1] assign variable #text[b] expression-int #text[2] assign variable #text[c] expression-operation operator #text[OP_SET] arguments expression-variable variable #text[a] expression-variable variable #text[b] assign variable #text[d] expression-operation operator #text[OP_SET] arguments expression-operation operator #text[OP_TUPLE] arguments expression-int #text[1] expression-int #text[2] expression-operation operator #text[OP_TUPLE] arguments expression-int #text[3] expression-int #text[4] </code></pre> <p>I hope this input isn't difficult to understand. Here is what it looks like normally when not parsed from an XML file:</p> <pre><code>a := 1; b := 2; c := {1,2}; d := {(1,2),(3,4)}; </code></pre> <p>etc...</p> <p>All of the assignment pairs (that is, a value and a variable) are to be stored in a hashmap so that the value can be looked up by it's variable and used in later expressions. I'm to use a recursive descent evaluator (I think?) to solve down the expressions according to the grammar.</p> <p>I've googled all sorts of things for the past 24 hours now and have seen a lot of tree evaluators for basic arithmetic (e.g. 2 + 3 * 8, etc) but haven't been able to see how that would work for my specific tree. </p> <p>Code I've written so far goes as low as finding the the variable names (a,b,c,d,e etc) but I can't begin to think of how to code the recursion which will provide the right values for the hash map.</p> <pre><code>public void evaluate(Node node){ HashMap&lt;String, String&gt; assignments = new HashMap&lt;String, String&gt;(); NodeList assignment = node.getChildNodes(); for (int i=0; i &lt; assignment.getLength(); i++){ //1 to 13 Node assign = assignment.item(i); Node variable = this.getChild(assign, 0); Node varValNode = this.getChild(variable, 0); String varVal = varValNode.getNodeValue(); Node expression = this.getChild(assign, 1); </code></pre> <p>The document, node and nodelist classes for my tree are unusual in that they don't allow a 'getChild' method which I think would save a lot of time. Does anybody know why this is?</p> <p>Really random problem here and I hope it made sense. Please ask me to elaborate on anything that is unclear and I will try the best that I can. I'm not looking for anyone to solve the problem for me but merely instruct me on how to decide how to code this recursive algorithm.</p> <p>EDIT: Also, the second 'input' I put above was actually the output. It should have been this:</p> <pre><code>a := 1; b := 2; c := @set(a,b); d := @set(@tuple(1,2),@tuple(3,4)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload