⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.java

📁 一个JAVA编写的简单编译器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    ts (CKeyWords.SY_RB2);        // )
    parseStatement ();
    tstat = (ast.IJSubNode)stack.pop ();
    if (sy == CKeyWords.SY_ELSE /* else */) {
      ts (CKeyWords.SY_ELSE);     // else
      parseStatement ();
      fstat = (ast.IJSubNode)stack.pop ();
    }
    ifstat = new ast.statement.IfNode ((ast.booleanExpr.JSubBooleanNode)expr, tstat, fstat);
    stack.push (ifstat);
    util.Trace.end ("ifStatement");
  } // end of parseifStatement

  void parsewhileStatement() {
    util.Trace.begin ("whileStatement");
    ast.IJSubNode expr = null;
    ast.statement.WhileNode whilestat = null;
    ast.IJSubNode stat = null;
    ts (CKeyWords.SY_WHILE); // while
    ts (CKeyWords.SY_LB2);   // (
    parseExpr ();
    expr = (ast.IJSubNode)stack.pop ();
    if (! (expr instanceof ast.booleanExpr.JSubBooleanNode))
      util.Error.e1 ("Boolean Expression of while expected");
    ts (CKeyWords.SY_RB2);   // )
    parseStatement ();
    stat = (ast.IJSubNode)stack.pop ();
    whilestat = new ast.statement.WhileNode ((ast.booleanExpr.JSubBooleanNode)expr, stat);
    stack.push (whilestat);
    util.Trace.end ("whileStatement");
  } // end of parsewhileStatement

  void parsereturnStatement() {
    util.Trace.begin ("returnStatement");
    ast.IJSubNode expr = null;
    ts (CKeyWords.SY_RETURN);              // return
    if (  sy == CKeyWords.SY_IDENTIFIER    /* Identifier */
       || sy == CKeyWords.SY_LB2           /* (          */
       || sy == CKeyWords.SY_MINUS         /* -          */
       || sy == CKeyWords.SY_BOOL_NOT      /* !          */
       || sy == CKeyWords.SY_NUMBERLITERAL /* Literal    */) {
      parseExpr ();
      expr = (ast.IJSubNode)stack.pop();
    }
    stack.push (new ast.statement.ReturnNode (expr, currentMethod));
    ts (CKeyWords.SY_SEMICOLON);           // ;
    util.Trace.end ("returnStatement");
  } // end of parsereturnStatement

  void parsenothingStatement() {
    util.Trace.begin ("nothingStatement");
    ts (CKeyWords.SY_SEMICOLON); // ;
    util.Trace.end ("nothingStatement");
  } // end of parsenothingStatement

  void parseExpr() {
    util.Trace.begin ("expr");
    ast.IJSubNode node = null;
    parseExpr13 ();
    while (sy == CKeyWords.SY_ASS /* = */) {
      if (node == null)
        node = new ast.binaryExpr.AssNode ((ast.IJSubNode)stack.pop());
      ts (CKeyWords.SY_ASS);        // =
      parseExpr13 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("expr");
  } // end of parseExpr

  void parseExpr13() {
    util.Trace.begin ("Expr13");
    parseExpr12 ();
    ast.booleanExpr.ORNode node = null;
    while (  sy == CKeyWords.SY_BOOL_OR /* || */) {
      if (node == null)
        node = new ast.booleanExpr.ORNode ((ast.IJSubNode)stack.pop());
      ts (CKeyWords.SY_BOOL_OR);        // ||
      parseExpr12 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("Expr13");
  } // end of parseExpr13

  void parseExpr12() {
    util.Trace.begin ("Expr12");
    parseExpr08 ();
    ast.booleanExpr.ANDNode node = null;
    while (  sy == CKeyWords.SY_BOOL_AND /* && */) {
      if (node == null)
        node = new ast.booleanExpr.ANDNode ((ast.IJSubNode)stack.pop());
      ts (CKeyWords.SY_BOOL_AND);        // &&
      parseExpr08 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("Expr12");
  } // end of parseExpr12

  void parseExpr08() {
    util.Trace.begin ("Expr08");
    ast.booleanExpr.JSubBooleanNode node = null;
    parseExpr07 ();
    while (  sy == CKeyWords.SY_EQ /* == */
         ||  sy == CKeyWords.SY_NE /* != */) {
      switch (sy) {
        case CKeyWords.SY_EQ :     // ==
          node = new ast.booleanExpr.EQNode ((ast.IJSubNode)stack.pop());
          ts (CKeyWords.SY_EQ);    // ==
          break;
        case CKeyWords.SY_NE :     // !=
          node = new ast.booleanExpr.NENode ((ast.IJSubNode)stack.pop());
          ts (CKeyWords.SY_NE);    // !=
          break;
        default : error ("Unexpected Token");
      }
      parseExpr07 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("Expr08");
  } // end of parseExpr08

  void parseExpr07() {
    util.Trace.begin ("Expr07");
    parseExpr05 ();
    ast.booleanExpr.JSubBooleanNode node = null;
    if (  sy == CKeyWords.SY_L  /* <  */
         ||  sy == CKeyWords.SY_LE /* <= */
         ||  sy == CKeyWords.SY_G  /* >  */
         ||  sy == CKeyWords.SY_GE /* >= */) {
      switch (sy) {
        case CKeyWords.SY_L :      // <
          node = new ast.booleanExpr.LTNode ((ast.IJSubNode)stack.pop());
          ts (CKeyWords.SY_L);     // <
          break;
        case CKeyWords.SY_LE :     // <=
          node = new ast.booleanExpr.LENode ((ast.IJSubNode)stack.pop());
          ts (CKeyWords.SY_LE);    // <=
          break;
        case CKeyWords.SY_G :      // >
          node = new ast.booleanExpr.GTNode ((ast.IJSubNode)stack.pop());
          ts (CKeyWords.SY_G);     // >
          break;
        case CKeyWords.SY_GE :     // >=
          node = new ast.booleanExpr.GENode ((ast.IJSubNode)stack.pop());
          ts (CKeyWords.SY_GE);    // >=
          break;
        default : error ("Unexpected Token");
      }
      parseExpr05 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("Expr07");
  } // end of parseExpr07

  void parseExpr05() {
    util.Trace.begin ("Expr05");
    parseExpr04 ();
    ast.IJSubNode node = null;
    int lastsy = -1;
    while (  sy == CKeyWords.SY_PLUS  /* + */
         ||  sy == CKeyWords.SY_MINUS /* - */) {
      switch (sy) {
        case CKeyWords.SY_PLUS :      // +
          if (node == null) {
            node = new ast.binaryExpr.AddNode ((ast.IJSubNode)stack.pop());
          } else if (sy != lastsy) {
            node = new ast.binaryExpr.AddNode (node);
          }
          lastsy = sy;
          ts (CKeyWords.SY_PLUS);     // +
          break;
        case CKeyWords.SY_MINUS :     // -
          if (node == null) {
            node = new ast.binaryExpr.SubNode ((ast.IJSubNode)stack.pop());
          } else if (sy != lastsy) {
            node = new ast.binaryExpr.SubNode (node);
          }
          lastsy = sy;
          ts (CKeyWords.SY_MINUS);    // -
          break;
        default : error ("Unexpected Token");
      }
      parseExpr04 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("Expr05");
  } // end of parseExpr05

  void parseExpr04() {
    util.Trace.begin ("Expr04");
    ast.IJSubNode node = null;
    int lastsy = -1;
    parseExpr00 ();
    while (  sy == CKeyWords.SY_TIMES     /* * */
         ||  sy == CKeyWords.SY_DIV       /* / */
         ||  sy == CKeyWords.SY_REMAINDER /* % */) {
      switch (sy) {
        case CKeyWords.SY_TIMES :         // *
          if (node == null) {
            node = new ast.binaryExpr.MultNode ((ast.IJSubNode)stack.pop());
          } else if (sy != lastsy) {
            node = new ast.binaryExpr.MultNode (node);
          }
          ts (CKeyWords.SY_TIMES);        // *
          break;
        case CKeyWords.SY_DIV :           // /
          if (node == null) {
            node = new ast.binaryExpr.DivNode ((ast.IJSubNode)stack.pop());
          } else if (sy != lastsy) {
            node = new ast.binaryExpr.DivNode (node);
          }
          ts (CKeyWords.SY_DIV);          // /
          break;
        case CKeyWords.SY_REMAINDER :     // %
          if (node == null) {
            node = new ast.binaryExpr.RemNode ((ast.IJSubNode)stack.pop());
          } else if (sy != lastsy) {
            node = new ast.binaryExpr.RemNode (node);
          }
          ts (CKeyWords.SY_REMAINDER);    // %
          break;
        default : error ("Unexpected Token");
      }
      parseExpr00 ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    if (node != null)
      stack.push (node);
    util.Trace.end ("Expr04");
  } // end of parseExpr04

  void parseExpr00() {
    util.Trace.begin ("Expr00");
    switch (sy) {
      case CKeyWords.SY_IDENTIFIER :             // Identifier
        {
          String identifier = scanner.getText();
          ast.declaration.JSubObject d = symboltable.getObject(identifier);
          if (d == null)
            util.Error.e1 ("Symbol nicht definiert " + scanner.getText ());
          ts (CKeyWords.SY_IDENTIFIER);            // Identifier
   
          if (  sy == CKeyWords.SY_LB2             /* (          */) {
            // this is a method call
            ast.binaryExpr.CallNode node = new ast.binaryExpr.CallNode (d);
            stack.push (node);
            ts (CKeyWords.SY_LB2);                 // (
            if (  sy == CKeyWords.SY_IDENTIFIER    /* Identifier */
               || sy == CKeyWords.SY_LB2           /* (          */
               || sy == CKeyWords.SY_MINUS         /* -          */
               || sy == CKeyWords.SY_BOOL_NOT      /* !          */
               || sy == CKeyWords.SY_NUMBERLITERAL /* Literal    */) {
               parseExprList ();
            }
            ts (CKeyWords.SY_RB2);                 // )
          } else {
            ast.Leaf leaf = new ast.Leaf (d);
            stack.push (leaf);
          }
          break;
        }
      case CKeyWords.SY_LB2 :                    // (
        ts (CKeyWords.SY_LB2);                   // (
        parseExpr ();
        ts (CKeyWords.SY_RB2);                   // )
        break;
      case CKeyWords.SY_BOOL_NOT :               // !
        ts (CKeyWords.SY_BOOL_NOT);              // !
        parseExpr00 ();
        break;
      case CKeyWords.SY_MINUS :                  // -
        ts (CKeyWords.SY_MINUS);                 // -
        parseExpr00 ();
        break;
      case CKeyWords.SY_NUMBERLITERAL :          // Literal
        {
          ast.declaration.JSubObject d = symboltable.addintObject(scanner.getText ());
          ast.Leaf leaf = new ast.Leaf (d);
          stack.push (leaf);
        }
        ts (CKeyWords.SY_NUMBERLITERAL);         // Literal
        break;
      default : error ("Unexpected Token");
    }
    util.Trace.end ("Expr00");
  } // end of parseExpr00

  void parseExprList() {
    util.Trace.begin ("ExprList");
    ast.IJSubNode node = (ast.IJSubNode)stack.pop();
    parseExpr ();
    node.addChild ((ast.IJSubNode)stack.pop());
    while (  sy == CKeyWords.SY_COMMA /* , */) {
      ts (CKeyWords.SY_COMMA);        // ,
      parseExpr ();
      node.addChild ((ast.IJSubNode)stack.pop());
    }
    stack.push (node);
    util.Trace.end ("ExprList");
  } // end of parseExprList

  void parseType() {
    util.Trace.begin ("Type");
    switch (sy) {
      case CKeyWords.SY_INT :   // int
        syType = ast.declaration.JSubObject.TINT;
        ts (CKeyWords.SY_INT);  // int
        break;
      case CKeyWords.SY_VOID :  // void
        syType = ast.declaration.JSubObject.TVOID;
        ts (CKeyWords.SY_VOID); // void
        break;
      default : error ("Unexpected Token");
    }
    util.Trace.end ("Type");
  } // end of parseType
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -