⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grammatica.java

📁 Grammatica is a C# and Java parser generator (compiler compiler). It improves upon simlar tools (lik
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */    private static void printInternalError(Exception e) {        System.err.println(INTERNAL_ERROR);        e.printStackTrace();    }        /**     * Breaks a string into multiple lines. This method will also add     * a prefix to each line in the resulting string. The prefix      * length will be taken into account when breaking the line. Line     * breaks will only be inserted as replacements for space      * characters.     *      * @param str            the string to line break     * @param prefix         the prefix to add to each line     * @param length         the maximum line length     *      * @return the new formatted string     */    private static String linebreakString(String str,                                           String prefix,                                           int length) {            StringBuffer  buffer = new StringBuffer();        int           pos;        while (str.length() + prefix.length() > length) {            pos = str.lastIndexOf(' ', length - prefix.length());            if (pos < 0) {                pos = str.indexOf(' ');                if (pos < 0) {                    break;                }            }            buffer.append(prefix);            buffer.append(str.substring(0, pos));            str = str.substring(pos + 1);            buffer.append("\n");        }        buffer.append(prefix);        buffer.append(str);        return buffer.toString();    }    /**     * Reads a number of lines from a file. In the file couldn't be      * opened or read correctly, null will be returned.     *      * @param file           the name of the file to read     * @param start          the first line number to read, from one (1)     * @param end            the last line number to read, from one (1)     *      * @return the lines read including newline characters     */    private static String readLines(String file, int start, int end) {        BufferedReader  input;        StringBuffer    buffer = new StringBuffer();        String          str;        // Check invalid line number        if (start < 1 || end < start) {            return null;        }                // Read line from file        try {            input = new BufferedReader(new FileReader(file));            for (int i = 0; i < end; i++) {                str = input.readLine();                if (str == null) {                    input.close();                    return null;                } else if (start <= i + 1) {                    buffer.append(str);                    buffer.append("\n");                }            }            input.close();        } catch (IOException e) {            return null;        }        return buffer.toString();    }    /**     * Debugs a grammar by printing the internal representation.     *      * @param grammar        the grammar to use     */    private static void debug(Grammar grammar) {        Tokenizer  tokenizer = null;        Parser     parser = null;                // Create tokenizer and parser        try {            tokenizer = grammar.createTokenizer(null);            parser = grammar.createParser(tokenizer);        } catch (GrammarException e) {            printInternalError(e);            System.exit(2);        }        // Print tokenizer and parser        System.out.println("Contents of " + grammar.getFileName() + ":");        System.out.println();        System.out.println("Token Declarations:");        System.out.println("-------------------");        System.out.print(tokenizer);        System.out.println("Production Declarations:");        System.out.println("------------------------");        System.out.print(parser);    }    /**     * Tokenizes the specified file with the token patterns from the     * grammar.     *      * @param grammar        the grammar to use     * @param file           the file to parse     */    private static void tokenize(Grammar grammar, File file) {        Tokenizer  tokenizer;        Token      token;                try {            tokenizer = grammar.createTokenizer(new FileReader(file));            System.out.println("Tokens from " + file + ":");            while ((token = tokenizer.next()) != null) {                System.out.println(token);            }        } catch (FileNotFoundException e) {            printError(file.toString(), e);            System.exit(1);        } catch (GrammarException e) {            printInternalError(e);            System.exit(2);        } catch (ParseException e) {            printError(file.toString(), e);            System.exit(1);        }    }        /**     * Parses the specified file with the grammar.     *      * @param grammar        the grammar to use     * @param file           the file to parse     */    private static void parse(Grammar grammar, File file) {        Tokenizer  tokenizer;        Analyzer   analyzer;        Parser     parser;                try {            tokenizer = grammar.createTokenizer(new FileReader(file));            analyzer = new TreePrinter(System.out);            parser = grammar.createParser(tokenizer, analyzer);            System.out.println("Parse tree from " + file + ":");            parser.parse();        } catch (FileNotFoundException e) {            printError(file.toString(), e);            System.exit(1);        } catch (GrammarException e) {            printInternalError(e);            System.exit(2);        } catch (ParserCreationException e) {            printInternalError(e);            System.exit(2);        } catch (ParserLogException e) {            printError(file.toString(), e);            System.exit(1);        }    }    /**     * Parses the specified file with the grammar and prints      * profiling information.     *      * @param grammar        the grammar to use     * @param file           the file to parse     */    private static void profile(Grammar grammar, File file) {        Tokenizer  tokenizer;        Parser     parser;        Node       node;        long       time;        int        counter;                // Profile tokenizer        try {            tokenizer = grammar.createTokenizer(new FileReader(file));            System.out.println("Tokenizing " + file);            time = System.currentTimeMillis();            counter = 0;            while (tokenizer.next() != null) {                counter++;            }            time = System.currentTimeMillis() - time;            System.out.println("  Time elapsed:  " + time + " millisec");            System.out.println("  Tokens found:  " + counter);            System.out.println("  Average speed: " + (counter / time) +                               " tokens/millisec");            System.out.println();        } catch (FileNotFoundException e) {            printError(file.toString(), e);            System.exit(1);        } catch (GrammarException e) {            printInternalError(e);            System.exit(2);        } catch (ParseException e) {            printError(file.toString(), e);            System.exit(1);        }        // Profile parser        try {            tokenizer = grammar.createTokenizer(new FileReader(file));            parser = grammar.createParser(tokenizer);            System.out.println("Parsing " + file);            time = System.currentTimeMillis();            node = parser.parse();            time = System.currentTimeMillis() - time;            counter = 1 + node.getDescendantCount();            System.out.println("  Time elapsed:  " + time + " millisec");            System.out.println("  Nodes found:   " + counter);            System.out.println("  Average speed: " + (counter / time) +                               " nodes/millisec");            System.out.println();        } catch (FileNotFoundException e) {            printError(file.toString(), e);            System.exit(1);        } catch (GrammarException e) {            printInternalError(e);            System.exit(2);        } catch (ParserCreationException e) {            printInternalError(e);            System.exit(2);        } catch (ParserLogException e) {            printError(file.toString(), e);            System.exit(1);        }    }        /**     * Parses the command-line arguments and generates the Java source      * code for a parser.      *      * @param args           the command-line arguments     * @param grammar        the grammar to use     */    private static void writeJavaCode(String[] args, Grammar grammar) {        JavaParserGenerator gen = new JavaParserGenerator(grammar);        // Read command-line arguments        for (int i = 1; i < args.length; i++) {            if (args[i].equals("--javaoutput")) {                gen.setBaseDir(new File(args[++i]));            } else if (args[i].equals("--javapackage")) {                gen.setBasePackage(args[++i]);            } else if (args[i].equals("--javaclassname")) {                gen.setBaseName(args[++i]);            } else if (args[i].equals("--javapublic")) {                gen.setPublicAccess(true);            } else {                printHelp("unrecognized option: " + args[i]);                System.exit(1);            }        }                // Write parser source code        try {            System.out.println("Writing Java parser source code...");            gen.write();            System.out.println("Done.");        } catch (IOException e) {            printError(e);            System.exit(1);        }    }    /**     * Parses the command-line arguments and generates the C# source      * code for a parser.      *      * @param args           the command-line arguments     * @param grammar        the grammar to use     */    private static void writeCSharpCode(String[] args, Grammar grammar) {        CSharpParserGenerator gen = new CSharpParserGenerator(grammar);        // Read command-line arguments        for (int i = 1; i < args.length; i++) {            if (args[i].equals("--csoutput")) {                gen.setBaseDir(new File(args[++i]));            } else if (args[i].equals("--csnamespace")) {                gen.setNamespace(args[++i]);            } else if (args[i].equals("--csclassname")) {                gen.setBaseName(args[++i]);            } else if (args[i].equals("--cspublic")) {                gen.setPublicAccess(true);            } else {                printHelp("unrecognized option: " + args[i]);                System.exit(1);            }        }        // Write parser source code        try {            System.out.println("Writing C# parser source code...");            gen.write();            System.out.println("Done.");        } catch (IOException e) {            printError(e);            System.exit(1);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -