Example #1
0
 private Type retrieve(String id, CommonTree occ) {
   // Retrieve id's type from the type table.
   Type type = typeTable.get(id);
   if (type == null) {
     reportError(id + " is undeclared", occ);
     return Type.ERROR;
   } else return type;
 }
Example #2
0
  // $ANTLR start "proc_decl"
  // FunChecker.g:169:1: proc_decl : ( ^( PROC ID t= formal ( var_decl )* com ) | ^( FUNC t1= type
  // ID t2= formal ( var_decl )* com t3= expr ) );
  public final void proc_decl() throws RecognitionException {
    CommonTree ID2 = null;
    CommonTree PROC3 = null;
    CommonTree ID4 = null;
    CommonTree FUNC5 = null;
    Type t = null;

    Type t1 = null;

    Type t2 = null;

    Type t3 = null;

    try {
      // FunChecker.g:170:2: ( ^( PROC ID t= formal ( var_decl )* com ) | ^( FUNC t1= type ID t2=
      // formal ( var_decl )* com t3= expr ) )
      int alt5 = 2;
      int LA5_0 = input.LA(1);

      if ((LA5_0 == PROC)) {
        alt5 = 1;
      } else if ((LA5_0 == FUNC)) {
        alt5 = 2;
      } else {
        NoViableAltException nvae = new NoViableAltException("", 5, 0, input);

        throw nvae;
      }
      switch (alt5) {
        case 1:
          // FunChecker.g:170:4: ^( PROC ID t= formal ( var_decl )* com )
          {
            PROC3 = (CommonTree) match(input, PROC, FOLLOW_PROC_in_proc_decl102);

            match(input, Token.DOWN, null);
            ID2 = (CommonTree) match(input, ID, FOLLOW_ID_in_proc_decl108);

            typeTable.enterLocalScope();

            pushFollow(FOLLOW_formal_in_proc_decl122);
            t = formal();

            state._fsp--;

            Type proctype = new Type.Mapping(t, Type.VOID);
            define((ID2 != null ? ID2.getText() : null), proctype, PROC3);
            // ... to enable recursion

            // FunChecker.g:180:5: ( var_decl )*
            loop3:
            do {
              int alt3 = 2;
              int LA3_0 = input.LA(1);

              if ((LA3_0 == VAR)) {
                alt3 = 1;
              }

              switch (alt3) {
                case 1:
                  // FunChecker.g:180:5: var_decl
                  {
                    pushFollow(FOLLOW_var_decl_in_proc_decl134);
                    var_decl();

                    state._fsp--;
                  }
                  break;

                default:
                  break loop3;
              }
            } while (true);

            pushFollow(FOLLOW_com_in_proc_decl141);
            com();

            state._fsp--;

            match(input, Token.UP, null);

            typeTable.exitLocalScope();
            // Enter the function in the local
            // scope, to enable it to be recursive:
            define((ID2 != null ? ID2.getText() : null), proctype, PROC3);
          }
          break;
        case 2:
          // FunChecker.g:187:4: ^( FUNC t1= type ID t2= formal ( var_decl )* com t3= expr )
          {
            FUNC5 = (CommonTree) match(input, FUNC, FOLLOW_FUNC_in_proc_decl154);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_type_in_proc_decl162);
            t1 = type();

            state._fsp--;

            ID4 = (CommonTree) match(input, ID, FOLLOW_ID_in_proc_decl168);

            typeTable.enterLocalScope();

            pushFollow(FOLLOW_formal_in_proc_decl182);
            t2 = formal();

            state._fsp--;

            Type functype = new Type.Mapping(t2, t1);
            define((ID4 != null ? ID4.getText() : null), functype, FUNC5);
            // ... to enable recursion

            // FunChecker.g:198:5: ( var_decl )*
            loop4:
            do {
              int alt4 = 2;
              int LA4_0 = input.LA(1);

              if ((LA4_0 == VAR)) {
                alt4 = 1;
              }

              switch (alt4) {
                case 1:
                  // FunChecker.g:198:5: var_decl
                  {
                    pushFollow(FOLLOW_var_decl_in_proc_decl194);
                    var_decl();

                    state._fsp--;
                  }
                  break;

                default:
                  break loop4;
              }
            } while (true);

            pushFollow(FOLLOW_com_in_proc_decl201);
            com();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_proc_decl209);
            t3 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            typeTable.exitLocalScope();
            define((ID4 != null ? ID4.getText() : null), functype, FUNC5);
            checkType(t1, t3, FUNC5);
          }
          break;
      }
    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
      // do for sure before leaving
    }
    return;
  }
Example #3
0
 private void define(String id, Type type, CommonTree decl) {
   // Add id with its type to the type table, checking
   // that id is not already declared in the same scope.
   boolean ok = typeTable.put(id, type);
   if (!ok) reportError(id + " is redeclared", decl);
 }
Example #4
0
 private void predefine() {
   // Add predefined procedures to the type table.
   typeTable.put("read", new Type.Mapping(Type.VOID, Type.INT));
   typeTable.put("write", new Type.Mapping(Type.INT, Type.VOID));
 }