📄 tool.java
字号:
if ( generate_DFA_dot ) { generateDFAs(grammar); } if ( report ) { GrammarReport report = new GrammarReport(grammar); System.out.println(report.toString()); // print out a backtracking report too (that is not encoded into log) System.out.println(report.getBacktrackingReport()); // same for aborted NFA->DFA conversions System.out.println(report.getEarlyTerminationReport()); } if ( profile ) { GrammarReport report = new GrammarReport(grammar); Stats.writeReport(GrammarReport.GRAMMAR_STATS_FILENAME, report.toNotifyString()); } // now handle the lexer if one was created for a merged spec String lexerGrammarStr = grammar.getLexerGrammar(); if ( grammar.type==Grammar.COMBINED && lexerGrammarStr!=null ) { String lexerGrammarFileName = grammar.getImplicitlyGeneratedLexerFileName(); Writer w = getOutputFile(grammar,lexerGrammarFileName); w.write(lexerGrammarStr); w.close(); StringReader sr = new StringReader(lexerGrammarStr); Grammar lexerGrammar = new Grammar(); lexerGrammar.setTool(this); File lexerGrammarFullFile = new File(getFileDirectory(lexerGrammarFileName),lexerGrammarFileName); lexerGrammar.setFileName(lexerGrammarFullFile.toString()); lexerGrammar.importTokenVocabulary(grammar); lexerGrammar.setGrammarContent(sr); sr.close(); processGrammar(lexerGrammar); } } catch (IOException e) { ErrorManager.error(ErrorManager.MSG_CANNOT_OPEN_FILE, grammarFileName); } catch (Exception e) { ErrorManager.error(ErrorManager.MSG_INTERNAL_ERROR, grammarFileName, e); } } } public Grammar getGrammar(String grammarFileName) throws IOException, antlr.TokenStreamException, antlr.RecognitionException { //StringTemplate.setLintMode(true); FileReader fr = null; fr = new FileReader(grammarFileName); BufferedReader br = new BufferedReader(fr); Grammar grammar = new Grammar(this,grammarFileName,br); grammar.setWatchNFAConversion(internalOption_watchNFAConversion); br.close(); fr.close(); return grammar; } protected void processGrammar(Grammar grammar) { String language = (String)grammar.getOption("language"); if ( language!=null ) { CodeGenerator generator = new CodeGenerator(this, grammar, language); grammar.setCodeGenerator(generator); generator.setDebug(debug); generator.setProfile(profile); generator.setTrace(trace); generator.genRecognizer(); } } protected void generateDFAs(Grammar g) { for (int d=1; d<=g.getNumberOfDecisions(); d++) { DFA dfa = g.getLookaheadDFA(d); if ( dfa==null ) { continue; // not there for some reason, ignore } DOTGenerator dotGenerator = new DOTGenerator(g); String dot = dotGenerator.getDOT( dfa.startState ); String dotFileName = g.name+"_dec-"+d; try { writeDOTFile(g, dotFileName, dot); } catch(IOException ioe) { ErrorManager.error(ErrorManager.MSG_CANNOT_GEN_DOT_FILE, dotFileName, ioe); } } } protected void generateNFAs(Grammar g) { DOTGenerator dotGenerator = new DOTGenerator(g); Collection rules = g.getRules(); for (Iterator itr = rules.iterator(); itr.hasNext();) { Rule r = (Rule) itr.next(); String ruleName = r.name; try { writeDOTFile( g, ruleName, dotGenerator.getDOT(g.getRuleStartState(ruleName))); } catch (IOException ioe) { ErrorManager.error(ErrorManager.MSG_CANNOT_WRITE_FILE, ioe); } } } protected void writeDOTFile(Grammar g, String name, String dot) throws IOException { Writer fw = getOutputFile(g, name+".dot"); fw.write(dot); fw.close(); } private static void help() { System.err.println("usage: java org.antlr.Tool [args] file.g [file2.g file3.g ...]"); System.err.println(" -o outputDir specify output directory where all output is generated"); System.err.println(" -fo outputDir same as -o but force even files with relative paths to dir"); System.err.println(" -lib dir specify location of token files"); System.err.println(" -depend generate file dependencies"); System.err.println(" -report print out a report about the grammar(s) processed"); System.err.println(" -print print out the grammar without actions"); System.err.println(" -debug generate a parser that emits debugging events"); System.err.println(" -profile generate a parser that computes profiling information"); System.err.println(" -nfa generate an NFA for each rule"); System.err.println(" -dfa generate a DFA for each decision point"); System.err.println(" -message-format name specify output style for messages"); System.err.println(" -X display extended argument list"); } private static void Xhelp() { System.err.println(" -Xgrtree print the grammar AST"); System.err.println(" -Xdfa print DFA as text "); System.err.println(" -Xnoprune test lookahead against EBNF block exit branches"); System.err.println(" -Xnocollapse collapse incident edges into DFA states"); System.err.println(" -Xdbgconversion dump lots of info during NFA conversion"); System.err.println(" -Xmultithreaded run the analysis in 2 threads"); System.err.println(" -Xnomergestopstates do not merge stop states"); System.err.println(" -Xdfaverbose generate DFA states in DOT with NFA configs"); System.err.println(" -Xwatchconversion print a message for each NFA before converting"); System.err.println(" -XdbgST put tags at start/stop of all templates in output"); System.err.println(" -Xm m max number of rule invocations during conversion"); System.err.println(" -Xmaxdfaedges m max \"comfortable\" number of edges for single DFA state"); System.err.println(" -Xconversiontimeout t set NFA conversion timeout for each decision"); System.err.println(" -Xnoinlinedfa make all DFA with tables; no inline prediction with IFs"); } public void setOutputDirectory(String outputDirectory) { this.outputDirectory = outputDirectory; } /** This method is used by all code generators to create new output * files. If the outputDir set by -o is not present it will be created. * The final filename is sensitive to the output directory and * the directory where the grammar file was found. If -o is /tmp * and the original grammar file was foo/t.g then output files * go in /tmp/foo. * * The output dir -o spec takes precedence if it's absolute. * E.g., if the grammar file dir is absolute the output dir is given * precendence. "-o /tmp /usr/lib/t.g" results in "/tmp/T.java" as * output (assuming t.g holds T.java). * * If no -o is specified, then just write to the directory where the * grammar file was found. * * If outputDirectory==null then write a String. */ public Writer getOutputFile(Grammar g, String fileName) throws IOException { if ( outputDirectory==null ) { return new StringWriter(); } // output directory is a function of where the grammar file lives // for subdir/T.g, you get subdir here. Well, depends on -o etc... File outputDir = getOutputDirectory(g.getFileName()); File outputFile = new File(outputDir, fileName); if( !outputDir.exists() ) { outputDir.mkdirs(); } FileWriter fw = new FileWriter(outputFile); return new BufferedWriter(fw); } public File getOutputDirectory(String fileNameWithPath) { File outputDir = new File(outputDirectory); String fileDirectory = getFileDirectory(fileNameWithPath); if ( outputDirectory!=UNINITIALIZED_DIR ) { // -o /tmp /var/lib/t.g => /tmp/T.java // -o subdir/output /usr/lib/t.g => subdir/output/T.java // -o . /usr/lib/t.g => ./T.java if ( fileDirectory!=null && (new File(fileDirectory).isAbsolute() || fileDirectory.startsWith("~")) || // isAbsolute doesn't count this :( forceAllFilesToOutputDir ) { // somebody set the dir, it takes precendence; write new file there outputDir = new File(outputDirectory); } else { // -o /tmp subdir/t.g => /tmp/subdir/t.g if ( fileDirectory!=null ) { outputDir = new File(outputDirectory, fileDirectory); } else { outputDir = new File(outputDirectory); } } } else { // they didn't specify a -o dir so just write to location // where grammar is, absolute or relative String dir = "."; if ( fileDirectory!=null ) { dir = fileDirectory; } outputDir = new File(dir); } return outputDir; } /** Open a file in the -lib dir. For now, it's just .tokens files */ public BufferedReader getLibraryFile(String fileName) throws IOException { String fullName = libDirectory+File.separator+fileName; FileReader fr = new FileReader(fullName); BufferedReader br = new BufferedReader(fr); return br; } public String getLibraryDirectory() { return libDirectory; } /** Return the directory containing the grammar file for this grammar. * normally this is a relative path from current directory. People will * often do "java org.antlr.Tool grammars/*.g3" So the file will be * "grammars/foo.g3" etc... This method returns "grammars". */ public String getFileDirectory(String fileName) { File f = new File(fileName); return f.getParent(); } /** If the tool needs to panic/exit, how do we do that? */ public void panic() { throw new Error("ANTLR panic"); } /** Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss */ public static String getCurrentTimeStamp() { GregorianCalendar calendar = new java.util.GregorianCalendar(); int y = calendar.get(Calendar.YEAR); int m = calendar.get(Calendar.MONTH)+1; // zero-based for months int d = calendar.get(Calendar.DAY_OF_MONTH); int h = calendar.get(Calendar.HOUR_OF_DAY); int min = calendar.get(Calendar.MINUTE); int sec = calendar.get(Calendar.SECOND); String sy = String.valueOf(y); String sm = m<10?"0"+m:String.valueOf(m); String sd = d<10?"0"+d:String.valueOf(d); String sh = h<10?"0"+h:String.valueOf(h); String smin = min<10?"0"+min:String.valueOf(min); String ssec = sec<10?"0"+sec:String.valueOf(sec); return new StringBuffer().append(sy).append("-").append(sm).append("-") .append(sd).append(" ").append(sh).append(":").append(smin) .append(":").append(ssec).toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -