예제 #1
0
  /**
   * 解析标识符
   *
   * @param token
   * @return 对应符号表项
   * @throws Exception
   */
  private SymTabEntry parseIdentifier(Token token) throws Exception {
    SymTabEntry id = null;

    if (token.getType() == IDENTIFIER) {
      String name = token.getText().toLowerCase();
      id = symTabStack.lookupLocal(name);

      // 申明必须得没有,否则重定义了
      if (id == null) {
        id = symTabStack.enterLocal(name);
        id.setDefinition(definition);
        id.appendLineNumber(token.getLineNumber());
      } else {
        errorHandler.flag(token, IDENTIFIER_REDEFINED, this);
      }

      token = nextToken();
    } else {
      errorHandler.flag(token, MISSING_IDENTIFIER, this);
    }

    return id;
  }
예제 #2
0
  /**
   * 解析标识符子表及类型说明
   *
   * @param token the current token.
   * @return 一个申明中的子表标识符列表
   * @throws Exception
   */
  protected ArrayList<SymTabEntry> parseIdentifierSublist(Token token) throws Exception {
    ArrayList<SymTabEntry> sublist = new ArrayList<SymTabEntry>();

    do {
      token = synchronize(IDENTIFIER_START_SET);
      SymTabEntry id = parseIdentifier(token);

      if (id != null) {
        sublist.add(id);
      }

      token = synchronize(COMMA_SET);
      TokenType tokenType = token.getType();

      // 找逗号
      if (tokenType == COMMA) {
        token = nextToken(); // 有逗号吞掉以便下一个

        if (IDENTIFIER_FOLLOW_SET.contains(token.getType())) {
          errorHandler.flag(token, MISSING_IDENTIFIER, this);
        }
      } else if (IDENTIFIER_START_SET.contains(tokenType)) {
        errorHandler.flag(token, MISSING_COMMA, this);
      }
    } while (!IDENTIFIER_FOLLOW_SET.contains(token.getType()));

    // 冒号后面的类型
    TypeSpec type = parseTypeSpec(token);

    // 将子表中的每个变量的类型设置上,比如a,b,c:int,先解析了a,b,c,然后解析了int类型
    // 那么这儿就会给a,b,c设置上int类型
    for (SymTabEntry variableId : sublist) {
      variableId.setTypeSpec(type);
    }

    return sublist;
  }
예제 #3
0
  /**
   * Parse type definitions.
   *
   * @param token the initial token.
   * @throws Exception if an error occurred.
   */
  public void parse(Token token) throws Exception {
    token = synchronize(IDENTIFIER_SET);

    // Loop to parse a sequence of type definitions
    // separated by semicolons.
    while (token.getType() == IDENTIFIER) {
      String name = token.getText().toLowerCase();
      SymTabEntry typeId = symTabStack.lookupLocal(name);

      // Enter the new identifier into the symbol table
      // but don't set how it's defined yet.
      if (typeId == null) {
        typeId = symTabStack.enterLocal(name);
        typeId.appendLineNumber(token.getLineNumber());
      } else {
        errorHandler.flag(token, IDENTIFIER_REDEFINED, this);
        typeId = null;
      }

      token = nextToken(); // consume the identifier token

      // Synchronize on the = token.
      token = synchronize(EQUALS_SET);
      if (token.getType() == EQUALS) {
        token = nextToken(); // consume the =
      } else {
        errorHandler.flag(token, MISSING_EQUALS, this);
      }

      // Parse the type specification.
      TypeSpecificationParser typeSpecificationParser = new TypeSpecificationParser(this);
      TypeSpec type = typeSpecificationParser.parse(token);

      // Set identifier to be a type and set its type specificationt.
      if (typeId != null) {
        typeId.setDefinition(TYPE);
      }

      // Cross-link the type identifier and the type specification.
      if (type != null && typeId != null) {
        if (type.getIdentifier() == null) {
          type.setIdentifier(typeId);
        }
        typeId.setTypeSpec(type);
      } else {
        token = synchronize(FOLLOW_SET);
      }

      token = currentToken();
      TokenType tokenType = token.getType();

      // Look for one or more semicolons after a definition.
      if (tokenType == SEMICOLON) {
        while (token.getType() == SEMICOLON) {
          token = nextToken(); // consume the ;
        }
      }

      // If at the start of the next definition or declaration,
      // then missing a semicolon.
      else if (NEXT_START_SET.contains(tokenType)) {
        errorHandler.flag(token, MISSING_SEMICOLON, this);
      }

      token = synchronize(IDENTIFIER_SET);
    }
  }