/**
   * Parse according to the grammar statement. statement: ID '=' expression ;
   *
   * <p>match(ID); match('='); expr()
   */
  public void parserRuleAssignmentStatement(final AbstractSyntaxTree ast) {
    final LangType p1 = this.parserLookAhead(1);
    final LangType p2 = this.parserLookAhead(2);
    boolean checkRuleSymbol = p1 != null && p1.eqlt(new LangTypeTokenSymbol("<none>"));
    boolean checkRuleAssignment = p2 != null && p2.eql(new LangTypeToken("="));

    if (checkRuleSymbol && checkRuleAssignment) {
      this.parserMatchType(new LangTypeTokenSymbol("<none>"));
      this.parserMatch(new LangTypeToken("="));

      AbstractSyntaxTree continueWithAssignTree = null;
      final AbstractSyntaxTree assignTree = new AssignmentNode(p2);
      assignTree.addChildToAbstractSyntaxTree(new SymbolNode(p1));
      ast.addChildToAbstractSyntaxTree(assignTree);
      continueWithAssignTree = assignTree;

      // Continue with rule for numbers
      this.parserRuleNumberExpression(continueWithAssignTree);

    } else if (checkRuleSymbol) {
      this.parserMatchType(new LangTypeTokenSymbol("<none>"));

    } else {
      System.out.println(
          "Parser rule fail - "
              + checkRuleSymbol
              + " // "
              + checkRuleAssignment
              + " $ "
              + p1
              + "/"
              + p2);
    } // End of if - check for assignment rule
  }
 /** Visitor dispatch based on node token type */
 @Override
 public Object visit(final ProgramRootNode nodeOnVisit) {
   compiler.writeClassFile(nodeOnVisit);
   if (nodeOnVisit.children() == null || nodeOnVisit.children().size() == 0) {
     return new Object();
   }
   for (final AbstractSyntaxTree node : nodeOnVisit.children()) {
     if (node instanceof AssignmentNode) {
       node.visit(this);
     } // End of if sub nodes //
   } // End of the for //
   return new Object();
 } // End of program dispatch //
  /** Parse according to the grammar statement. expressionRest: +/- NUMBER {expressionRest} */
  public void parserRuleNumberExpressionRest(
      final AbstractSyntaxTree ast, final LangType numberValLhs) {
    final LangType p1 = this.parserLookAhead(1);
    final boolean checkRulePlus = p1 != null && p1.eql(new LangTypeToken("+"));
    if (checkRulePlus) {
      final LangType plus = new LangTypeToken("+");
      final AbstractSyntaxTree newNodePlus = new PlusNode(plus);
      final AbstractSyntaxTree valnode = new NumberNode(numberValLhs);
      newNodePlus.addChildToAbstractSyntaxTree(valnode);

      this.parserMatch(plus);
      final LangType numberValRhs = this.parserLookAhead(1);
      this.parserMatchType(new LangTypeTokenDouble(-1));
      this.parserRuleNumberExpressionRest(newNodePlus, numberValRhs);
      ast.addChildToAbstractSyntaxTree(newNodePlus);
    } else {
      final AbstractSyntaxTree valnode = new NumberNode(numberValLhs);
      ast.addChildToAbstractSyntaxTree(valnode);
    } // End of the if - check rule for plus
  }
Exemple #4
0
  public static ArrayList<String> withdrawVariablesInRule(
      AbstractSyntaxTree<DataTypeEnum, IValue> booleanRule) {

    ArrayList<String> ret = new ArrayList<String>();
    AbstractSyntaxTreeNode<DataTypeEnum, IValue> root = null;

    if (booleanRule != null && booleanRule.getRootNode() != null) root = booleanRule.getRootNode();
    else return ret;

    Queue<AbstractSyntaxTreeNode<DataTypeEnum, IValue>> nodeQueue =
        new LinkedList<AbstractSyntaxTreeNode<DataTypeEnum, IValue>>();
    nodeQueue.offer(root);

    while (!nodeQueue.isEmpty()) {
      AbstractSyntaxTreeNode<DataTypeEnum, IValue> currentNode = nodeQueue.poll();

      for (int i = 0; i < currentNode.getNumberOfChildren(); i++)
        nodeQueue.offer(currentNode.getChildAt(i));

      if (currentNode.isLeaf()) ret.add(currentNode.toString());
    }

    return ret;
  }