예제 #1
0
 public void S() throws IOException {
   AbstractNode node;
   AbstractNode exprn;
   Id assId;
   if (look.tag == '(' || look.tag == Tag.NUM) {
     node = E();
   } else if (look.tag == Tag.ID) {
     currentAssigneeSymbol = (Id) look;
     match(Tag.ID);
     if (look.tag == '=') {
       match('=');
       exprn = E();
       node =
           threeAddressCodeGenerator.generateCodeForNode(
               threeAddressCodeGenerator.insertAndGetLeaf(currentAssigneeSymbol),
               exprn,
               "="); //// generate code for assignment
     } else {
       skipId = currentAssigneeSymbol;
       currentAssigneeSymbol = null;
       skipFlag = 1;
       node = E();
     }
   } else {
     throw new Error("Syntax Error");
   }
 }
예제 #2
0
  public void N1(Type b) throws IOException {
    if (look.tag == ',') {
      match(',');
      Token tok = look;
      Id id = (Id) tok;
      top.put(tok, id);
      used = used + b.width;
      match(Tag.ID);
      N1(b);
    } else {

    }
  }
예제 #3
0
 public void D() throws IOException {
   Type b = B();
   N(b);
   lexer.setCurrentType(null);
   match(';');
   D1();
 }
예제 #4
0
 public void N(Type b) throws IOException {
   Token tok = look;
   Id id = (Id) tok;
   top.put(tok, id);
   used = used + b.width;
   match(Tag.ID);
   N1(b);
 }
예제 #5
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();
 }
예제 #6
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;
  }
예제 #7
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;
 }
예제 #8
0
 public Type B() throws IOException {
   Type b = (Type) look;
   match(Tag.BASIC);
   return b;
 }