Package net.percederberg.grammatica.parser

Examples of net.percederberg.grammatica.parser.Parser


     *             initialized correctly
     */
    public Parser createParser(Tokenizer tokenizer, Analyzer analyzer)
        throws GrammarException {

        Parser  parser;

        try {
            parser = new RecursiveDescentParser(tokenizer, analyzer);
            for (int i = 0; i < productions.size(); i++) {
                parser.addPattern((ProductionPattern) productions.get(i));
            }
            parser.prepare();
        } catch (ParserCreationException e) {
            LineRange range = (LineRange) lines.get(e.getName());
            if (range == null) {
                throw new GrammarException(fileName, e.getMessage());
            } else {
View Full Code Here


     *
     * @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);
View Full Code Here

     * @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);
View Full Code Here

     * @param first          the index of the first file
     */
    private static void profile(Grammar grammar, String[] files, int first) {
        File       file = new File(files[first]);
        Tokenizer  tokenizer;
        Parser     parser;
        Node       node;
        int        fileCount = files.length - first;
        long       time;
        int        counter;

        // Profile tokenizer
        try {
            System.out.println("Tokenizing " + fileCount + " file(s)...");
            tokenizer = grammar.createTokenizer(new FileReader(file));
            time = System.currentTimeMillis();
            counter = 0;
            for (int i = first; i < files.length; i++) {
                if (i > first) {
                    file = new File(files[i]);
                    tokenizer.reset(new FileReader(file));
                }
                while (tokenizer.next() != null) {
                    counter++;
                }
            }
            time = System.currentTimeMillis() - time + 1;
            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 {
            System.out.println("Parsing " + fileCount + " file(s)...");
            file = new File(files[first]);
            tokenizer = grammar.createTokenizer(new FileReader(file));
            parser = grammar.createParser(tokenizer);
            time = System.currentTimeMillis();
            counter = 0;
            for (int i = first; i < files.length; i++) {
                if (i > first) {
                    file = new File(files[i]);
                    parser.reset(new FileReader(file));
                }
                node = parser.parse();
                counter += 1 + node.getDescendantCount();
            }
            time = System.currentTimeMillis() - time + 1;
            System.out.println("  Time elapsed:  " + time + " millisec");
            System.out.println("  Nodes found:   " + counter);
View Full Code Here

     *
     * @throws RuntimeException if a parser couldn't be created
     */
    private void debug(Grammar grammar) throws RuntimeException {
        Tokenizer  tokenizer = null;
        Parser     parser = null;

        // Create tokenizer and parser
        try {
            tokenizer = grammar.createTokenizer(null);
            parser = grammar.createParser(tokenizer);
View Full Code Here

     *             correctly
     */
    private void parse(Grammar grammar) throws RuntimeException {
        Tokenizer  tokenizer;
        Analyzer   analyzer;
        Parser     parser;

        try {
            tokenizer = grammar.createTokenizer(new FileReader(file));
            if (quiet) {
                analyzer = null;
            } else {
                analyzer = new TreePrinter(System.out);
            }
            parser = grammar.createParser(tokenizer, analyzer);
            if (!quiet) {
                System.out.println("Parse tree from " + file + ":");
            }
            parser.parse();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.getMessage());
        } catch (GrammarException e) {
            throw new RuntimeException("in grammar " + grammar.getFileName() +
                                       ": " + e.getMessage());
View Full Code Here

     * @throws RuntimeException if the input file couldn't be profiled
     *             correctly
     */
    private void profile(Grammar grammar) throws RuntimeException {
        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");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.getMessage());
        } catch (GrammarException e) {
            throw new RuntimeException("in grammar " + grammar.getFileName() +
                                       ": " + e.getMessage());
        } catch (ParseException e) {
            throw new RuntimeException("in file " + file + ": " +
                                       e.getMessage());
        }

        // 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) +
View Full Code Here

     * @param input          the input to parse
     *
     * @return the parser created
     */
    private Parser createParser(String input) {
        Parser  parser = null;

        try {
            parser = new RegexpParser(new StringReader(input));
            parser.prepare();
        } catch (ParserCreationException e) {
            fail(e.getMessage());
        }
        return parser;
    }
View Full Code Here

    /**
     * Tests reusing the same parser for various different inputs.
     */
    public void testParserReusage() {
        Parser  p;

        p = createParser(VALID_INPUT);
        parse(p, VALID_OUTPUT);
        p.getTokenizer().reset(new StringReader(UNEXPECTED_CHAR_INPUT));
        failParse(p, ParseException.UNEXPECTED_CHAR_ERROR, 2, 2);
        p.getTokenizer().reset(new StringReader(VALID_INPUT));
        parse(p, VALID_OUTPUT);
    }
View Full Code Here

     * @param input          the input to parse
     *
     * @return the parser created
     */
    private Parser createParser(String input) {
        Parser  parser = null;

        try {
            parser = new ArithmeticParser(new StringReader(input));
            parser.prepare();
        } catch (ParserCreationException e) {
            fail(e.getMessage());
        }
        return parser;
    }
View Full Code Here

TOP

Related Classes of net.percederberg.grammatica.parser.Parser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.