📄 main.java
字号:
/* record the name */ emit.package_name = argv[i]; } else if (argv[i].equals("-parser")) { /* must have an arg */ if (++i >= len || argv[i].startsWith("-") || argv[i].endsWith(".cup")) usage("-parser must have a name argument"); /* record the name */ emit.parser_class_name = argv[i]; } else if (argv[i].equals("-symbols")) { /* must have an arg */ if (++i >= len || argv[i].startsWith("-") || argv[i].endsWith(".cup")) usage("-symbols must have a name argument"); /* record the name */ emit.symbol_const_class_name = argv[i]; } else if (argv[i].equals("-nonterms")) { include_non_terms = true; } else if (argv[i].equals("-expect")) { /* must have an arg */ if (++i >= len || argv[i].startsWith("-") || argv[i].endsWith(".cup")) usage("-expect must have a name argument"); /* record the number */ try { expect_conflicts = Integer.parseInt(argv[i]); } catch (NumberFormatException e) { usage("-expect must be followed by a decimal integer"); } } else if (argv[i].equals("-compact_red")) opt_compact_red = true; else if (argv[i].equals("-nosummary")) no_summary = true; else if (argv[i].equals("-nowarn")) emit.nowarn = true; else if (argv[i].equals("-dump_states")) opt_dump_states = true; else if (argv[i].equals("-dump_tables")) opt_dump_tables = true; else if (argv[i].equals("-progress")) print_progress = true; else if (argv[i].equals("-dump_grammar")) opt_dump_grammar = true; else if (argv[i].equals("-dump")) opt_dump_states = opt_dump_tables = opt_dump_grammar = true; else if (argv[i].equals("-time")) opt_show_timing = true; else if (argv[i].equals("-debug")) opt_do_debug = true; /* frankf 6/18/96 */ else if (argv[i].equals("-nopositions")) lr_values = false; /* CSA 12/21/97 */ else if (argv[i].equals("-interface")) sym_interface = true; /* CSA 23-Jul-1999 */ else if (argv[i].equals("-noscanner")) suppress_scanner = true; /* CSA 23-Jul-1999 */ else if (argv[i].equals("-version")) { System.out.println(version.title_str); System.exit(1); } /* CSA 24-Jul-1999; suggestion by Jean Vaucher */ else if (!argv[i].startsWith("-") && i==len-1) { /* use input from file. */ try { System.setIn(new FileInputStream(argv[i])); } catch (java.io.FileNotFoundException e) { usage("Unable to open \"" + argv[i] +"\" for input"); } } else { usage("Unrecognized option \"" + argv[i] + "\""); } } } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /*-------*/ /* Files */ /*-------*/ /** Input file. This is a buffered version of System.in. */ protected static BufferedInputStream input_file; /** Output file for the parser class. */ protected static PrintWriter parser_class_file; /** Output file for the symbol constant class. */ protected static PrintWriter symbol_class_file; /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Open various files used by the system. */ protected static void open_files() { File fil; String out_name; /* open each of the output files */ /* parser class */ out_name = emit.parser_class_name + ".java"; fil = new File(out_name); try { parser_class_file = new PrintWriter( new BufferedOutputStream(new FileOutputStream(fil), 4096)); } catch(Exception e) { System.err.println("Can't open \"" + out_name + "\" for output"); System.exit(3); } /* symbol constants class */ out_name = emit.symbol_const_class_name + ".java"; fil = new File(out_name); try { symbol_class_file = new PrintWriter( new BufferedOutputStream(new FileOutputStream(fil), 4096)); } catch(Exception e) { System.err.println("Can't open \"" + out_name + "\" for output"); System.exit(4); } } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Close various files used by the system. */ protected static void close_files() throws java.io.IOException { if (input_file != null) input_file.close(); if (parser_class_file != null) parser_class_file.close(); if (symbol_class_file != null) symbol_class_file.close(); } /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /** Parse the grammar specification from standard input. This produces * sets of terminal, non-terminals, and productions which can be accessed * via static variables of the respective classes, as well as the setting * of various variables (mostly in the emit class) for small user supplied * items such as the code to scan with. */ protected static void parse_grammar_spec() throws java.lang.Exception { parser parser_obj; /* create a parser and parse with it */ parser_obj = new parser(); try { if (opt_do_debug) parser_obj.debug_parse(); else parser_obj.parse(); } catch (Exception e) { /* 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -