문제

Here is an example of a parsed xml file I'm working with that tabs it into a tree form

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]

I hope this input isn't difficult to understand. Here is what it looks like normally when not parsed from an XML file:

a := 1;
b := 2;
c := {1,2};
d := {(1,2),(3,4)};

etc...

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.

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.

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.

public void evaluate(Node node){
    HashMap<String, String> assignments = new HashMap<String, String>();
    NodeList assignment = node.getChildNodes();
    for (int i=0; i < 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);

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?

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.

EDIT: Also, the second 'input' I put above was actually the output. It should have been this:

a := 1;
b := 2;
c := @set(a,b);
d := @set(@tuple(1,2),@tuple(3,4));
도움이 되었습니까?

해결책

Assuming that all your values are of integer type, you should create a HashMap<string,Integer> to store variable values, and pass it to your evaluate method:

public static void main(String[] args) {
    NodeList commandList = ... // get your XML from somewhere
    Map<string,Integer> vars = new HashMap<string,Integer>();
    for (Node node : commandList) {
        evaluate(node, vars);
    }
    // At this point, vars contains values of all variables assigned in commands
    // from the command list
}

The evaluation should become relatively straightforward:

private static Integer evaluate(Node node, Map<string,Integer> vars) {
    if (node is an assignment) {
        String varName = ... // get variable name from node
        Node expr = ... // get the node representing expression being assigned
        Integer value = evaluate(expr, vars);
        vars.put(varName, value);
        return value;
    }
    if (node is a variable reference) {
        String varName = ... // get variable name from node
        return vars.get(varName);
    }
    if (node is an integer constant) {
        String constVal = ... // Get the representation from XML
        return Integer.decode(constVal);
    }
    if (node is a binary expression) {
        Node lhs = ... // Get the left-hand side expression from the node
        Node rhs = ... // Get the right-hand side expression from the node
        Integer lhsVal = evaluate(lhs, vars); 
        Integer rhsVal = evaluate(rhs, vars);
        if (operator is plus) {
            return new Integer(((int)lhsVal) + ((int)rhsVal));
        }
        if (operator is minus) {
            return new Integer(((int)lhsVal) - ((int)rhsVal));
        }
        if (operator is multiply) {
            return new Integer(((int)lhsVal) * ((int)rhsVal));
        }
        if (operator is divide) {
            return new Integer(((int)lhsVal) / ((int)rhsVal));
        }
        // ... and so on
    }
    // ... and so on
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top