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

📄 parser.java

📁 Grammatica是一个C#和Java的语法分析程序生成器(编译器的编译器)。它可以用LL(k)语法创建可读的和带有注释的源代码。它也支持创建一个运行时语法分析器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */    ProductionPattern getPattern(int id) {        Integer  value = new Integer(id);                return (ProductionPattern) patternIds.get(value);    }    /**     * Returns the production pattern for the starting production.     *       * @return the start production pattern, or     *         null if no patterns have been added     */    ProductionPattern getStartPattern() {        if (patterns.size() <= 0) {            return null;        } else {            return (ProductionPattern) patterns.get(0);        }    }    /**     * Returns the ordered set of production patterns.     *      * @return the ordered set of production patterns     */    Collection getPatterns() {        return patterns;    }    /**     * Handles the parser entering a production. This method calls the     * appropriate analyzer callback if the node is not hidden. Note     * that this method will not call any callback if an error      * requiring recovery has ocurred.     *      * @param node           the parse tree node     */    void enterNode(Node node) {        if (!node.isHidden() && errorRecovery < 0) {            try {                analyzer.enter(node);            } catch (ParseException e) {                addError(e, false);            }        }    }        /**     * Handles the parser leaving a production. This method calls the     * appropriate analyzer callback if the node is not hidden, and      * returns the result. Note that this method will not call any      * callback if an error requiring recovery has ocurred.     *      * @param node           the parse tree node     *      * @return the parse tree node, or     *         null if no parse tree should be created     */    Node exitNode(Node node) {        if (!node.isHidden() && errorRecovery < 0) {            try {                return analyzer.exit(node);            } catch (ParseException e) {                addError(e, false);            }        }        return node;    }    /**     * Handles the parser adding a child node to a production. This      * method calls the appropriate analyzer callback. Note that this      * method will not call any callback if an error requiring      * recovery has ocurred.     *      * @param node           the parent parse tree node     * @param child          the child parse tree node, or null     */    void addNode(Production node, Node child) {        if (errorRecovery >= 0) {            // Do nothing        } else if (node.isHidden()) {            node.addChild(child);        } else if (child != null && child.isHidden()) {            for (int i = 0; i < child.getChildCount(); i++) {                addNode(node, child.getChildAt(i));            }        } else {            try {                analyzer.child(node, child);            } catch (ParseException e) {                addError(e, false);            }        }    }    /**     * Reads and consumes the next token in the queue. If no token was     * available for consumation, a parse error will be thrown.     *      * @return the token consumed     *      * @throws ParseException if the input stream couldn't be read or     *             parsed correctly     */    Token nextToken() throws ParseException {        Token  token = peekToken(0);                if (token != null) {            tokens.remove(0);            return token;        } else {            throw new ParseException(                ParseException.UNEXPECTED_EOF_ERROR,                null,                tokenizer.getCurrentLine(),                tokenizer.getCurrentColumn());        }    }        /**     * Reads and consumes the next token in the queue. If no token was     * available for consumation, a parse error will be thrown. A      * parse error will also be thrown if the token id didn't match      * the specified one.      *     * @param id             the expected token id     *       * @return the token consumed     *      * @throws ParseException if the input stream couldn't be parsed     *             correctly, or if the token wasn't expected     */    Token nextToken(int id) throws ParseException {        Token      token = nextToken();        ArrayList  list;                if (token.getId() == id) {            if (errorRecovery > 0) {                errorRecovery--;            }            return token;        } else {            list = new ArrayList(1);            list.add(tokenizer.getPatternDescription(id));            throw new ParseException(                ParseException.UNEXPECTED_TOKEN_ERROR,                token.toShortString(),                list,                token.getStartLine(),                token.getStartColumn());        }    }    /**     * Returns a token from the queue. This method is used to check      * coming tokens before they have been consumed. Any number of      * tokens forward can be checked.      *      * @param steps          the token queue number, zero (0) for first     *      * @return the token in the queue, or     *         null if no more tokens in the queue     */    Token peekToken(int steps) {        Token  token;        while (steps >= tokens.size()) {            try {                token = tokenizer.next();                if (token == null) {                    return null;                } else {                    tokens.add(token);                }            } catch (ParseException e) {                addError(e, true);            }        }        return (Token) tokens.get(steps);    }    /**     * Returns a string representation of this parser. The string will     * contain all the production definitions and various additional      * information.     *      * @return a detailed string representation of this parser     */    public String toString() {        StringBuffer  buffer = new StringBuffer();                for (int i = 0; i < patterns.size(); i++) {            buffer.append(toString((ProductionPattern) patterns.get(i)));             buffer.append("\n");        }        return buffer.toString();    }    /**     * Returns a string representation of a production pattern.     *      * @param prod           the production pattern     *      * @return a detailed string representation of the pattern     */    private String toString(ProductionPattern prod) {        StringBuffer  buffer = new StringBuffer();        StringBuffer  indent = new StringBuffer();        LookAheadSet  set;        int           i;        buffer.append(prod.getName());        buffer.append(" (");        buffer.append(prod.getId());        buffer.append(") ");        for (i = 0; i < buffer.length(); i++) {            indent.append(" ");        }        buffer.append("= ");        indent.append("| ");        for (i = 0; i < prod.getAlternativeCount(); i++) {            if (i > 0) {                buffer.append(indent);            }            buffer.append(toString(prod.getAlternative(i)));            buffer.append("\n");        }        for (i = 0; i < prod.getAlternativeCount(); i++) {            set = prod.getAlternative(i).getLookAhead();            if (set.getMaxLength() > 1) {                buffer.append("Using ");                buffer.append(set.getMaxLength());                buffer.append(" token look-ahead for alternative ");                 buffer.append(i + 1);                buffer.append(": ");                buffer.append(set.toString(tokenizer));                buffer.append("\n");            }        }        return buffer.toString();    }        /**     * Returns a string representation of a production pattern      * alternative.     *      * @param alt            the production pattern alternative     *      * @return a detailed string representation of the alternative     */    private String toString(ProductionPatternAlternative alt) {        StringBuffer  buffer = new StringBuffer();        for (int i = 0; i < alt.getElementCount(); i++) {            if (i > 0) {                buffer.append(" ");            }            buffer.append(toString(alt.getElement(i)));        }        return buffer.toString();    }    /**     * Returns a string representation of a production pattern      * element.     *      * @param elem           the production pattern element     *      * @return a detailed string representation of the element     */    private String toString(ProductionPatternElement elem) {        StringBuffer  buffer = new StringBuffer();        int           min = elem.getMinCount();        int           max = elem.getMaxCount();        if (min == 0 && max == 1) {            buffer.append("[");        }        if (elem.isToken()) {            buffer.append(getTokenDescription(elem.getId()));        } else {            buffer.append(getPattern(elem.getId()).getName());        }        if (min == 0 && max == 1) {            buffer.append("]");        } else if (min == 0 && max == Integer.MAX_VALUE) {            buffer.append("*");        } else if (min == 1 && max == Integer.MAX_VALUE) {            buffer.append("+");        } else if (min != 1 || max != 1) {            buffer.append("{");            buffer.append(min);            buffer.append(",");            buffer.append(max);            buffer.append("}");        }        return buffer.toString();    }    /**     * Returns a token description for a specified token.     *      * @param token          the token to describe     *      * @return the token description     */    String getTokenDescription(int token) {        if (tokenizer == null) {            return "";        } else {            return tokenizer.getPatternDescription(token);        }    }}

⌨️ 快捷键说明

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