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

📄 grammatica.java

📁 Grammatica是一个C#和Java的语法分析程序生成器(编译器的编译器)。它可以用LL(k)语法创建可读的和带有注释的源代码。它也支持创建一个运行时语法分析器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                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;        Token      token;                try {            tokenizer = grammar.createTokenizer(new FileReader(file));            analyzer = new TreePrinter(new PrintWriter(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);        }    }            /**     * A parse tree printer. This class prints the parse tree while     * it is being parsed.      */    private static class TreePrinter extends Analyzer {        /**         * The current indentation level.         */        private int indentation = 0;         /**         * The output stream to use.         */        private PrintWriter output;        /**         * Creates a new parse tree printer.         *          * @param output         the output stream to use         */        public TreePrinter(PrintWriter output) {            this.output = output;        }        /**         * Called when entering a parse tree node. By default this method         * does nothing. A subclass can override this method to handle          * each node separately.           *          * @param node           the node being entered         */        protected void enter(Node node) {            for (int i = 0; i < indentation; i++) {                output.print("  ");            }            output.println(node.toString());            output.flush();            indentation++;        }            /**         * Called when exiting a parse tree node. By default this method         * returns the node. A subclass can override this method to handle          * each node separately. If no parse tree should be created, this          * method should return null.         *          * @param node           the node being exited         *          * @return the node to add to the parse tree         */        protected Node exit(Node node) {            indentation--;            return null;        }    }}

⌨️ 快捷键说明

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