Beispiel #1
0
 /**
  * Sets the children of node to be those specified in array.
  *
  * @param node the node whose children will be set.
  * @param children an array of nodes which will be the children of the node.
  */
 public void copyChildren(Node node, Node children[]) {
   int nchild = children.length;
   node.jjtOpen();
   for (int i = 0; i < nchild; ++i) {
     children[i].jjtSetParent(node);
     node.jjtAddChild(children[i], i);
   }
   node.jjtClose();
 }
Beispiel #2
0
  /**
   * Parses the expression. If there are errors in the expression, they are added to the <code>
   * errorList</code> member. Errors can be obtained through <code>getErrorInfo()</code>.
   *
   * @param expression_in The input expression string
   * @return the top node of the expression tree if the parse was successful, <code>null</code>
   *     otherwise
   */
  public Node parseExpression(String expression_in) {
    Reader reader = new StringReader(expression_in);

    try {
      // try parsing
      errorList.removeAllElements();
      topNode = parser.parseStream(reader, this);

      // if there is an error in the list, the parse failed
      // so set topNode to null
      if (hasError()) topNode = null;
    } catch (Throwable e) {
      // an exception was thrown, so there is no parse tree
      topNode = null;

      // check the type of error
      if (e instanceof ParseException) {
        // the ParseException object contains additional error
        // information
        errorList.addElement(((ParseException) e).getMessage());
        // getErrorInfo());
      } else {
        // if the exception was not a ParseException, it was most
        // likely a syntax error
        if (debug) {
          System.out.println(e.getMessage());
          e.printStackTrace();
        }
        errorList.addElement("Syntax error");
      }
    }

    // If traversing is enabled, print a dump of the tree to
    // standard output
    if (traverse && !hasError()) {
      ParserVisitor v = new ParserDumpVisitor();
      try {
        topNode.jjtAccept(v, null);
      } catch (ParseException e) {
        errorList.addElement(e.getMessage());
      }
    }

    return topNode;
  }
Beispiel #3
0
  /**
   * Returns the value of the expression as an object. The expression tree is specified with its top
   * node. The algorithm uses a stack for evaluation.
   *
   * <p>The symTab parameter can be null, if no variables are expected in the expression. If a
   * variable is found, an error is added to the error list.
   *
   * <p>An exception is thrown, if an error occurs during evaluation.
   *
   * @return The value of the expression as an object.
   * @throws ParseException if there is a problem with the evaluation.
   */
  public Object getValue(Node topNode, SymbolTable symTab_in) throws ParseException {

    // check if arguments are ok
    if (topNode == null) {
      throw new ParseException("topNode parameter is null");
    }

    // set member vars
    // errorList = errorList_in;
    symTab = symTab_in;
    // System.out.println("Evaluating expression " + topNode.toLongString() + " with symTab: " +
    // symTab);
    // errorFlag = false;
    stack.removeAllElements();
    // rjm addition ensure stack is correct before beginning.
    // njf changed from clear() to removeAllElements for 1.1 compatibility

    // evaluate by letting the top node accept the visitor
    topNode.jjtAccept(this, null);
    /*
    } catch (ParseException e) {
    this.addToErrorList("Error: "+e.getMessage());
    return null;
    }
    if(errorFlag) return null;
     */

    // something is wrong if not exactly one item remains on the stack
    // or if the error flag has been set
    if (stack.size() != 1) {
      throw new ParseException("Stack corrupted");
    }

    // return the value of the expression
    return stack.pop();
  }
Beispiel #4
0
 public Object eval(Node node) throws ParseException {
   MatrixValueI val = (MatrixValueI) node.jjtAccept(this, null);
   return val.copy();
 }
Beispiel #5
0
 /**
  * Evaluates a given node, in the current context.
  *
  * @param node The node to evaluate
  * @return result of the evaluation
  */
 public Object eval(Node node) throws ParseException {
   node.jjtAccept(this, null);
   return stack.pop();
 }