Package net.percederberg.grammatica.parser

Examples of net.percederberg.grammatica.parser.Node


    protected Node exitProductionDeclaration(Production node)
        throws ParseException {

        ProductionPattern             pattern;
        ProductionPatternAlternative  alt;
        Node                          child;

        pattern = (ProductionPattern) getValue(getChildAt(node, 0), 0);
        child = getChildAt(node, 2);
        for (int i = 0; i < child.getValueCount(); i++) {
            alt = (ProductionPatternAlternative) getValue(child, i);
            try {
                pattern.addAlternative(alt);
            } catch (ParserCreationException e) {
                throw new ParseException(
View Full Code Here


     * @throws ParseException if the node analysis discovered errors
     */
    protected Node exitProduction(Production node) throws ParseException {
        ProductionPatternAlternative  alt;
        ProductionPatternElement      elem;
        Node                          child;

        alt = new ProductionPatternAlternative();
        node.addValue(alt);
        for (int i = 0; i < node.getChildCount(); i++) {
            child = getChildAt(node, i);
            if (child.getId() == GrammarConstants.PRODUCTION_ATOM) {
                for (int j = 0; j < child.getValueCount(); j++) {
                    elem = (ProductionPatternElement) getValue(child, j);
                    alt.addElement(elem);
                }
            } else if (child.getId() == GrammarConstants.PRODUCTION) {
                node.addValues(child.getAllValues());
            }
        }

        return node;
    }
View Full Code Here

     * @throws ParseException if the node analysis discovered errors
     */
    protected Node exitProductionAtom(Production node)
        throws ParseException {

        Node     child;
        boolean  token = false;
        int      id = 0;
        int      min = 1;
        int      max = 1;
        Object   obj;

        // Handle the alternatives
        child = getChildAt(node, 0);
        switch (child.getId()) {
        case GrammarConstants.IDENTIFIER:
            obj = getValue(child, 0);
            if (obj instanceof TokenPattern) {
                token = true;
                id = ((TokenPattern) obj).getId();
            } else {
                token = false;
                id = ((ProductionPattern) obj).getId();
            }
            break;
        case GrammarConstants.QUOTED_STRING:
            token = true;
            id = ((TokenPattern) getValue(child, 0)).getId();
            break;
        case GrammarConstants.LEFT_PAREN:
        case GrammarConstants.LEFT_BRACE:
        case GrammarConstants.LEFT_BRACKET:
            ProductionPatternElement  elem;

            if (child.getId() == GrammarConstants.LEFT_BRACE) {
                min = 0;
                max = -1;
            } else if (child.getId() == GrammarConstants.LEFT_BRACKET) {
                min = 0;
                max = 1;
            }
            elem = getProductionElement(getChildAt(node, 1));
            token = elem.isToken();
            id = elem.getId();
            break;
        }

        // Handle optional '?', '*' or '+'
        child = getChildAt(node, node.getChildCount() - 1);
        if (child.getId() == GrammarConstants.QUESTION_MARK) {
            min = 0;
            max = 1;
        } else if (child.getId() == GrammarConstants.ASTERISK) {
            min = 0;
            max = -1;
        } else if (child.getId() == GrammarConstants.PLUS_SIGN) {
            min = 1;
            max = -1;
        }

        // Create production pattern element
View Full Code Here

        TokenPattern  pattern;
        String        name;
        int           type;
        String        str;
        Token         token;
        Node          child;

        // Create token pattern
        name = getIdentifier((Token) getChildAt(node, 0));
        child = getChildAt(node, 2);
        type = getIntValue(child, 0);
        str = getStringValue(child, 1);
        pattern = new TokenPattern(nextTokenId++, name, type, str);

        // Process optional ignore or error
        if (node.getChildCount() == 4) {
            child = getChildAt(node, 3);
            token = (Token) getValue(child, 0);
            str = null;
            if (child.getValueCount() == 2) {
                str = getStringValue(child, 1);
            }
            switch (token.getId()) {
            case GrammarConstants.IGNORE:
                if (str == null) {
View Full Code Here

     * @throws ParseException if the node analysis discovered errors
     */
    protected Node exitTokenHandling(Production node)
        throws ParseException {

        Node  child = getChildAt(node, 0);

        node.addValue(child);
        if (child.getValueCount() > 0) {
            node.addValue(getValue(child, 0));
        }
        return node;
    }
View Full Code Here

     */
    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);
            System.out.println("  Average speed: " + (counter / time) +
View Full Code Here

     *             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) +
                               " nodes/millisec");
        } catch (FileNotFoundException e) {
View Full Code Here

     *
     * @throws Exception if the expression contained an error
     */
    public int calculate(String expression) throws Exception {
        ArithmeticParser  parser;
        Node              node;

        parser = new ArithmeticParser(new StringReader(expression), this);
        parser.prepare();
        node = parser.parse();
        return ((Integer) node.getValue(0)).intValue();
    }
View Full Code Here

     * @throws ParseException if the node analysis discovered errors
     */
    protected Node exitTagDefault(Production node)
        throws ParseException {

        Node  child;

        child = getChildAt(node, 0);
        if (child.getId() == Asn1Constants.EXPLICIT) {
            implicitTags = false;
        } else {
            implicitTags = true;
        }
        return null;
View Full Code Here

        throws ParseException {

        MibImport  imp;
        String     module;
        ArrayList  symbols;
        Node       child;

        // Create MIB reference
        child = getChildAt(node, 0);
        symbols = child.getAllValues();
        if (symbols == null) {
            symbols = new ArrayList();
        }
        child = getChildAt(node, 2);
        module = getStringValue(child, 0);
View Full Code Here

TOP

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

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.