Example #1
0
  public static void main(String args[]) throws Exception {
    Yylex l = null;
    parser p;
    Interpreter I;
    try {
      if (args.length == 0) l = new Yylex(System.in);
      else l = new Yylex(new FileReader(args[0]));
    } catch (FileNotFoundException e) {
      System.err.println("Error: File not found: " + args[0]);
      System.exit(1);
    }
    p = new parser(l);
    /* The default parser is the first-defined entry point. */
    /* You may want to change this. Other options are: */
    /*  */
    try {

      calc.Absyn.Exp parse_tree = p.pExp();
      I = new Interpreter();
      // I.eval(parse_tree) ;
      System.out.println(I.eval(parse_tree));
      /*System.out.println();
      System.out.println("Parse Succesful!");
      System.out.println();
      System.out.println("[Abstract Syntax]");
      System.out.println();
      System.out.println(PrettyPrinter.show(parse_tree));
      System.out.println();
      System.out.println("[Linearized Tree]");
      System.out.println();
      System.out.println(PrettyPrinter.print(parse_tree));
      */
    } catch (Throwable e) {
      System.err.println(
          "At line " + String.valueOf(l.line_num()) + ", near \"" + l.buff() + "\" :");
      System.err.println("     " + e.getMessage());
      System.exit(1);
    }
  }
  /** Method with the actual generated action code. */
  public final java_cup.runtime.Symbol CUP$parser$do_action(
      int CUP$parser$act_num,
      java_cup.runtime.lr_parser CUP$parser$parser,
      java.util.Stack CUP$parser$stack,
      int CUP$parser$top)
      throws java.lang.Exception {
    /* Symbol object for return from actions */
    java_cup.runtime.Symbol CUP$parser$result;

    /* select the action based on the action number */
    switch (CUP$parser$act_num) {
        /*. . . . . . . . . . . . . . . . . . . .*/
      case 14: // expresion ::= LPARENT expresion RPARENT
        {
          Integer RESULT = null;
          int EXWleft =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).left;
          int EXWright =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).right;
          Integer EXW =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).value;
          RESULT = new Integer(EXW.intValue());
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 13: // expresion ::= RESTA expresion
        {
          Integer RESULT = null;
          int EPX1left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int EPX1right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer EPX1 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer(EPX1 * (-1));
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 12: // expresion ::= expresion POTENCIA expresion
        {
          Integer RESULT = null;
          int ED1left =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).left;
          int ED1right =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).right;
          Integer ED1 =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).value;
          int ED2left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int ED2right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer ED2 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer((int) Math.pow(ED1.intValue(), ED2.intValue()));
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 11: // expresion ::= expresion DIVISION expresion
        {
          Integer RESULT = null;
          int EC1left =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).left;
          int EC1right =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).right;
          Integer EC1 =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).value;
          int EC2left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int EC2right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer EC2 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer(EC1.intValue() / EC2.intValue());
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 10: // expresion ::= expresion MULTIPLICACION expresion
        {
          Integer RESULT = null;
          int EA1left =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).left;
          int EA1right =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).right;
          Integer EA1 =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).value;
          int EA2left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int EA2right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer EA2 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer(EA1.intValue() * EA2.intValue());
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 9: // expresion ::= expresion RESTA expresion
        {
          Integer RESULT = null;
          int EE1left =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).left;
          int EE1right =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).right;
          Integer EE1 =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).value;
          int EE2left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int EE2right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer EE2 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer(EE1.intValue() - EE2.intValue());
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 8: // expresion ::= expresion SUMA expresion
        {
          Integer RESULT = null;
          int E1left =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).left;
          int E1right =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).right;
          Integer E1 =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).value;
          int E2left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int E2right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer E2 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer(E1.intValue() + E2.intValue());
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 7: // expresion ::= ENTERO
        {
          Integer RESULT = null;
          int D1left = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int D1right = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer D1 = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          RESULT = new Integer(D1.intValue());
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "expresion",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 6: // ecuacion ::= error NT$1 RESULTADO
        {
          Object RESULT = null;
          // propagate RESULT from NT$1
          RESULT =
              (Object)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).value;

          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "ecuacion",
                      2,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 5: // NT$1 ::=
        {
          Object RESULT = null;
          System.out.println("Aprenda a escribir br..");
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "NT$1",
                      4,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 4: // ecuacion ::= expresion NT$0 RESULTADO
        {
          Object RESULT = null;
          // propagate RESULT from NT$0
          RESULT =
              (Object)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).value;
          int EQleft =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).left;
          int EQright =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).right;
          Integer EQ =
              (Integer)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)).value;

          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "ecuacion",
                      2,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 2)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 3: // NT$0 ::=
        {
          Object RESULT = null;
          int EQleft = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).left;
          int EQright = ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).right;
          Integer EQ = (Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.peek()).value;
          System.out.println("Sintaxis OK");
          System.out.println("Resultado = " + EQ.intValue());

          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "NT$0",
                      3,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 2: // sesion ::= ecuacion
        {
          Object RESULT = null;

          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "sesion",
                      1,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 1: // $START ::= sesion EOF
        {
          Object RESULT = null;
          int start_valleft =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).left;
          int start_valright =
              ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).right;
          Object start_val =
              (Object)
                  ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)).value;
          RESULT = start_val;
          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "$START",
                      0,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        /* ACCEPT */
        CUP$parser$parser.done_parsing();
        return CUP$parser$result;

        /*. . . . . . . . . . . . . . . . . . . .*/
      case 0: // sesion ::= sesion ecuacion
        {
          Object RESULT = null;

          CUP$parser$result =
              parser
                  .getSymbolFactory()
                  .newSymbol(
                      "sesion",
                      1,
                      ((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top - 1)),
                      ((java_cup.runtime.Symbol) CUP$parser$stack.peek()),
                      RESULT);
        }
        return CUP$parser$result;

        /* . . . . . .*/
      default:
        throw new Exception("Invalid action number found in internal parse table");
    }
  }
Example #3
0
  public static void main(String[] args) throws IOException // may be thrown by the scanner
      {
    // check for command-line args
    if (args.length != 2) {
      System.err.println(
          "please supply name of file to be parsed and name of file for unparsed version.");
      System.exit(-1);
    }

    try {
      Codegen.p = new PrintWriter("/home/ben/Scripts/github/Academic/cs 536/p6/gencode.asm");
    } catch (FileNotFoundException e) {
      System.err.println("File " + args[0] + " not found.");
      System.exit(-1);
    }

    // open input file
    FileReader inFile = null;
    try {
      inFile = new FileReader(args[0]);
    } catch (FileNotFoundException ex) {
      System.err.println("File " + args[0] + " not found.");
      System.exit(-1);
    }

    // open output file
    PrintWriter outFile = null;
    try {
      outFile = new PrintWriter(args[1]);
    } catch (FileNotFoundException ex) {
      System.err.println("File " + args[1] + " could not be opened for writing.");
      System.exit(-1);
    }

    parser P = new parser(new Yylex(inFile));

    Symbol root = null; // the parser will return a Symbol whose value
    // field is the translation of the root
    // nonterminal (i.e., of the nonterminal
    // "program")

    try {
      root = P.parse(); // do the parse
      System.out.println("Little program parsed correctly.");
    } catch (Exception ex) {
      System.err.println("Exception occured during parse: " + ex);
      ex.printStackTrace();
      System.exit(-1);
    }
    try {
      //		((SymTabNode)root.value).provideSymTab(new SymTab());
      ((ProgramNode) root.value).processNames();
    } catch (Exception ex) {
      System.err.println("Exception occured during parse: " + ex);
      ex.printStackTrace();
      System.exit(-1);
    }
    if (!Errors.errors) {
      ((ASTnode) root.value).unparse(outFile, 0);
      try {
        ((ProgramNode) root.value).typeCheck();
      } catch (Exception ex) {
        System.err.println("Exception occured during typecheck: " + ex);
        ex.printStackTrace();
        System.exit(-1);
      }
    }

    if (!Errors.errors) {
      try {
        ((ProgramNode) root.value).generate();
        Codegen.p.close();
      } catch (Exception ex) {
        System.err.println("Exception occured during codegen: " + ex);
        ex.printStackTrace();
        System.exit(-1);
      }
    }
    outFile.close();

    return;
  }
 public static void main(String args[]) throws Exception {
   parser analizador;
   analizador = new parser(new Scanner(System.in));
   analizador.parse();
 }