📄 main.java
字号:
{ /* something threw an exception. catch it and emit a message so we have a line number to work with, then re-throw it */ lexer.emit_error("Internal error: Unexpected exception"); throw e; } } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Check for unused symbols. Unreduced productions get checked when * tables are created. */ protected static void check_unused() { terminal term; non_terminal nt; /* check for unused terminals */ for (Enumeration t = terminal.all(); t.hasMoreElements(); ) { term = (terminal)t.nextElement(); /* don't issue a message for EOF */ if (term == terminal.EOF) continue; /* or error */ if (term == terminal.error) continue; /* is this one unused */ if (term.use_count() == 0) { /* count it and warn if we are doing warnings */ emit.unused_term++; if (!emit.nowarn) { System.err.println("Warning: Terminal \"" + term.name() + "\" was declared but never used"); lexer.warning_count++; } } } /* check for unused non terminals */ for (Enumeration n = non_terminal.all(); n.hasMoreElements(); ) { nt = (non_terminal)n.nextElement(); /* is this one unused */ if (nt.use_count() == 0) { /* count and warn if we are doing warnings */ emit.unused_term++; if (!emit.nowarn) { System.err.println("Warning: Non terminal \"" + nt.name() + "\" was declared but never used"); lexer.warning_count++; } } } } /* . . . . . . . . . . . . . . . . . . . . . . . . .*/ /* . . Internal Results of Generating the Parser . .*/ /* . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Start state in the overall state machine. */ protected static lalr_state start_state; /** Resulting parse action table. */ protected static parse_action_table action_table; /** Resulting reduce-goto table. */ protected static parse_reduce_table reduce_table; /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** 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 lst = (lalr_state)st.nextElement(); lst.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++; // indicate the problem. // we'll die on return, after clean up. } } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Call the emit routines necessary to write out the generated parser. */ protected static void emit_parser() throws internal_error { emit.symbols(symbol_class_file, include_non_terms, sym_interface); emit.parser(parser_class_file, action_table, reduce_table, start_state.index(), emit.start_production, opt_compact_red); } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Helper routine to optionally return a plural or non-plural ending. * @param val the numerical value determining plurality. */ protected static String plural(int val) { if (val == 1) return ""; else return "s"; } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Emit a long summary message to standard error (System.err) which * summarizes what was found in the specification, how many states were * produced, how many conflicts were found, etc. A detailed timing * summary is also produced if it was requested by the user. * @param output_produced did the system get far enough to generate code. */ protected static void emit_summary(boolean output_produced) { final_time = System.currentTimeMillis(); if (no_summary) return; System.err.println("------- " + version.title_str + " Parser Generation Summary -------"); /* error and warning count */ System.err.println(" " + lexer.error_count + " error" + plural(lexer.error_count) + " and " + lexer.warning_count + " warning" + plural(lexer.warning_count)); /* basic stats */ System.err.print(" " + terminal.number() + " terminal" + plural(terminal.number()) + ", "); System.err.print(non_terminal.number() + " non-terminal" + plural(non_terminal.number()) + ", and "); System.err.println(production.number() + " production" + plural(production.number()) + " declared, "); System.err.println(" producing " + lalr_state.number() + " unique parse states."); /* unused symbols */ System.err.println(" " + emit.unused_term + " terminal" + plural(emit.unused_term) + " declared but not used."); System.err.println(" " + emit.unused_non_term + " non-terminal" + plural(emit.unused_term) + " declared but not used."); /* productions that didn't reduce */ System.err.println(" " + emit.not_reduced + " production" + plural(emit.not_reduced) + " never reduced."); /* conflicts */ System.err.println(" " + emit.num_conflicts + " conflict" + plural(emit.num_conflicts) + " detected" + " (" + expect_conflicts + " expected)."); /* code location */ if (output_produced) System.err.println(" Code written to \"" + emit.parser_class_name + ".java\", and \"" + emit.symbol_const_class_name + ".java\"."); else System.err.println(" No code produced."); if (opt_show_timing) show_times(); System.err.println( "---------------------------------------------------- (" + version.version_str + ")"); } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Produce the optional timing summary as part of an overall summary. */ protected static void show_times() { long total_time = final_time - start_time; System.err.println(". . . . . . . . . . . . . . . . . . . . . . . . . "); System.err.println(" Timing Summary"); System.err.println(" Total time " + timestr(final_time-start_time, total_time)); System.err.println(" Startup " + timestr(prelim_end-start_time, total_time)); System.err.println(" Parse " + timestr(parse_end-prelim_end, total_time) ); if (check_end != 0) System.err.println(" Checking " + timestr(check_end-parse_end, total_time)); if (check_end != 0 && build_end != 0) System.err.println(" Parser Build " + timestr(build_end-check_end, total_time)); if (nullability_end != 0 && check_end != 0) System.err.println(" Nullability " + timestr(nullability_end-check_end, total_time)); if (first_end != 0 && nullability_end != 0) System.err.println(" First sets " + timestr(first_end-nullability_end, total_time)); if (machine_end != 0 && first_end != 0) System.err.println(" State build " + timestr(machine_end-first_end, total_time)); if (table_end != 0 && machine_end != 0) System.err.println(" Table build " + timestr(table_end-machine_end, total_time)); if (reduce_check_end != 0 && table_end != 0) System.err.println(" Checking " + timestr(reduce_check_end-table_end, total_time)); if (emit_end != 0 && build_end != 0) System.err.println(" Code Output " + timestr(emit_end-build_end, total_time)); if (emit.symbols_time != 0) System.err.println(" Symbols " + timestr(emit.symbols_time, total_time)); if (emit.parser_time != 0) System.err.println(" Parser class " + timestr(emit.parser_time, total_time)); if (emit.action_code_time != 0) System.err.println(" Actions " + timestr(emit.action_code_time, total_time)); if (emit.production_table_time != 0) System.err.println(" Prod table " + timestr(emit.production_table_time, total_time)); if (emit.action_table_time != 0) System.err.println(" Action tab " + timestr(emit.action_table_time, total_time)); if (emit.goto_table_time != 0) System.err.println(" Reduce tab " + timestr(emit.goto_table_time, total_time)); System.err.println(" Dump Output " + timestr(dump_end-emit_end, total_time)); } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Helper routine to format a decimal based display of seconds and * percentage of total time given counts of milliseconds. Note: this * is broken for use with some instances of negative time (since we don't * use any negative time here, we let if be for now). * @param time_val the value being formatted (in ms). * @param total_time total time percentages are calculated against (in ms). */ protected static String timestr(long time_val, long total_time) { boolean neg; long ms = 0; long sec = 0; long percent10; String pad; /* work with positives only */ neg = time_val < 0; if (neg) time_val = -time_val; /* pull out seconds and ms */ ms = time_val % 1000; sec = time_val / 1000; /* construct a pad to blank fill seconds out to 4 places */ if (sec < 10) pad = " "; else if (sec < 100) pad = " "; else if (sec < 1000) pad = " "; else pad = ""; /* calculate 10 times the percentage of total */ percent10 = (time_val*1000)/total_time; /* build and return the output string */ return (neg ? "-" : "") + pad + sec + "." + ((ms%1000)/100) + ((ms%100)/10) + (ms%10) + "sec" + " (" + percent10/10 + "." + percent10%10 + "%)"; } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Produce a human readable dump of the grammar. */ public static void dump_grammar() throws internal_error { int cnt; Enumeration t, n, p; production prod; System.err.println("===== Terminals ====="); for (t = terminal.all(), cnt=0; t.hasMoreElements(); cnt++) { System.err.print(((terminal)t.nextElement()).name() + " "); if ((cnt+1) % 5 == 0) System.err.println(); } System.err.println(); System.err.println(); System.err.println("===== Non terminals ====="); for (n=non_terminal.all(), cnt=0; n.hasMoreElements(); cnt++) { System.err.print(((non_terminal)n.nextElement()).name() + " "); if ((cnt+1) % 5 == 0) System.err.println(); } System.err.println(); System.err.println(); System.err.println("===== Productions ====="); for (p=production.all(); p.hasMoreElements(); ) { prod = (production)p.nextElement(); System.err.print(prod.lhs().the_symbol().name() + " ::= "); for (int i=0; i<prod.rhs_length(); i++) if (prod.rhs(i).is_action()) System.err.print("{action} "); else System.err.print( ((symbol_part)prod.rhs(i)).the_symbol().name() + " "); System.err.println(); } System.err.println(); } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Produce a (semi-) human readable dump of the complete viable prefix * recognition state machine. */ public static void dump_machine() { lalr_state ordered[] = new lalr_state[lalr_state.number()]; /* put the states in sorted order for a nicer display */ for (Enumeration s = lalr_state.all(); s.hasMoreElements(); ) { lalr_state st = (lalr_state)s.nextElement(); ordered[st.index()] = st; } System.err.println("===== Viable Prefix Recognizer ====="); for (int i = 0; i<lalr_state.number(); i++) { if (ordered[i] == start_state) System.err.print("START "); System.err.println(ordered[i]); System.err.println("-------------------"); } } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Produce a (semi-) human readable dumps of the parse tables */ public static void dump_tables() { System.err.println(action_table); System.err.println(reduce_table); } /*-----------------------------------------------------------*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -