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

📄 javasourcegenerator.java

📁 java实现的一个pascal编译器
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        node.getTerm().apply(this);
      }
  }

  /**
   * {not_equal} expression ne term |
   */
  public void caseANotEqualExpression(ANotEqualExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" != ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {less_than} expression lt term |
   */
  public void caseALessThanExpression(ALessThanExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" < ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {greater_than} expression gt term |
   */
  public void caseAGreaterThanExpression(AGreaterThanExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" > ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {less_or_equal} expression le term |
   */
  public void caseALessOrEqualExpression(ALessOrEqualExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" <= ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * {greater_or_equal} expression ge term ;
   */
  public void caseAGreaterOrEqualExpression(AGreaterOrEqualExpression node) {
      if (node.getExpression() != null) {
        node.getExpression().apply(this);
      }
      out.print(" >= ");
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
  }

  /**
   * term =
   *  {factor} factor |
   *  {mult} term mult factor |
   */
  public void caseAMultTerm(AMultTerm node) {
      if (node.getTerm() != null) {
        node.getTerm().apply(this);
      }
      out.print(" * ");
      if (node.getFactor() != null) {
        node.getFactor().apply(this);
      }
  }

  /**
   * {divide} term divide factor |
   */
  public void caseADivideTerm(ADivideTerm node) {
    if (node.getTerm() != null) {
      node.getTerm().apply(this);
    }
    out.print(" / ");
    if (node.getFactor() != null) {
      node.getFactor().apply(this);
    }
  }

  /**
   * {div} term div factor |
   */
  public void caseADivTerm(ADivTerm node) {
    if (node.getTerm() != null) {
      node.getTerm().apply(this);
    }
    out.print(" / ");
    if (node.getFactor() != null) {
      node.getFactor().apply(this);
    }
  }

  /**
   * {mod} term mod factor |
   */
  public void caseAModTerm(AModTerm node) {
    if (node.getTerm() != null) {
        node.getTerm().apply(this);
    }
    out.print(" % ");
    if (node.getFactor() != null) {
      node.getFactor().apply(this);
    }
  }

  /**
   * {and} term and factor ;
   */
  public void caseAAndTerm(AAndTerm node) {
    if (node.getTerm() != null) {
      node.getTerm().apply(this);
    }
    out.print(" && ");
    if (node.getFactor() != null) {
      node.getFactor().apply(this);
    }
  }

  /**
   * factor =
   * {identifier} identifier | // function or identifier variable
   */
  public void outAIdentifierFactor(AIdentifierFactor node) {
      String key = node.getIdentifier().getText().toUpperCase();
      Object entry;

      if (program) {
        entry = global_table.get(key);
      }
      else if (monitor) {
        if  (monitor_table.containsKey(key)) {
          entry = monitor_table.get(key);
        }
        else {
          entry = global_table.get(key);
        }
      }
      else {
        if (local_table.containsKey(key)) {
          entry = local_table.get(key);
        }
        else if (previous_entity == MONITOR) {
          if (monitor_table.containsKey(key)) {
            entry = monitor_table.get(key);
          }
          else {
            entry = global_table.get(key);
          }
        }
        else {
          entry = global_table.get(key);
        }
      }

      if (entry instanceof Variable) {
        out.print(((Variable) entry).getImage());
      }
      else if (entry instanceof Constant) {
        out.print(((Constant) entry).getImage());
      }
      else {
        out.print(((Function) entry).getImage() + "()");
      }
  }

  /**
   * {array} identifier l_bracket expression_list r_bracket |
   */
  public void inAArrayFactor(AArrayFactor node) {
      String key = node.getIdentifier().getText().toUpperCase();
      String image;
      if (program) {
        image = ((Variable) global_table.get(key)).getImage();
      }
      else {
        if (monitor) {
          image = ((Variable) monitor_table.get(key)).getImage();
        }
        else {
          image = ((Variable) local_table.get(key)).getImage();
        }
      }
      out.print(image +"[");
   }

  public void outAArrayFactor(AArrayFactor node) {
    out.print(" - 1]");
  }

 /**
  * {integer} integer_literal |
  */
  public void outAIntegerFactor(AIntegerFactor node) {
    String int_value = node.getIntegerLiteral().getText();
    out.print(int_value);
  }

  /**
   * {real} real_literal |
   */
  public void outARealFactor(ARealFactor node) {
    String real_value = node.getRealLiteral().getText();
    out.print(real_value);
  }

  /**
   * {string} character_literal |
   */
  public void outAStringFactor(AStringFactor node) {
      String str_value = node.getCharacterLiteral().getText();
      if (str_value.length() <= 3) {
        out.print("'" + str_value.substring(1,2) + "'");
      }
      else {
        out.print("\"" + str_value.substring(1, str_value.length() - 1) + "\"");
      }
  }

  /**
   * {function_call} identifier l_paren expression_list r_paren |
   */
  public void inAFunctionCallFactor(AFunctionCallFactor node) {
      String key = node.getIdentifier().getText().toUpperCase(),
          func_image;

      func_image = ((Function) global_table.get(key)).getImage();
      out.print(func_image + "(");
    }

    public void outAFunctionCallFactor(AFunctionCallFactor node) {
      out.print(")");
    }

  /**
   * {expression} l_paren expression r_paren |
   */
  public void inAExpressionFactor(AExpressionFactor node) {
    out.print("(");
  }

  public void outAExpressionFactor(AExpressionFactor node) {
    out.print(")");
  }

  /**
   * {char_cast} char l_paren expression r_paren |
   */
  public void inACharCastFactor(ACharCastFactor node) {
    out.print(" (char) (");
  }

  public void outACharCastFactor(ACharCastFactor node) {
    out.print(")");
  }

  /**
   * {int_cast} integer l_paren expression r_paren |
   */
  public void inAIntCastFactor(AIntCastFactor node) {
    out.print(" (int) (");
  }

  public void outAIntCastFactor(AIntCastFactor node) {
    out.print(")");
  }

  /**
   * {real_cast} real l_paren expression r_paren |
   */
  public void inARealCastFactor(ARealCastFactor node) {
    out.print(" (double) (");
  }

  /**
   */
  public void outARealCastFactor(ARealCastFactor node) {
    out.print(")");
  }

  /**
   * {true} true |
   */
  public void outATrueFactor(ATrueFactor node) {
    out.print("true");
  }

  /**
   * {false} false |
   */
  public void outAFalseFactor(AFalseFactor node) {
    out.print("false");
  }

  /**
   * {negation} not factor ;
   */
  public void inANegationFactor(ANegationFactor node) {
    out.print(" !");
  }

  /**
   * variable =
   *  {identifier} identifier |
   */
  public void outAIdentifierVariable(AIdentifierVariable node) {
    String key = node.getIdentifier().getText().toUpperCase();
    Object ident;

    if (program) {
      out.print("    ");
      ident = global_table.get(key);
    }
    else if (monitor) {
        out.print("    ");
        if (monitor_table.containsKey(key)) {
          ident = monitor_table.get(key);
        }
        else {
          ident = global_table.get(key);
        }
    }
    else if (process) {
      out.print("    ");
      if (local_table.containsKey(key)) {
        ident = local_table.get(key);
      }
      else {
        ident = global_table.get(key);
      }
    }
    else {
      out.print("    ");
      if (local_table.containsKey(key)) {
        ident = local_table.get(key);
      }
      else if (previous_entity == MONITOR) {
        ident = monitor_table.get(key);
      }
      else {
        ident = global_table.get(key);
      }
    }
    if (ident instanceof Variable) {
      out.print(((Variable) ident).getImage());
    }
    else {
      function_assign = true;
    }
  }

  /**
   * {array} identifier l_bracket expression_list r_bracket ;
   */
  public void inAArrayVariable(AArrayVariable node) {
    String key = node.getIdentifier().getText().toUpperCase();
    String image;
    if (program) {
        image = ((Variable) global_table.get(key)).getImage();
    }
    else if (monitor) {
      if (monitor_table.containsKey(key)) {
        image = ((Variable) monitor_table.get(key)).getImage();
      }
      else {
        image = ((Variable) global_table.get(key)).getImage();
      }
    }
    else {
      if (local_table.containsKey(key)) {
        image = ((Variable) local_table.get(key)).getImage();
      }
      else if (previous_entity == MONITOR) {
        image = ((Variable) monitor_table.get(key)).getImage();
      }
      else {
        image = ((Variable) global_table.get(key)).getImage();
      }
    }
    out.print("    " + image +"[");
  }

  public void outAArrayVariable(AArrayVariable node) {
    out.print(" - 1]");
  }
  
  /**
   * flushes everything to the file
   */
  public void outAProgram(AProgram node) {
    out.flush();
  }
}


/** 
 * Represents an entry in the symbol table
 */
class Entry {
  /**
   * entity's image
   */
  private String image;

  /**
   * entity's type
   */
  private String type;
 
  /**
   * Default constructor
   */
  public Entry(String img, String typ) {
    type = typ;
    image = img;
  }

  /**
   * Returns the entity's image
   */
  public String getImage() {
    return image;
  }

  /**
   * Returns the entity's type
   */
  public String getType() {
    return type;
  }
}

/**
 * Represents a Pascal Procedure
 */
class Procedure extends Entry {
  public Procedure(String image) {
    super(image, null);
  }
}

/**
 * Represents a Pascal Function
 */
class Function extends Entry {
  public Function(String image, String type) {
    super(image, type);
  }
}

/**
 * Represents a JPascal Process
 */
class Process extends Entry {
  public Process(String image) {
    super(image, null);
  }
}

/**
 * Represents a JPascal Monitor
 */
class Monitor extends Entry {
  public Monitor(String image) {
    super(image, null);
  }
}

/**
 * Represents a Pascal constant
 */
class Constant extends Entry {
  public Constant(String image, String type) {
    super(image, type);
  }
}

/**
 * Represents a Pascal Variable
 */
class Variable extends Entry {
  public Variable(String image, String type) {
    super(image, type);
  }
}








⌨️ 快捷键说明

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