Example #1
0
  public AbstractNode F() throws IOException {
    AbstractNode abstractNode = null;
    if (look.tag == '(') {
      match('(');
      abstractNode = E();
      match(')');
    } else if (look.tag == Tag.ID) {
      Id word = (Id) look;
      String workLex = word.lexeme;
      stackMachine.postfixTokenStack.push(word);
      match(Tag.ID);
      abstractNode =
          threeAddressCodeGenerator.insertAndGetLeaf(
              word); // insert leaf to  processed symbols if not exists
      postFix.append(workLex);
      // System.out.print(workLex);

    } else if (look.tag == Tag.NUM) {
      Num num = (Num) look;
      String IntNum = num.tostring();
      match(Tag.NUM);
      stackMachine.postfixTokenStack.push(num);
      abstractNode =
          threeAddressCodeGenerator.insertAndGetLeaf(
              num); // insert leaf to  processed symbols if not exists
      postFix.append(IntNum);
      // System.out.print(IntNum);
    } else if (look.tag == Tag.FLOAT) {
      Real real = (Real) look;
      String floatNum = real.tostring();
      match(Tag.FLOAT);
      stackMachine.postfixTokenStack.push(real);
      abstractNode =
          threeAddressCodeGenerator.insertAndGetLeaf(
              real); // insert leaf to  processed symbols if not exists
      postFix.append(floatNum);
      // System.out.print(floatNum);
    } else if (skipId != null && skipFlag == 1) {
      Id word = (Id) skipId;
      String workLex = word.lexeme;
      stackMachine.postfixTokenStack.push(word);
      match(Tag.ID);
      abstractNode =
          threeAddressCodeGenerator.insertAndGetLeaf(
              word); // insert leaf to  processed symbols if not exists
      postFix.append(workLex);
      skipId = null;
      // System.out.print(workLex);
    } else {
      throw new Error("Syntax Error");
    }
    return abstractNode;
  }
Example #2
0
 public AbstractNode T1(AbstractNode factnodeinh) throws IOException {
   AbstractNode node; // node which rerpesents the operation so far
   AbstractNode snode; // synthesised attribute which gives the full answer
   AbstractNode prefn = factnodeinh; // previous factor nonterminal is inherited
   AbstractNode curfn;
   if (look.tag == '*') {
     match('*');
     curfn = F();
     // System.out.print("*");
     postFix.append("*");
     stackMachine.evaluate("*");
     node =
         threeAddressCodeGenerator.generateCodeForNode(
             prefn, curfn, "*"); // generate code for node
     snode = T1(node);
   } else {
     snode = factnodeinh;
   }
   return snode;
 }
Example #3
0
 public void L() throws IOException {
   S();
   stackMachine.evaluate(null);
   if (currentAssigneeSymbol != null) {
     currentAssigneeSymbol.value = stackMachine.value;
     // System.out.println();
     postFix.append("\n");
     if (currentAssigneeSymbol.type == "int" && stackMachine.value instanceof Float) {
       postFix.append(
           "Warning : Stack Machine-Time mismatch (Narrowing convention) between assignee id type ="
               + currentAssigneeSymbol.type
               + " calculated value type="
               + Type.Float.tostring()
               + "\n");
       // System.out.println("Warning : Stack Machine-Time mismatch (Narrowing convention) between
       // assignee id type ="+currentAssigneeSymbol.typeOb.tostring()+ " calculated value
       // type="+Type.Float.tostring());
     } else if (currentAssigneeSymbol.type == "float" && stackMachine.value instanceof Integer) {
       postFix.append(
           "Warning : Stack Machine-Time mismatch (Widening convention) between assignee id type ="
               + currentAssigneeSymbol.type
               + " calculated value type="
               + Type.Int.tostring()
               + "\n");
       // System.out.println("Warning : Stack Machine-Time mismatch (Widening convention) between
       // assignee id type ="+currentAssigneeSymbol.typeOb.tostring()+ " calculated value
       // type="+Type.Int.tostring());
     }
     // System.out.println(currentAssigneeSymbol.lexeme+"="+currentAssigneeSymbol.value);
     postFix.append(currentAssigneeSymbol.lexeme + "=" + currentAssigneeSymbol.value + "\n");
   } else {
     // System.out.println();
     postFix.append("\n");
     postFix.append(stackMachine.value);
   }
   currentAssigneeSymbol = null;
   // toCode(AbsNode.used,bw);
   AbstractNode.processedSymbols = new ArrayList<AbstractNode>(); // new set of nodes for new stmt
   AbstractNode.tempVal = 0;
   System.out.println("Postfix notation and value of the statement");
   System.out.println(postFix);
   System.out.println();
   postFix = new StringBuffer();
   match(';');
   L1();
 }