示例#1
0
  // $ANTLR start symbol
  // /Users/stroetma/Kurse/FormaleSprachen/ANTLR/Lisp/Interpreter/Lisp.g:19:1: symbol returns
  // [SymbolicExpression symExp] : ( OP | ID | NUMBER );
  public final SymbolicExpression symbol() throws RecognitionException {
    SymbolicExpression symExp = null;

    Token OP3 = null;
    Token ID4 = null;
    Token NUMBER5 = null;

    try {
      dbg.enterRule("symbol");
      if (ruleLevel == 0) {
        dbg.commence();
      }
      ruleLevel++;
      dbg.location(19, 1);

      try {
        // /Users/stroetma/Kurse/FormaleSprachen/ANTLR/Lisp/Interpreter/Lisp.g:20:5: ( OP | ID |
        // NUMBER )
        int alt4 = 3;
        try {
          dbg.enterDecision(4);

          switch (input.LA(1)) {
            case OP:
              {
                alt4 = 1;
              }
              break;
            case ID:
              {
                alt4 = 2;
              }
              break;
            case NUMBER:
              {
                alt4 = 3;
              }
              break;
            default:
              NoViableAltException nvae =
                  new NoViableAltException(
                      "19:1: symbol returns [SymbolicExpression symExp] : ( OP | ID | NUMBER );",
                      4,
                      0,
                      input);

              dbg.recognitionException(nvae);
              throw nvae;
          }

        } finally {
          dbg.exitDecision(4);
        }

        switch (alt4) {
          case 1:
            dbg.enterAlt(1);

            // /Users/stroetma/Kurse/FormaleSprachen/ANTLR/Lisp/Interpreter/Lisp.g:20:7: OP
            {
              dbg.location(20, 7);
              OP3 = (Token) input.LT(1);
              match(input, OP, FOLLOW_OP_in_symbol126);
              dbg.location(20, 14);
              symExp = new Atom(OP3.getText());
            }
            break;
          case 2:
            dbg.enterAlt(2);

            // /Users/stroetma/Kurse/FormaleSprachen/ANTLR/Lisp/Interpreter/Lisp.g:21:7: ID
            {
              dbg.location(21, 7);
              ID4 = (Token) input.LT(1);
              match(input, ID, FOLLOW_ID_in_symbol140);
              dbg.location(21, 14);
              symExp = new Atom(ID4.getText());
            }
            break;
          case 3:
            dbg.enterAlt(3);

            // /Users/stroetma/Kurse/FormaleSprachen/ANTLR/Lisp/Interpreter/Lisp.g:22:7: NUMBER
            {
              dbg.location(22, 7);
              NUMBER5 = (Token) input.LT(1);
              match(input, NUMBER, FOLLOW_NUMBER_in_symbol154);
              dbg.location(22, 14);
              symExp = new Number(NUMBER5.getText());
            }
            break;
        }
      } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
      } finally {
      }
      dbg.location(23, 5);

    } finally {
      dbg.exitRule("symbol");
      ruleLevel--;
      if (ruleLevel == 0) {
        dbg.terminate();
      }
    }

    return symExp;
  }
  // $ANTLR start "declaration"
  // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:50:1:
  // declaration[HashMap<String,Par> declarations_in] returns [HashMap<String,Par> declarations_out]
  // : (tipo= Type_Int | tipo= Type_Real ) ID ;
  public final HashMap<String, Par> declaration(HashMap<String, Par> declarations_in)
      throws RecognitionException {
    HashMap<String, Par> declarations_out = null;

    Token tipo = null;
    Token ID2 = null;

    try {
      dbg.enterRule(getGrammarFileName(), "declaration");
      if (getRuleLevel() == 0) {
        dbg.commence();
      }
      incRuleLevel();
      dbg.location(50, 1);

      try {
        // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:51:2: (
        // (tipo= Type_Int | tipo= Type_Real ) ID )
        dbg.enterAlt(1);

        // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:51:4:
        // (tipo= Type_Int | tipo= Type_Real ) ID
        {
          dbg.location(51, 4);
          // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:51:4:
          // (tipo= Type_Int | tipo= Type_Real )
          int alt3 = 2;
          try {
            dbg.enterSubRule(3);
            try {
              dbg.enterDecision(3, decisionCanBacktrack[3]);

              int LA3_0 = input.LA(1);

              if ((LA3_0 == Type_Int)) {
                alt3 = 1;
              } else if ((LA3_0 == Type_Real)) {
                alt3 = 2;
              } else {
                NoViableAltException nvae = new NoViableAltException("", 3, 0, input);

                dbg.recognitionException(nvae);
                throw nvae;
              }
            } finally {
              dbg.exitDecision(3);
            }

            switch (alt3) {
              case 1:
                dbg.enterAlt(1);

                // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:51:5: tipo= Type_Int
                {
                  dbg.location(51, 9);
                  tipo = (Token) match(input, Type_Int, FOLLOW_Type_Int_in_declaration94);
                }
                break;
              case 2:
                dbg.enterAlt(2);

                // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:51:21: tipo= Type_Real
                {
                  dbg.location(51, 25);
                  tipo = (Token) match(input, Type_Real, FOLLOW_Type_Real_in_declaration100);
                }
                break;
            }
          } finally {
            dbg.exitSubRule(3);
          }

          dbg.location(51, 39);
          ID2 = (Token) match(input, ID, FOLLOW_ID_in_declaration105);
          dbg.location(51, 42);

          if (declarations_in.containsKey((ID2 != null ? ID2.getText() : null))) {
            System.out.println(
                "A variavel "
                    + (ID2 != null ? ID2.getText() : null)
                    + " ja foi declarada antes.\n");
            declarations_out = declarations_in;
          } else {
            Par p = new Par((tipo != null ? tipo.getText() : null), 0);
            declarations_in.put((ID2 != null ? ID2.getText() : null), p);
            declarations_out = declarations_in;
          }
        }

      } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
      } finally {
      }
      dbg.location(62, 2);

    } finally {
      dbg.exitRule(getGrammarFileName(), "declaration");
      decRuleLevel();
      if (getRuleLevel() == 0) {
        dbg.terminate();
      }
    }

    return declarations_out;
  }
  // $ANTLR start "attrib"
  // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:64:1:
  // attrib[HashMap<String,Par> declarations_in] : ID '=' expression[declarations_in] ;
  public final void attrib(HashMap<String, Par> declarations_in) throws RecognitionException {
    Token ID3 = null;
    calcParser.expression_return expression4 = null;

    try {
      dbg.enterRule(getGrammarFileName(), "attrib");
      if (getRuleLevel() == 0) {
        dbg.commence();
      }
      incRuleLevel();
      dbg.location(64, 1);

      try {
        // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:65:2: ( ID
        // '=' expression[declarations_in] )
        dbg.enterAlt(1);

        // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:65:4: ID
        // '=' expression[declarations_in]
        {
          dbg.location(65, 4);
          ID3 = (Token) match(input, ID, FOLLOW_ID_in_attrib120);
          dbg.location(65, 7);
          match(input, 13, FOLLOW_13_in_attrib122);
          dbg.location(65, 11);
          pushFollow(FOLLOW_expression_in_attrib124);
          expression4 = expression(declarations_in);

          state._fsp--;

          dbg.location(66, 4);

          if (!declarations_in.containsKey((ID3 != null ? ID3.getText() : null))) {
            System.out.println(
                "A variavel "
                    + (ID3 != null ? ID3.getText() : null)
                    + " ainda nao foi declarada!\n");
          } else {
            if (declarations_in.get((ID3 != null ? ID3.getText() : null)).getTipo().equals("int")
                && (expression4 != null ? expression4.tipo : null) == Tipo.REAL) {
              System.out.println("Possivel perda de precis‹o! (guardar real em variavel inteira)");
              Par p = declarations_in.get((ID3 != null ? ID3.getText() : null));
              p.setValue(Math.round((expression4 != null ? expression4.valor : 0.0f)));
            } else {
              Par p = declarations_in.get((ID3 != null ? ID3.getText() : null));
              p.setValue((expression4 != null ? expression4.valor : 0.0f));
            }
            System.out.println(
                (ID3 != null ? ID3.getText() : null)
                    + " == "
                    + declarations_in.get((ID3 != null ? ID3.getText() : null)).getValue());
          }
        }

      } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
      } finally {
      }
      dbg.location(84, 2);

    } finally {
      dbg.exitRule(getGrammarFileName(), "attrib");
      decRuleLevel();
      if (getRuleLevel() == 0) {
        dbg.terminate();
      }
    }

    return;
  }
  // $ANTLR start "value"
  // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:148:1:
  // value[HashMap<String,Par> declarations_in] returns [Tipo tipo, float valor] : ( ID | INT |
  // FLOAT );
  public final calcParser.value_return value(HashMap<String, Par> declarations_in)
      throws RecognitionException {
    calcParser.value_return retval = new calcParser.value_return();
    retval.start = input.LT(1);

    Token ID8 = null;
    Token INT9 = null;
    Token FLOAT10 = null;

    try {
      dbg.enterRule(getGrammarFileName(), "value");
      if (getRuleLevel() == 0) {
        dbg.commence();
      }
      incRuleLevel();
      dbg.location(148, 1);

      try {
        // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:151:3: (
        // ID | INT | FLOAT )
        int alt9 = 3;
        try {
          dbg.enterDecision(9, decisionCanBacktrack[9]);

          switch (input.LA(1)) {
            case ID:
              {
                alt9 = 1;
              }
              break;
            case INT:
              {
                alt9 = 2;
              }
              break;
            case FLOAT:
              {
                alt9 = 3;
              }
              break;
            default:
              NoViableAltException nvae = new NoViableAltException("", 9, 0, input);

              dbg.recognitionException(nvae);
              throw nvae;
          }

        } finally {
          dbg.exitDecision(9);
        }

        switch (alt9) {
          case 1:
            dbg.enterAlt(1);

            // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:151:5:
            // ID
            {
              dbg.location(151, 5);
              ID8 = (Token) match(input, ID, FOLLOW_ID_in_value398);
              dbg.location(151, 8);
              String type = declarations_in.get((ID8 != null ? ID8.getText() : null)).getTipo();
              if (type.equals("int")) retval.tipo = Tipo.INT;
              else retval.tipo = Tipo.REAL;
              retval.valor = declarations_in.get((ID8 != null ? ID8.getText() : null)).getValue();
            }
            break;
          case 2:
            dbg.enterAlt(2);

            // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:155:6:
            // INT
            {
              dbg.location(155, 6);
              INT9 = (Token) match(input, INT, FOLLOW_INT_in_value408);
              dbg.location(155, 10);
              retval.tipo = Tipo.INT;
              retval.valor = Integer.parseInt((INT9 != null ? INT9.getText() : null));
            }
            break;
          case 3:
            dbg.enterAlt(3);

            // /Users/Pedro/Dropbox/MEI/EL/PI/Static-Code-Analyzer/AulasEG/calculadora/calc.g:156:6:
            // FLOAT
            {
              dbg.location(156, 6);
              FLOAT10 = (Token) match(input, FLOAT, FOLLOW_FLOAT_in_value417);
              dbg.location(156, 12);
              retval.tipo = Tipo.REAL;
              retval.valor = Float.parseFloat((FLOAT10 != null ? FLOAT10.getText() : null));
            }
            break;
        }
        retval.stop = input.LT(-1);

      } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
      } finally {
      }
      dbg.location(157, 3);

    } finally {
      dbg.exitRule(getGrammarFileName(), "value");
      decRuleLevel();
      if (getRuleLevel() == 0) {
        dbg.terminate();
      }
    }

    return retval;
  }