예제 #1
0
  protected void parseFormalParameters(Token token, SymTabEntry routineId) throws Exception {
    // Parse the formal parameters if there is an opening left parenthesis.
    token = synchronize(LEFT_PAREN_SET);
    if (token.getType() == LEFT_PAREN) {
      token = nextToken(); // consume (
      TokenType tokenType1 = token.getType();

      ArrayList<SymTabEntry> parms = new ArrayList<SymTabEntry>();
      while (tokenType1 != RIGHT_PAREN) {

        parms.addAll(parseParmSublist(token, routineId));

        token = currentToken();

        tokenType1 = token.getType();
      }

      // This sync needs to check for Int or Char
      token = synchronize(PARAMETER_SET);

      // Closing right parenthesis.
      if (token.getType() == RIGHT_PAREN) {
        token = nextToken(); // consume )
      } else {
        errorHandler.flag(token, MISSING_RIGHT_PAREN, this);
      }

      routineId.setAttribute(ROUTINE_PARMS, parms);
    }
  }
예제 #2
0
  public SymTabEntry parse(Token token, SymTabEntry parentId) throws Exception {

    Token FunctionReturnToken = token;

    Definition routineDefn = null;
    String dummyName = null;
    SymTabEntry routineId = null;
    TokenType routineType = token.getType();

    // Initialize.
    switch ((WookieTokenType) routineType) {
      case INT:
        {
          // save the int data type for use later
          FunctionReturnToken = token;
          token = nextToken(); // consume Int		
          routineDefn = DefinitionImpl.FUNCTION;
          dummyName = "DummyProgramName".toLowerCase() + String.format("%03d", ++dummyCounter);
          break;
        }

      case CHAR:
        {
          // save the char data type for use later
          FunctionReturnToken = token;
          token = nextToken(); // consume Char
          routineDefn = DefinitionImpl.FUNCTION;
          dummyName = "DummyProgramName".toLowerCase() + String.format("%03d", ++dummyCounter);
          break;
        }

      case VOID:
        {
          // save the void data type for use later
          FunctionReturnToken = token;
          token = nextToken(); // consume PROGRAM
          routineDefn = DefinitionImpl.PROCEDURE;
          dummyName = "DummyProgramName".toLowerCase() + String.format("%03d", ++dummyCounter);
          break;
        }

      default:
        {
          routineDefn = DefinitionImpl.PROGRAM;
          dummyName = "DummyProgramName".toLowerCase();
          break;
        }
    }

    // Parse the routine name.
    routineId = parseRoutineName(token, dummyName);

    routineId.setDefinition(routineDefn);
    token = currentToken();

    // Create new intermediate code for the routine.
    ICode iCode = ICodeFactory.createICode();
    routineId.setAttribute(ROUTINE_ICODE, iCode);
    routineId.setAttribute(ROUTINE_ROUTINES, new ArrayList<SymTabEntry>());

    SymTab symTab = symTabStack.push();

    routineId.setAttribute(ROUTINE_SYMTAB, symTab);

    ((SymTabImpl) symTab).setfuncName(routineId.getName());

    parseFormalParameters(token, routineId);
    token = currentToken();

    if (FunctionReturnToken.getType() == INT) {
      routineId.setTypeSpec(integerType);
    } else if (FunctionReturnToken.getType() == CHAR) {
      routineId.setTypeSpec(charType);
    }

    // Parse the routine's block  declaration.
    if (token.getType() == LEFT_BRACE) {

      routineId.setAttribute(ROUTINE_CODE, DECLARED);
      BlockParser blockParser = new BlockParser(this);

      ICodeNode rootNode = blockParser.parse(token, routineId);
      iCode.setRoot(rootNode);
    }
    ArrayList<SymTabEntry> subroutines =
        (ArrayList<SymTabEntry>) parentId.getAttribute(ROUTINE_ROUTINES);
    subroutines.add(routineId);
    // Pop the routine's symbol table off the stack.

    symTabStack.pop();

    return routineId;
  }