예제 #1
0
파일: DefRef.java 프로젝트: chuzui/antlr
  // $ANTLR start "assignment"
  // DefRef.g:107:1: assignment : ^(eq= '=' member . ) ;
  public final void assignment() throws RecognitionException {
    CommonTree eq = null;
    Type member6 = null;

    try {
      // DefRef.g:108:5: ( ^(eq= '=' member . ) )
      // DefRef.g:108:9: ^(eq= '=' member . )
      {
        eq = (CommonTree) match(input, 27, FOLLOW_27_in_assignment473);
        if (state.failed) return;

        match(input, Token.DOWN, null);
        if (state.failed) return;
        pushFollow(FOLLOW_member_in_assignment475);
        member6 = member();

        state._fsp--;
        if (state.failed) return;
        matchAny(input);
        if (state.failed) return;

        match(input, Token.UP, null);
        if (state.failed) return;
        if (state.backtracking == 1) {

          System.out.println("line " + eq.getLine() + ": assign to type " + member6.getName());
        }
      }

    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return;
  }
예제 #2
0
  // $ANTLR start "expr"
  // FunChecker.g:268:1: expr returns [Type type] : ( FALSE | TRUE | NUM | ID | ^( FUNCCALL ID t=
  // expr ) | ^( EQ t1= expr t2= expr ) | ^( LT t1= expr t2= expr ) | ^( GT t1= expr t2= expr ) | ^(
  // PLUS t1= expr t2= expr ) | ^( MINUS t1= expr t2= expr ) | ^( TIMES t1= expr t2= expr ) | ^( DIV
  // t1= expr t2= expr ) | ^( NOT t= expr ) | NOACTUAL );
  public final Type expr() throws RecognitionException {
    Type type = null;

    CommonTree ID19 = null;
    CommonTree ID20 = null;
    CommonTree FUNCCALL21 = null;
    CommonTree EQ22 = null;
    CommonTree LT23 = null;
    CommonTree GT24 = null;
    CommonTree PLUS25 = null;
    CommonTree MINUS26 = null;
    CommonTree TIMES27 = null;
    CommonTree DIV28 = null;
    CommonTree NOT29 = null;
    Type t = null;

    Type t1 = null;

    Type t2 = null;

    try {
      // FunChecker.g:269:2: ( FALSE | TRUE | NUM | ID | ^( FUNCCALL ID t= expr ) | ^( EQ t1= expr
      // t2= expr ) | ^( LT t1= expr t2= expr ) | ^( GT t1= expr t2= expr ) | ^( PLUS t1= expr t2=
      // expr ) | ^( MINUS t1= expr t2= expr ) | ^( TIMES t1= expr t2= expr ) | ^( DIV t1= expr t2=
      // expr ) | ^( NOT t= expr ) | NOACTUAL )
      int alt10 = 14;
      switch (input.LA(1)) {
        case FALSE:
          {
            alt10 = 1;
          }
          break;
        case TRUE:
          {
            alt10 = 2;
          }
          break;
        case NUM:
          {
            alt10 = 3;
          }
          break;
        case ID:
          {
            alt10 = 4;
          }
          break;
        case FUNCCALL:
          {
            alt10 = 5;
          }
          break;
        case EQ:
          {
            alt10 = 6;
          }
          break;
        case LT:
          {
            alt10 = 7;
          }
          break;
        case GT:
          {
            alt10 = 8;
          }
          break;
        case PLUS:
          {
            alt10 = 9;
          }
          break;
        case MINUS:
          {
            alt10 = 10;
          }
          break;
        case TIMES:
          {
            alt10 = 11;
          }
          break;
        case DIV:
          {
            alt10 = 12;
          }
          break;
        case NOT:
          {
            alt10 = 13;
          }
          break;
        case NOACTUAL:
          {
            alt10 = 14;
          }
          break;
        default:
          NoViableAltException nvae = new NoViableAltException("", 10, 0, input);

          throw nvae;
      }

      switch (alt10) {
        case 1:
          // FunChecker.g:269:4: FALSE
          {
            match(input, FALSE, FOLLOW_FALSE_in_expr479);

            type = Type.BOOL;
          }
          break;
        case 2:
          // FunChecker.g:271:4: TRUE
          {
            match(input, TRUE, FOLLOW_TRUE_in_expr490);

            type = Type.BOOL;
          }
          break;
        case 3:
          // FunChecker.g:273:4: NUM
          {
            match(input, NUM, FOLLOW_NUM_in_expr501);

            type = Type.INT;
          }
          break;
        case 4:
          // FunChecker.g:275:4: ID
          {
            ID19 = (CommonTree) match(input, ID, FOLLOW_ID_in_expr512);

            type = retrieve((ID19 != null ? ID19.getText() : null), ID19);
          }
          break;
        case 5:
          // FunChecker.g:278:4: ^( FUNCCALL ID t= expr )
          {
            FUNCCALL21 = (CommonTree) match(input, FUNCCALL, FOLLOW_FUNCCALL_in_expr524);

            match(input, Token.DOWN, null);
            ID20 = (CommonTree) match(input, ID, FOLLOW_ID_in_expr526);

            pushFollow(FOLLOW_expr_in_expr530);
            t = expr();

            state._fsp--;

            match(input, Token.UP, null);

            Type result = checkCall((ID20 != null ? ID20.getText() : null), t, FUNCCALL21);
            if (result.equiv(Type.VOID)) reportError("procedure should be non-void", FUNCCALL21);
            type = result;
          }
          break;
        case 6:
          // FunChecker.g:287:4: ^( EQ t1= expr t2= expr )
          {
            EQ22 = (CommonTree) match(input, EQ, FOLLOW_EQ_in_expr543);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr547);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr551);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(COMPTYPE, t1, t2, EQ22);
          }
          break;
        case 7:
          // FunChecker.g:290:4: ^( LT t1= expr t2= expr )
          {
            LT23 = (CommonTree) match(input, LT, FOLLOW_LT_in_expr564);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr568);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr572);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(COMPTYPE, t1, t2, LT23);
          }
          break;
        case 8:
          // FunChecker.g:293:4: ^( GT t1= expr t2= expr )
          {
            GT24 = (CommonTree) match(input, GT, FOLLOW_GT_in_expr585);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr589);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr593);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(COMPTYPE, t1, t2, GT24);
          }
          break;
        case 9:
          // FunChecker.g:296:4: ^( PLUS t1= expr t2= expr )
          {
            PLUS25 = (CommonTree) match(input, PLUS, FOLLOW_PLUS_in_expr606);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr610);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr614);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(ARITHTYPE, t1, t2, PLUS25);
          }
          break;
        case 10:
          // FunChecker.g:299:4: ^( MINUS t1= expr t2= expr )
          {
            MINUS26 = (CommonTree) match(input, MINUS, FOLLOW_MINUS_in_expr627);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr631);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr635);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(ARITHTYPE, t1, t2, MINUS26);
          }
          break;
        case 11:
          // FunChecker.g:302:4: ^( TIMES t1= expr t2= expr )
          {
            TIMES27 = (CommonTree) match(input, TIMES, FOLLOW_TIMES_in_expr648);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr652);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr656);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(ARITHTYPE, t1, t2, TIMES27);
          }
          break;
        case 12:
          // FunChecker.g:305:4: ^( DIV t1= expr t2= expr )
          {
            DIV28 = (CommonTree) match(input, DIV, FOLLOW_DIV_in_expr669);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr673);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_expr677);
            t2 = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkBinary(ARITHTYPE, t1, t2, DIV28);
          }
          break;
        case 13:
          // FunChecker.g:308:4: ^( NOT t= expr )
          {
            NOT29 = (CommonTree) match(input, NOT, FOLLOW_NOT_in_expr690);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_expr694);
            t = expr();

            state._fsp--;

            match(input, Token.UP, null);

            type = checkUnary(NOTTYPE, t, NOT29);
          }
          break;
        case 14:
          // FunChecker.g:310:4: NOACTUAL
          {
            match(input, NOACTUAL, FOLLOW_NOACTUAL_in_expr706);

            type = Type.VOID;
          }
          break;
      }
    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
      // do for sure before leaving
    }
    return type;
  }
예제 #3
0
  // $ANTLR start "com"
  // FunChecker.g:231:1: com : ( ^( ASSN ID t= expr ) | ^( PROCCALL ID t= expr ) | ^( IF t= expr com
  // ) | ^( IFELSE t= expr com com ) | ^( WHILE t= expr com ) | ^( FOR ID t1= expr t2= expr com ) |
  // ^( SEQ ( com )* ) );
  public final void com() throws RecognitionException {
    CommonTree ID10 = null;
    CommonTree ASSN11 = null;
    CommonTree ID12 = null;
    CommonTree PROCCALL13 = null;
    CommonTree IF14 = null;
    CommonTree IFELSE15 = null;
    CommonTree WHILE16 = null;
    CommonTree ID17 = null;
    CommonTree FOR18 = null;
    Type t = null;

    Type t1 = null;

    Type t2 = null;

    try {
      // FunChecker.g:232:2: ( ^( ASSN ID t= expr ) | ^( PROCCALL ID t= expr ) | ^( IF t= expr com )
      // | ^( IFELSE t= expr com com ) | ^( WHILE t= expr com ) | ^( FOR ID t1= expr t2= expr com )
      // | ^( SEQ ( com )* ) )
      int alt9 = 7;
      switch (input.LA(1)) {
        case ASSN:
          {
            alt9 = 1;
          }
          break;
        case PROCCALL:
          {
            alt9 = 2;
          }
          break;
        case IF:
          {
            alt9 = 3;
          }
          break;
        case IFELSE:
          {
            alt9 = 4;
          }
          break;
        case WHILE:
          {
            alt9 = 5;
          }
          break;
        case FOR:
          {
            alt9 = 6;
          }
          break;
        case SEQ:
          {
            alt9 = 7;
          }
          break;
        default:
          NoViableAltException nvae = new NoViableAltException("", 9, 0, input);

          throw nvae;
      }

      switch (alt9) {
        case 1:
          // FunChecker.g:232:4: ^( ASSN ID t= expr )
          {
            ASSN11 = (CommonTree) match(input, ASSN, FOLLOW_ASSN_in_com331);

            match(input, Token.DOWN, null);
            ID10 = (CommonTree) match(input, ID, FOLLOW_ID_in_com333);

            pushFollow(FOLLOW_expr_in_com337);
            t = expr();

            state._fsp--;

            match(input, Token.UP, null);

            Type tvar = retrieve((ID10 != null ? ID10.getText() : null), ASSN11);
            checkType(tvar, t, ASSN11);
          }
          break;
        case 2:
          // FunChecker.g:237:4: ^( PROCCALL ID t= expr )
          {
            PROCCALL13 = (CommonTree) match(input, PROCCALL, FOLLOW_PROCCALL_in_com350);

            match(input, Token.DOWN, null);
            ID12 = (CommonTree) match(input, ID, FOLLOW_ID_in_com352);

            pushFollow(FOLLOW_expr_in_com356);
            t = expr();

            state._fsp--;

            match(input, Token.UP, null);

            Type tres = checkCall((ID12 != null ? ID12.getText() : null), t, PROCCALL13);
            if (!tres.equiv(Type.VOID)) reportError("procedure should be void", PROCCALL13);
          }
          break;
        case 3:
          // FunChecker.g:245:4: ^( IF t= expr com )
          {
            IF14 = (CommonTree) match(input, IF, FOLLOW_IF_in_com369);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_com373);
            t = expr();

            state._fsp--;

            pushFollow(FOLLOW_com_in_com375);
            com();

            state._fsp--;

            match(input, Token.UP, null);

            checkType(Type.BOOL, t, IF14);
          }
          break;
        case 4:
          // FunChecker.g:248:4: ^( IFELSE t= expr com com )
          {
            IFELSE15 = (CommonTree) match(input, IFELSE, FOLLOW_IFELSE_in_com388);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_com392);
            t = expr();

            state._fsp--;

            pushFollow(FOLLOW_com_in_com394);
            com();

            state._fsp--;

            pushFollow(FOLLOW_com_in_com396);
            com();

            state._fsp--;

            match(input, Token.UP, null);

            checkType(Type.BOOL, t, IFELSE15);
          }
          break;
        case 5:
          // FunChecker.g:251:4: ^( WHILE t= expr com )
          {
            WHILE16 = (CommonTree) match(input, WHILE, FOLLOW_WHILE_in_com409);

            match(input, Token.DOWN, null);
            pushFollow(FOLLOW_expr_in_com413);
            t = expr();

            state._fsp--;

            pushFollow(FOLLOW_com_in_com415);
            com();

            state._fsp--;

            match(input, Token.UP, null);

            checkType(Type.BOOL, t, WHILE16);
          }
          break;
        case 6:
          // FunChecker.g:254:5: ^( FOR ID t1= expr t2= expr com )
          {
            FOR18 = (CommonTree) match(input, FOR, FOLLOW_FOR_in_com429);

            match(input, Token.DOWN, null);
            ID17 = (CommonTree) match(input, ID, FOLLOW_ID_in_com431);

            pushFollow(FOLLOW_expr_in_com435);
            t1 = expr();

            state._fsp--;

            pushFollow(FOLLOW_expr_in_com439);
            t2 = expr();

            state._fsp--;

            pushFollow(FOLLOW_com_in_com441);
            com();

            state._fsp--;

            match(input, Token.UP, null);

            Type ident = retrieve((ID17 != null ? ID17.getText() : null), FOR18);
            checkType(Type.INT, ident, FOR18);
            checkType(Type.INT, t1, FOR18);
            checkType(Type.INT, t2, FOR18);
          }
          break;
        case 7:
          // FunChecker.g:262:4: ^( SEQ ( com )* )
          {
            match(input, SEQ, FOLLOW_SEQ_in_com454);

            if (input.LA(1) == Token.DOWN) {
              match(input, Token.DOWN, null);
              // FunChecker.g:262:10: ( com )*
              loop8:
              do {
                int alt8 = 2;
                int LA8_0 = input.LA(1);

                if ((LA8_0 == ASSN
                    || LA8_0 == FOR
                    || (LA8_0 >= IF && LA8_0 <= IFELSE)
                    || LA8_0 == PROCCALL
                    || LA8_0 == SEQ
                    || LA8_0 == WHILE)) {
                  alt8 = 1;
                }

                switch (alt8) {
                  case 1:
                    // FunChecker.g:262:10: com
                    {
                      pushFollow(FOLLOW_com_in_com456);
                      com();

                      state._fsp--;
                    }
                    break;

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

              match(input, Token.UP, null);
            }
          }
          break;
      }
    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
      // do for sure before leaving
    }
    return;
  }
예제 #4
0
 private void checkType(Type typeExpected, Type typeActual, CommonTree construct) {
   // Check that a construct's actual type matches
   // the expected type.
   if (!typeActual.equiv(typeExpected))
     reportError("type is " + typeActual + ", should be " + typeExpected, construct);
 }
예제 #5
0
파일: DefRef.java 프로젝트: chuzui/antlr
  // $ANTLR start "member"
  // DefRef.g:118:1: member returns [Type type] : ( ^( '.' m= member ID ) | ID );
  public final Type member() throws RecognitionException {
    Type type = null;

    CommonTree ID7 = null;
    CommonTree ID8 = null;
    Type m = null;

    try {
      // DefRef.g:119:5: ( ^( '.' m= member ID ) | ID )
      int alt6 = 2;
      int LA6_0 = input.LA(1);

      if ((LA6_0 == 30)) {
        alt6 = 1;
      } else if ((LA6_0 == ID)) {
        alt6 = 2;
      } else {
        if (state.backtracking > 0) {
          state.failed = true;
          return type;
        }
        NoViableAltException nvae = new NoViableAltException("", 6, 0, input);

        throw nvae;
      }
      switch (alt6) {
        case 1:
          // DefRef.g:119:7: ^( '.' m= member ID )
          {
            match(input, 30, FOLLOW_30_in_member527);
            if (state.failed) return type;

            match(input, Token.DOWN, null);
            if (state.failed) return type;
            pushFollow(FOLLOW_member_in_member531);
            m = member();

            state._fsp--;
            if (state.failed) return type;
            ID7 = (CommonTree) match(input, ID, FOLLOW_ID_in_member533);
            if (state.failed) return type;

            match(input, Token.UP, null);
            if (state.failed) return type;
            if (state.backtracking == 1) {

              StructSymbol scope = (StructSymbol) m; // get scope of expr
              Symbol s =
                  scope.resolveMember((ID7 != null ? ID7.getText() : null)); // resolve ID in scope
              System.out.println(
                  "line "
                      + ID7.getLine()
                      + ": ref "
                      + m.getName()
                      + "."
                      + (ID7 != null ? ID7.getText() : null)
                      + "="
                      + s);
              if (s != null) type = s.type; // return ID's type
            }
          }
          break;
        case 2:
          // DefRef.g:127:7: ID
          {
            ID8 = (CommonTree) match(input, ID, FOLLOW_ID_in_member550);
            if (state.failed) return type;
            if (state.backtracking == 1) {

              Symbol s = currentScope.resolve((ID8 != null ? ID8.getText() : null));
              System.out.println(
                  "line "
                      + ID8.getLine()
                      + ": ref "
                      + (ID8 != null ? ID8.getText() : null)
                      + "="
                      + s);
              if (s != null) type = s.type;
            }
          }
          break;
      }
    } catch (RecognitionException re) {
      reportError(re);
      recover(input, re);
    } finally {
    }
    return type;
  }