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

📄 typechecker.java

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

  /**
   * constant =
   *   ......... |
   *   {string} string ;
   *   ......... ;
   * sets type to char or string.
   */
  public void outAStringConstant(AStringConstant node) {
    TCharacterLiteral char_lit = node.getCharacterLiteral();
    String image = char_lit.getText();
   
    if (image.length() <= 3) {
      type = CHAR;
    }   
    else {
      type = STRING;
    }
  }

  /**
   * var_decl =
   *   identifier_list colon P.type semicolon ;
   *  empties idlist if it has some elements
   */
  public void inAVarDecl(AVarDecl node) {
    if (!idlist.isEmpty()) {
      idlist.removeAllElements();
    }
  }

  /**
   * puts all the identifiers in idlist in the 
   * corresponding symbol table
   */
  public void outAVarDecl(AVarDecl node) {
    TIdentifier ident;
    String key;
    for (Enumeration e = idlist.elements(); e.hasMoreElements(); ) {
      ident = (TIdentifier) e.nextElement();
      key = ident.getText().toUpperCase();
      if (program) {
        global_table.put(key, new Variable(type));
      }
      else if (monitor) {
        monitor_table.put(key, new Variable(type));
      }
      else {
        local_table.put(key, new Variable(type));
      }
    }
  }

  /**
   * adds the identifier list to the corresponding symbol table.
   *
  /**
   * identifier_list =
   *   {single} identifier |
   *   .......... ;
   * puts the identifier in the idlist
   */
  public void outASingleIdentifierList(ASingleIdentifierList node) {
    idlist.addElement(node.getIdentifier());
  }

  /**
   * identifier_list =
   *   ............. |
   *   {sequence} identifier_list comma identifier ;
   *
   * @see TypeChecker.outASingleIdentifierList
   */
  public void outASequenceIdentifierList(ASequenceIdentifierList node) {
    idlist.addElement(node.getIdentifier());
  }

  /**
   * procedure_heading =
   *   {simple} identifier |
   *   ............. ;
   * puts the procedure identifier in the corresponding table.
   */
  public void outASimpleProcedureHeading(ASimpleProcedureHeading node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();
    
    if (program) {
      global_table.put(key, new Procedure(false, false));
      program = false;
      previous_entity = PROGRAM;
    }
    else if (monitor) {
      if (private_modifier) {
        monitor_table.put(key, new Procedure(false, true));
        private_modifier = false;
      }
      else {
        monitor_table.put(key, new Procedure(false, false));
      }
      monitor = false;
      previous_entity = MONITOR;
    }
    if (!local_table.isEmpty()) {
      local_table.clear();
    }
  }

  /**
   * removes all elements in the idlist 
   */
  public void inASimpleThrowsProcedureHeading(ASimpleThrowsProcedureHeading node) {
    if (!idlist.isEmpty()) {
      idlist.removeAllElements();
    }
  }

  /** 
   * procedure_heading =
   *   ............ |
   *   {simple_throws} procedure identifier semicolon throws 
   *     identifier_list semicolon |
   *   .............. ;
   */
  public void outASimpleThrowsProcedureHeading(ASimpleThrowsProcedureHeading node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (program) {
      global_table.put(key, new Procedure(true, false));
      program = false;
      previous_entity = PROGRAM;
    }
    else if (monitor) {
      if (private_modifier) {
        monitor_table.put(key, new Procedure(true, true));
        private_modifier = false;
      }
      else {
        monitor_table.put(key, new Procedure(true, false));
      }
      monitor = false;
      previous_entity = MONITOR;
    }
    if (!throws_list.isEmpty()) {
      throws_list.clear();
    }
    for (Enumeration e = idlist.elements(); e.hasMoreElements();) {
      TIdentifier ident1 = (TIdentifier) e.nextElement();
      String key1 = ident1.getText().toUpperCase();

      if (!global_table.containsKey(key1) &&
          !(global_table.get(key) instanceof Variable) &&
          (( (Variable) global_table.get(key1) ).getType() != EXCEPTION)) {
        err.report(8, ident1.getLine(), ident1.getPos());
      }
      throws_list.put(key1, key1);
    }
    if (!local_table.isEmpty()) {
      local_table.clear();
    }
  }

  /**
   * creates a new instance of class Vector
   * if it is not empty
   */
  public void inAArgumentsProcedureHeading(AArgumentsProcedureHeading node) {
    arguments = new Vector();
    if (!local_table.isEmpty()) {
      local_table.clear();
    }
    arguments = new Vector();
  }

  /**
   * procedure_heading =
   *   ........... |
   *   {arguments} procedure identifier l_paren identifier_list r_paren
   *       semicolon |
   *    ............ ;
   * @see TypeChecker.outASimpleProcedureHeading
   */
  public void outAArgumentsProcedureHeading(AArgumentsProcedureHeading node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (program) {
      global_table.put(key, new Procedure((Vector)arguments.clone(), false, false));
      program = false;
      previous_entity = PROGRAM;
    }
    else if (monitor) {
      if (private_modifier) {
        monitor_table.put(key, new Procedure((Vector)arguments.clone(), false,true));
        private_modifier = false;
      }
      else {
        monitor_table.put(key, new Procedure((Vector)arguments.clone(), false, false));
      }
      monitor = false;
      previous_entity = MONITOR;
    }
  }

  /**
   * procedure_heading =
   *   ............. |
   *   {arguments_throws} procedure identifier l_paren parameter_list 
   *      r_paren semicolon throws identifier_list semicolon ;
   *
   * @see TypeChecker.outAArgumentsProcedureHeading
   */
  public void caseAArgumentsThrowsProcedureHeading(
          AArgumentsThrowsProcedureHeading node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();

    if (!arguments.isEmpty()) {
      arguments.removeAllElements();
    }
    if (node.getParameterList() != null) {
      node.getParameterList().apply(this);
    }

    if (!idlist.isEmpty()) {
      idlist.removeAllElements();
    }

    if (node.getIdentifierList() != null) {
      node.getIdentifierList().apply(this);
    }

    if (!throws_list.isEmpty()) {
      throws_list.clear();
    }

    for (Enumeration e = idlist.elements(); e.hasMoreElements();) {
      TIdentifier ident1 = (TIdentifier) e.nextElement();
      String key1 = ident1.getText().toUpperCase();

      if (!global_table.containsKey(key1) ||
          !(global_table.get(key1) instanceof Variable) ||
          (( (Variable) global_table.get(key1) ).getType() != EXCEPTION)) {
        err.report(8, ident.getLine(), ident.getPos());
      }
      throws_list.put(key1, key1);
    }
     
    if (program) {
      global_table.put(key, new Procedure((Vector) arguments.clone(), true, false));
      program = false;
      previous_entity = PROGRAM;
    } 
    else if (monitor) {
      if (private_modifier) {
        monitor_table.put(key, new Procedure((Vector)arguments.clone(), true, true));
        private_modifier = false;
      }
      else {
        monitor_table.put(key, new Procedure((Vector) arguments.clone(), true, false));
      }
      monitor = false;
      previous_entity = MONITOR;
    }   
  }    

  /**
   * removes all elements in idlist
   */
  public void inAFormalParameter(AFormalParameter node) {
    if(!idlist.isEmpty()) {
      idlist.removeAllElements();
    }
  }

  /**
   * formal_parameter = 
   *   identifier_list colon P.type ;
   *
   * adds to local table all the elements in the list with type, and 
   * adds the number of identifiers times type to the arguments list.
   */
  public void outAFormalParameter(AFormalParameter node) {
    String key;

    for (Enumeration e = idlist.elements(); e.hasMoreElements(); ) {
      key = ((TIdentifier) e.nextElement()).getText().toUpperCase();
      arguments.addElement(type);
      local_table.put(key, new Variable(type));
    }
  }

  /**
   * function_heading =
   *   {simple} function identifier colon P.type semicolon |
   * adds a new function to global_table.
   */
  public void outASimpleFunctionHeading(ASimpleFunctionHeading node) {
    String key = node.getIdentifier().getText().toUpperCase();
    current_function = node.getIdentifier().getText();
    global_table.put(key, new Function(false, type));
    function = true;
    program = false;
  }

  /**
   * clears the identifier list
   */
  public void inASimpleThrowsFunctionHeading(ASimpleThrowsFunctionHeading node) {
    if (!idlist.isEmpty()) {
      idlist.removeAllElements();
    }
  }

  /**
   * adds a new function to global table
   */
  public void outASimpleThrowsFunctionHeading(ASimpleThrowsFunctionHeading node) {
    TIdentifier ident = node.getIdentifier();
    String key = ident.getText().toUpperCase();
    current_function = ident.getText();

    for (Enumeration e = idlist.elements(); e.hasMoreElements();) {
      TIdentifier ident1 = (TIdentifier) e.nextElement();
      String key1 = ident1.getText().toUpperCase();

      if (!global_table.containsKey(key1) ||
          !(global_table.get(key1) instanceof Variable) ||
          (((Variable) global_table.get(key1)).getType() != EXCEPTION)) {
        err.report(8, ident1.getLine(), ident1.getPos());
      }
      throws_list.put(key1, key1);
    }
    global_table.put(key, new Function(true, type));
    function = true;
    program = false;
  }

  /**
   * clears the arguments Vector
   */
  public void inAArgumentsThrowsFunctionHeading(AArgumentsThrowsFunctionHeading node) {
    if (!arguments.isEmpty()) {
      arguments.removeAllElements();
    }
  }

  /**
   * adds a new function to the global table
   */
  public void outAArgumentsFunctionHeading(AArgumentsFunctionHeading node) {
    String key = node.getIdentifier().getText().toUpperCase();
    current_function = node.getIdentifier().getText().toUpperCase();

    global_table.put(key, new Function((Vector) arguments.clone(), false, type));
    program = false;
    function = true;
  }

  /**
   * @see TypeChecker.outAArgumentsFunctionHeading
   */
  public void caseAArgumentsThrowsFunctionHeading(
                  AArgumentsThrowsFunctionHeading node) {
    String key = node.getIdentifier().getText().toUpperCase();
    current_function = node.getIdentifier().getText();

    if (!local_table.isEmpty()) {
      local_table.clear();
    }
    if (!arguments.isEmpty()) {
      arguments.removeAllElements();
    }
    if (node.getParameterList() != null) {
      node.getParameterList().apply(this);
    }
    
    if (node.getType() != null) {
      node.getType().apply(this);
    }

    if (!idlist.isEmpty()) {
      idlist.removeAllElements(); 
    }
    if (node.getIdentifierList() != null) {
      node.getIdentifierList().apply(this);
    }

    if (!throws_list.isEmpty()) {
      throws_list.clear();
    }

    for (Enumeration e = idlist.elements(); e.hasMoreElements();) {
      TIdentifier ident1 = (TIdentifier) e.nextElement();
      String key1 = ident1.getText().toUpperCase();

      if (!global_table.containsKey(key1) ||
          !(global_table.get(key1) instanceof Variable) ||
          (((Variable) global_table.get(key1)).getType() != EXCEPTION)) {
        err.report(8, ident1.getLine(), ident1.getPos());
      }
      throws_list.put(key1, key1);
    }
    global_table.put(key, new Function((Vector) arguments.clone(), true, type));
    program = false;
    function = true;
  }           

  /**
   * sets program to if previous_entity is equal to PROGRAM
   * , else sets monitor to true;
   */
  public void outAProcedureDeclaration(AProcedureDeclaration node) {
    if (previous_entity == PROGRAM) {
      program = true;
    }
    else {
      monitor = true;
    }
  }

  /**
   * sets program to true
   */
  public void outAFunctionDeclaration(AFunctionDeclaration node) {
    program = true;

⌨️ 快捷键说明

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