Search in sources :

Example 1 with SequenceExp

use of priv.bajdcc.LL1.syntax.exp.SequenceExp in project jMiniLang by bajdcc.

the class Syntax method doAnalysis.

/**
 * 分析表达式
 *
 * @param type
 *            结束类型
 * @param obj
 *            结束数据
 * @return 表达式树根结点
 * @throws SyntaxException 词法错误
 */
private ISyntaxComponent doAnalysis(TokenType type, Object obj) throws SyntaxException {
    /* 新建序列用于存放结果 */
    SequenceExp sequence = new SequenceExp();
    /* 可能会使用的分支 */
    BranchExp branch = null;
    /* 添加子结点接口 */
    IExpCollction collection = sequence;
    /* 表达式通用接口 */
    ISyntaxComponent result = sequence;
    for (; ; ) {
        if ((token.kToken == type && (token.object == null || token.object.equals(obj)))) {
            // 结束字符
            if (syntaxLexer.index() == 0) {
                // 表达式为空
                err(SyntaxError.NULL);
            } else if (collection.isEmpty()) {
                // 部件为空
                err(SyntaxError.INCOMPLETE);
            } else {
                next();
                // 正常终止
                break;
            }
        } else if (token.kToken == TokenType.EOF) {
            err(SyntaxError.INCOMPLETE);
        }
        // 当前待赋值的表达式
        ISyntaxComponent exp = null;
        switch(token.kToken) {
            case OPERATOR:
                OperatorType op = (OperatorType) token.object;
                next();
                switch(op) {
                    case ALTERNATIVE:
                        if (// 在此之前没有存储表达式 (|...)
                        collection.isEmpty()) {
                            err(SyntaxError.INCOMPLETE);
                        } else {
                            if (branch == null) {
                                // 分支为空,则建立分支
                                branch = new BranchExp();
                                // 用新建的分支包含并替代当前序列
                                branch.add(sequence);
                                result = branch;
                            }
                            // 新建一个序列
                            sequence = new SequenceExp();
                            branch.add(sequence);
                            continue;
                        }
                        break;
                    default:
                        err(SyntaxError.SYNTAX);
                        break;
                }
                break;
            case EOF:
                return result;
            case TERMINAL:
                exp = matchTerminal();
                next();
                break;
            case NONTERMINAL:
                exp = matchNonTerminal();
                next();
                break;
            default:
                err(SyntaxError.SYNTAX);
                break;
        }
        if (exp != null) {
            sequence.add(exp);
        }
    }
    return result;
}
Also used : IExpCollction(priv.bajdcc.LL1.syntax.exp.IExpCollction) BranchExp(priv.bajdcc.LL1.syntax.exp.BranchExp) SequenceExp(priv.bajdcc.LL1.syntax.exp.SequenceExp) OperatorType(priv.bajdcc.LL1.syntax.token.OperatorType)

Example 2 with SequenceExp

use of priv.bajdcc.LL1.syntax.exp.SequenceExp in project jMiniLang by bajdcc.

the class FirstSetSolver method visitBegin.

@Override
public void visitBegin(SequenceExp node, VisitBag bag) {
    bag.bVisitChildren = false;
    bag.bVisitEnd = false;
    boolean zero = false;
    for (ISyntaxComponent exp : node.arrExpressions) {
        exp.visit(this);
        zero = bZero;
        if (!zero) {
            break;
        }
    }
    bZero = zero;
}
Also used : ISyntaxComponent(priv.bajdcc.LL1.syntax.ISyntaxComponent)

Example 3 with SequenceExp

use of priv.bajdcc.LL1.syntax.exp.SequenceExp in project jMiniLang by bajdcc.

the class Syntax method doAnalysis.

/**
 * 分析表达式
 *
 * @param type
 *            结束类型
 * @param obj
 *            结束数据
 * @return 表达式树根结点
 * @throws SyntaxException 词法错误
 */
private ISyntaxComponent doAnalysis(TokenType type, Object obj) throws SyntaxException {
    /* 新建序列用于存放结果 */
    SequenceExp sequence = new SequenceExp();
    /* 可能会使用的分支 */
    BranchExp branch = null;
    /* 添加子结点接口 */
    IExpCollction collection = sequence;
    /* 表达式通用接口 */
    ISyntaxComponent result = sequence;
    for (; ; ) {
        if ((token.kToken == type && (token.object == null || token.object.equals(obj)))) {
            // 结束字符
            if (syntaxLexer.index() == 0) {
                // 表达式为空
                err(SyntaxError.NULL);
            } else if (collection.isEmpty()) {
                // 部件为空
                err(SyntaxError.INCOMPLETE);
            } else {
                next();
                // 正常终止
                break;
            }
        } else if (token.kToken == TokenType.EOF) {
            err(SyntaxError.INCOMPLETE);
        }
        // 当前待赋值的表达式
        ISyntaxComponent exp = null;
        switch(token.kToken) {
            case OPERATOR:
                OperatorType op = (OperatorType) token.object;
                next();
                switch(op) {
                    case ALTERNATIVE:
                        if (// 在此之前没有存储表达式 (|...)
                        collection.isEmpty()) {
                            err(SyntaxError.INCOMPLETE);
                        } else {
                            if (branch == null) {
                                // 分支为空,则建立分支
                                branch = new BranchExp();
                                // 用新建的分支包含并替代当前序列
                                branch.add(sequence);
                                result = branch;
                            }
                            // 新建一个序列
                            sequence = new SequenceExp();
                            branch.add(sequence);
                            continue;
                        }
                        break;
                    case // '('
                    LPARAN:
                        // 递归分析
                        exp = doAnalysis(TokenType.OPERATOR, OperatorType.RPARAN);
                        break;
                    case // '['
                    LSQUARE:
                        // 递归分析
                        exp = doAnalysis(TokenType.OPERATOR, OperatorType.RSQUARE);
                        // 包装
                        OptionExp option = new OptionExp();
                        option.expression = exp;
                        exp = option;
                        break;
                    default:
                        err(SyntaxError.SYNTAX);
                        break;
                }
                break;
            case EOF:
                return result;
            case TERMINAL:
                exp = matchTerminal();
                next();
                PropertyExp property1 = matchStorage(exp, token.object);
                exp = property1;
                if (token.kToken == TokenType.ACTION) {
                    property1.actionHandler = matchAction();
                    next();
                }
                if (token.kToken == TokenType.HANDLER) {
                    property1.errorHandler = matchHandler();
                    next();
                }
                break;
            case NONTERMINAL:
                exp = matchNonTerminal();
                next();
                PropertyExp property2 = matchStorage(exp, token.object);
                exp = property2;
                if (token.kToken == TokenType.ACTION) {
                    property2.actionHandler = matchAction();
                    next();
                }
                if (token.kToken == TokenType.HANDLER) {
                    property2.errorHandler = matchHandler();
                    next();
                }
                break;
            default:
                err(SyntaxError.SYNTAX);
                break;
        }
        if (exp != null) {
            sequence.add(exp);
        }
    }
    return result;
}
Also used : IExpCollction(priv.bajdcc.LALR1.syntax.exp.IExpCollction) BranchExp(priv.bajdcc.LALR1.syntax.exp.BranchExp) SequenceExp(priv.bajdcc.LALR1.syntax.exp.SequenceExp) OptionExp(priv.bajdcc.LALR1.syntax.exp.OptionExp) PropertyExp(priv.bajdcc.LALR1.syntax.exp.PropertyExp) OperatorType(priv.bajdcc.LALR1.syntax.token.OperatorType)

Aggregations

BranchExp (priv.bajdcc.LALR1.syntax.exp.BranchExp)1 IExpCollction (priv.bajdcc.LALR1.syntax.exp.IExpCollction)1 OptionExp (priv.bajdcc.LALR1.syntax.exp.OptionExp)1 PropertyExp (priv.bajdcc.LALR1.syntax.exp.PropertyExp)1 SequenceExp (priv.bajdcc.LALR1.syntax.exp.SequenceExp)1 OperatorType (priv.bajdcc.LALR1.syntax.token.OperatorType)1 ISyntaxComponent (priv.bajdcc.LL1.syntax.ISyntaxComponent)1 BranchExp (priv.bajdcc.LL1.syntax.exp.BranchExp)1 IExpCollction (priv.bajdcc.LL1.syntax.exp.IExpCollction)1 SequenceExp (priv.bajdcc.LL1.syntax.exp.SequenceExp)1 OperatorType (priv.bajdcc.LL1.syntax.token.OperatorType)1