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

📄 identifierchecker.java

📁 java实现的一个pascal编译器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

  /**
   * checks if the identifier exists in any of the tables
   */
  public void outAIdentifierProcessActivationList(AIdentifierProcessActivationList node) {
    checkId(node.getIdentifier());
  }

  /**
   * same as above.
   */
  public void outASequenceProcessActivationList(ASequenceProcessActivationList node) {
    checkId(node.getIdentifier());
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void outARaiseStatement(ARaiseStatement node) {
    checkId(node.getIdentifier());
  }

 /**
   * @see IdentifierChecker.checkId
   */
  public void outAWaitStatement(AWaitStatement node) {
    checkId(node.getIdentifier());
  }

 /**
   * @see IdentifierChecker.checkId
   */
  public void outASignalStatement(ASignalStatement node) {
    checkId(node.getIdentifier());
  }

  /**
   * sets readln_statement to true
   */
  public void inAArgumentsReadlnStmt(AArgumentsReadlnStmt node) {
    readln_statement = true;
  }

  /**
   * sets readln_statement to false;
   */
  public void outAArgumentsReadlnStmt(AArgumentsReadlnStmt node) {
    readln_statement = false;
  }

  /**
   * checks if the identifier in the sleep statement was declared.
   */
  public void outAIdentifierValue(AIdentifierValue node) {
    checkId(node.getIdentifier());
  }

  /**
   * Checks if the exception handler has been declared.
   */
  public void outAExceptionHandler(AExceptionHandler node) {
    checkId(node.getIdentifier());
  }

  /**
   * checks if the identifier being sent as a message was
   * previously declared
   */
  public void outAChannelVariable(AChannelVariable node) {
    checkId(node.getIdentifier());
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void outAIdentifierProcedureCall(AIdentifierProcedureCall node) {
    TIdentifier ident = node.getIdentifier();
    if (monitor_call) {
      Monitor mon = (Monitor) global_table.get(monitor_name.toUpperCase());      
      String key = node.getIdentifier().getText().toUpperCase();
      if (!mon.getTable().containsKey(key)) { 
        err.report(1, ident.getLine(), ident.getPos());
      }
    }
    else {
      checkId(node.getIdentifier());
    }
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void inAArgumentsProcedureCall(AArgumentsProcedureCall node) {
    TIdentifier ident = node.getIdentifier();
    if (monitor_call) {
      Monitor mon;
      if (!global_table.containsKey(monitor_name.toUpperCase())) {
        err.report(35, ident.getLine(), ident.getPos());
      }
      else {
        mon = (Monitor) global_table.get(monitor_name.toUpperCase());      
      
        String key = node.getIdentifier().getText().toUpperCase();
        if (!mon.getTable().containsKey(key)) { 
          err.report(1, ident.getLine(), ident.getPos());
        }
      }
    }
    else {
      checkId(node.getIdentifier());
    }
  }

   /**
   * @see IdentifierChecker.checkId
   */
  public void outAReceiveStatement(AReceiveStatement node) {
    checkId(node.getIdentifier());
  }

  /**
   * checks if the monitor identifier was previously declared
   */
  public void inAMonitorCallStatement(AMonitorCallStatement node) {
    checkId(node.getIdentifier());
    monitor_call = true;
    monitor_name = node.getIdentifier().getText();
  }

  /** 
   * sets monitor_call to false
   */
  public void outAMonitorCallStatement(AMonitorCallStatement node) {
    monitor_call = false;
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void outAIdentifierFactor(AIdentifierFactor node) {
    checkId(node.getIdentifier());
  }


  /**
   * @see IdentifierChecker.checkId
   */
  public void outAFunctionCallFactor(AFunctionCallFactor node) {
    checkId(node.getIdentifier());
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void outAArrayFactor(AArrayFactor node) {
    checkId(node.getIdentifier());
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void outAArrayVariable(AArrayVariable node) {
    checkId(node.getIdentifier());
  }

  /**
   * @see IdentifierChecker.checkId
   */
  public void outAIdentifierVariable(AIdentifierVariable node) {
    checkId(node.getIdentifier());
  }

  /**
   * checks the headings for procedures or functions
   */
  private void checkProcedure(TIdentifier ident) {
    String key = ident.getText().toUpperCase();
    // this isn't a program declaration part?
    if (program) { // check if the identifier is equal to the outter
      // procedure, function, process or monitor
      if (key.equalsIgnoreCase(program_name)) {
        err.report(32, ident.getLine(), ident.getPos());
      }
      else { 
        if (global_table.containsKey(key)) {
          err.report(2, ident.getLine(), ident.getPos());
        }
        else {
          global_table.put(key, key);
        }
      }
      program = false;
      previous_entity = PROGRAM;
    } 
    else if (monitor) {// this isn't a inner procedure?
      if (monitor_table.containsKey(key)) {
        err.report(2, ident.getLine(), ident.getPos());
      }
      if (monitor_name.equalsIgnoreCase(key)) {
        err.report(29, ident.getLine(), ident.getPos());
      }
      monitor_table.put(key, key);
      monitor_procs.put(key, key);
      monitor = false;
      previous_entity = MONITOR;
    } 
  }

  /**
   * checks if the function was previously declared and
   * stores it in the global table if it wasn't
   */ 
  public void checkFunction(TIdentifier ident) {
    String key = ident.getText().toUpperCase();

    if (key.equalsIgnoreCase(program_name)) {
      err.report(32, ident.getLine(), ident.getPos());
    }
    else if (global_table.containsKey(key)) {
      err.report(2, ident.getLine(), ident.getPos());
    }
    else {
      global_table.put(key, key);
    }
    program = false;
  }
  
  /** 
   * checks if the process identifier already exists
   * and puts it in the table if it doesn't
   */
  public void checkProcess(TIdentifier ident) {
    String key = ident.getText().toUpperCase();
    if (key.equalsIgnoreCase(program_name)) {
      err.report(33, ident.getLine(), ident.getPos());
    }
    else { 
      if (global_table.containsKey(key)) {
        err.report(2, ident.getLine(), ident.getPos());
      }
      else {
        global_table.put(key, key);
      }
    }
  }

  /** 
   * checks if the monitor identifier already exists
   * and puts it in the table if it doesn't
   */
  public void checkMonitor(TIdentifier ident) {
    String key = ident.getText().toUpperCase();

    if (key.equalsIgnoreCase(program_name)) {
      err.report(33, ident.getLine(), ident.getPos());
    }
    else { 
      if (global_table.containsKey(key)) {
        err.report(2, ident.getLine(), ident.getPos());
      }
      else {
        global_table.put(key, new Monitor());
      }
    }
  }

  /**
   * If curret_block is true, it checks if the identifier was already declared.
   * If it was declared, it reports an error. If not, it puts it in the symbol
   * table. If the current_block is false, then it checks if the identifier was
   * already declared, and reports an error if the identifier was not declared.
   *
   * @param ident The identifier to be checked in the symbol table(s)
   * @param current_block true if the identifier is to be searched on the current block
   *                      only. False if it is to be checked on all the tables.
   */
  private void checkNewId(TIdentifier ident) {
    String key = ident.getText().toUpperCase();

    if (program) {
      // if the identifier already exists in the table then report an error
      if (global_table.containsKey(key)) {
        err.report(2, ident.getLine(), ident.getPos());
      }
      else { // add the identifier to the table
        global_table.put(key, key);
      }
    }
    else if (monitor) { // the identifier is to be checked for declaration
      if (monitor_table.containsKey(key)) {
        err.report(2, ident.getLine(), ident.getPos());
      }
      else {
        monitor_table.put(key, key);
      }
    }
    else {
      if (local_table.containsKey(key)) {
        err.report(2, ident.getLine(), ident.getPos());
      }
      else {
        local_table.put(key, key);
      }
    }
  }

  /**
   * checks if the identifier exists
   */
  public void checkId(TIdentifier ident) {
    String key = ident.getText().toUpperCase();
    
    if (program) {
      if (!global_table.containsKey(key)) {
          // report an error
          err.report(1, ident.getLine(), ident.getPos());
      }
    }
    else if (monitor) { // if this is subprogram, monitor or process then
      if (!monitor_table.containsKey(key)) {
        if (!global_table.containsKey(key)) {
          err.report(1, ident.getLine(), ident.getPos());
        }
      }
    }
    else {
      if (!local_table.containsKey(key)) {
        if (previous_entity == MONITOR) {
          if (!monitor_table.containsKey(key)) {
            if (!global_table.containsKey(key)) {
              err.report(1, ident.getLine(), ident.getPos());
            }
          }
        }
        else if (!global_table.containsKey(key)) {
            err.report(1, ident.getLine(), ident.getPos());
        }
      }
    }
  }

  /**
   * represents a monitor
   */
  static class Monitor {
    /**
     * procedures table
     */
    private Hashtable procs_table;

    /**
     * assigns the monitor_procs to proc_table
     */
    public void setTable(Hashtable procs) {
      procs_table = procs;
    }

    /**
     *returns the procedure table
     */
    public Hashtable getTable() {
      return procs_table;
    }
  }
}

⌨️ 快捷键说明

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