Search in sources :

Example 1 with ISyntaxComponent

use of priv.bajdcc.LL1.syntax.ISyntaxComponent in project jMiniLang by bajdcc.

the class BranchExp method visit.

@Override
public void visit(ISyntaxComponentVisitor visitor) {
    VisitBag bag = new VisitBag();
    visitor.visitBegin(this, bag);
    if (bag.bVisitChildren) {
        for (ISyntaxComponent exp : arrExpressions) {
            exp.visit(visitor);
        }
    }
    if (bag.bVisitEnd) {
        visitor.visitEnd(this);
    }
}
Also used : ISyntaxComponent(priv.bajdcc.LALR1.syntax.ISyntaxComponent) VisitBag(priv.bajdcc.util.VisitBag)

Example 2 with ISyntaxComponent

use of priv.bajdcc.LL1.syntax.ISyntaxComponent in project jMiniLang by bajdcc.

the class SequenceExp method visit.

@Override
public void visit(ISyntaxComponentVisitor visitor) {
    VisitBag bag = new VisitBag();
    visitor.visitBegin(this, bag);
    if (bag.bVisitChildren) {
        for (ISyntaxComponent exp : arrExpressions) {
            exp.visit(visitor);
        }
    }
    if (bag.bVisitEnd) {
        visitor.visitEnd(this);
    }
}
Also used : ISyntaxComponent(priv.bajdcc.LALR1.syntax.ISyntaxComponent) VisitBag(priv.bajdcc.util.VisitBag)

Example 3 with ISyntaxComponent

use of priv.bajdcc.LL1.syntax.ISyntaxComponent in project jMiniLang by bajdcc.

the class Syntax method getSingleString.

/**
 * 获得单一产生式描述
 *
 * @param name
 *            非终结符名称
 * @param exp
 *            表达式树
 * @param focused
 *            焦点
 * @param front
 *            前向
 * @return 原产生式描述
 */
public static String getSingleString(String name, ISyntaxComponent exp, ISyntaxComponent focused, boolean front) {
    StringBuilder sb = new StringBuilder();
    sb.append(name);
    sb.append(" -> ");
    SyntaxToString alg = new SyntaxToString(focused, front);
    exp.visit(alg);
    sb.append(alg.toString());
    return sb.toString();
}
Also used : SyntaxToString(priv.bajdcc.LL1.syntax.stringify.SyntaxToString)

Example 4 with ISyntaxComponent

use of priv.bajdcc.LL1.syntax.ISyntaxComponent 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 5 with ISyntaxComponent

use of priv.bajdcc.LL1.syntax.ISyntaxComponent in project jMiniLang by bajdcc.

the class BranchExp method visit.

@Override
public void visit(ISyntaxComponentVisitor visitor) {
    VisitBag bag = new VisitBag();
    visitor.visitBegin(this, bag);
    if (bag.bVisitChildren) {
        for (ISyntaxComponent exp : arrExpressions) {
            exp.visit(visitor);
        }
    }
    if (bag.bVisitEnd) {
        visitor.visitEnd(this);
    }
}
Also used : ISyntaxComponent(priv.bajdcc.LL1.syntax.ISyntaxComponent) VisitBag(priv.bajdcc.util.VisitBag)

Aggregations

ISyntaxComponent (priv.bajdcc.LALR1.syntax.ISyntaxComponent)4 ISyntaxComponent (priv.bajdcc.LL1.syntax.ISyntaxComponent)4 VisitBag (priv.bajdcc.util.VisitBag)4 SyntaxToString (priv.bajdcc.LL1.syntax.stringify.SyntaxToString)2 BranchExp (priv.bajdcc.LL1.syntax.exp.BranchExp)1 IExpCollction (priv.bajdcc.LL1.syntax.exp.IExpCollction)1 SequenceExp (priv.bajdcc.LL1.syntax.exp.SequenceExp)1 RuleItem (priv.bajdcc.LL1.syntax.rule.RuleItem)1 OperatorType (priv.bajdcc.LL1.syntax.token.OperatorType)1