Esempio n. 1
0
  /**
   * The main driver for the system.
   *
   * @param argv an array of strings containing command line arguments.
   */
  public static void main(String argv[])
      throws internal_error, java.io.IOException, java.lang.Exception {
    boolean did_output = false;

    start_time = System.currentTimeMillis();

    /* process user options and arguments */
    parse_args(argv);

    /* open output files */
    if (print_progress) System.err.println("Opening files...");
    open_files();

    prelim_end = System.currentTimeMillis();

    /* parse spec into internal data structures */
    if (print_progress) System.err.println("Parsing specification from standard input...");
    parse_grammar_spec();

    parse_end = System.currentTimeMillis();

    /* don't proceed unless we are error free */
    if (lexer.error_count == 0) {
      /* check for unused bits */
      if (print_progress) System.err.println("Checking specification...");
      check_unused();

      check_end = System.currentTimeMillis();

      /* build the state machine and parse tables */
      if (print_progress) System.err.println("Building parse tables...");
      build_parser();

      build_end = System.currentTimeMillis();

      /* output the generated code */
      if (print_progress) System.err.println("Writing parser...");
      emit_parser();
      did_output = true;

      emit_end = System.currentTimeMillis();
    } else {
      /* fix up the times to make the summary easier */
      emit_end = parse_end;
    }

    /* do requested dumps */
    if (opt_dump_grammar) dump_grammar();
    if (opt_dump_states) dump_machine();
    if (opt_dump_tables) dump_tables();

    dump_end = System.currentTimeMillis();

    /* close output files */
    if (print_progress) System.err.println("Closing files...");
    close_files();

    /* produce a summary if desired */
    if (!no_summary) emit_summary(did_output);
  }
Esempio n. 2
0
  /**
   * Build the (internal) parser from the previously parsed specification. This includes:
   *
   * <ul>
   *   <li>Computing nullability of non-terminals.
   *   <li>Computing first sets of non-terminals and productions.
   *   <li>Building the viable prefix recognizer machine.
   *   <li>Filling in the (internal) parse tables.
   *   <li>Checking for unreduced productions.
   * </ul>
   */
  protected static void build_parser() throws internal_error {
    /* compute nullability of all non terminals */
    if (opt_do_debug || print_progress)
      System.err.println("  Computing non-terminal nullability...");
    non_terminal.compute_nullability();

    nullability_end = System.currentTimeMillis();

    /* compute first sets of all non terminals */
    if (opt_do_debug || print_progress) System.err.println("  Computing first sets...");
    non_terminal.compute_first_sets();

    first_end = System.currentTimeMillis();

    /* build the LR viable prefix recognition machine */
    if (opt_do_debug || print_progress) System.err.println("  Building state machine...");
    start_state = lalr_state.build_machine(emit.start_production);

    machine_end = System.currentTimeMillis();

    /* build the LR parser action and reduce-goto tables */
    if (opt_do_debug || print_progress) System.err.println("  Filling in tables...");
    action_table = new parse_action_table();
    reduce_table = new parse_reduce_table();
    for (Enumeration st = lalr_state.all(); st.hasMoreElements(); ) {
      ((lalr_state) st.nextElement()).build_table_entries(action_table, reduce_table);
    }

    table_end = System.currentTimeMillis();

    /* check and warn for non-reduced productions */
    if (opt_do_debug || print_progress)
      System.err.println("  Checking for non-reduced productions...");
    action_table.check_reductions();

    reduce_check_end = System.currentTimeMillis();

    /* if we have more conflicts than we expected issue a message and die */
    if (emit.num_conflicts > expect_conflicts) {
      System.err.println(
          "*** More conflicts encountered than expected " + "-- parser generation aborted");
      lexer.error_count++;
      build_end = System.currentTimeMillis();

      /* do dumps and summary as needed */
      if (opt_dump_grammar) dump_grammar();
      if (opt_dump_states) dump_machine();
      if (!no_summary) emit_summary(false);

      System.exit(100);
    }
  }