Search in sources :

Example 6 with BreadthFirstSearch

use of priv.bajdcc.util.lexer.automata.BreadthFirstSearch in project jMiniLang by bajdcc.

the class NGAToString method visitBegin.

@Override
public void visitBegin(NGAStatus status, VisitBag bag) {
    /* 若首次访问节点则先构造状态表 */
    if (arrNGAStatus.isEmpty()) {
        BreadthFirstSearch<NGAEdge, NGAStatus> bfs = new BreadthFirstSearch<>();
        status.visit(bfs);
        arrNGAStatus = bfs.arrStatus;
    }
    /* 输出状态标签 */
    appendLine();
    appendPrefix();
    context.append("--== 状态[").append(arrNGAStatus.indexOf(status)).append("]").append(status.data.bFinal ? "[结束]" : "").append(" ==--");
    appendLine();
    appendPrefix();
    context.append("项目: ").append(status.data.label);
    appendLine();
    /* 输出边 */
    for (NGAEdge edge : status.outEdges) {
        appendPrefix();
        context.append("\t到达 ").append(arrNGAStatus.indexOf(edge.end)).append("  :  ");
        context.append(edge.data.kAction.getName());
        switch(edge.data.kAction) {
            case EPSILON:
                break;
            case RULE:
                context.append(" = ").append(edge.data.rule);
                break;
            case TOKEN:
                context.append(" = ").append(edge.data.token);
                break;
            default:
                break;
        }
        appendLine();
    }
}
Also used : BreadthFirstSearch(priv.bajdcc.util.lexer.automata.BreadthFirstSearch) NGAEdge(priv.bajdcc.LALR1.syntax.automata.nga.NGAEdge) NGAStatus(priv.bajdcc.LALR1.syntax.automata.nga.NGAStatus)

Example 7 with BreadthFirstSearch

use of priv.bajdcc.util.lexer.automata.BreadthFirstSearch in project jMiniLang by bajdcc.

the class NPAStatus method visit.

/**
 * 用于遍历包括该状态在内的所有状态(连通),结果存放在PATH中
 *
 * @param bfs
 *            遍历算法
 */
public void visit(BreadthFirstSearch<NPAEdge, NPAStatus> bfs) {
    ArrayList<NPAStatus> stack = bfs.arrStatus;
    HashSet<NPAStatus> set = new HashSet<>();
    stack.clear();
    set.add(this);
    stack.add(this);
    for (int i = 0; i < stack.size(); i++) {
        // 遍历每个状态
        NPAStatus status = stack.get(i);
        VisitBag bag = new VisitBag();
        bfs.visitBegin(status, bag);
        if (bag.bVisitChildren) {
            // 遍历状态的出边
            // 边未被访问,且边类型符合要求
            status.outEdges.stream().filter(edge -> !set.contains(edge.end) && bfs.testEdge(edge)).forEach(edge -> {
                // 边未被访问,且边类型符合要求
                stack.add(edge.end);
                set.add(edge.end);
            });
        }
        if (bag.bVisitEnd) {
            bfs.visitEnd(status);
        }
    }
}
Also used : HashSet(java.util.HashSet) VisitBag(priv.bajdcc.util.VisitBag) BreadthFirstSearch(priv.bajdcc.util.lexer.automata.BreadthFirstSearch) ArrayList(java.util.ArrayList) VisitBag(priv.bajdcc.util.VisitBag) HashSet(java.util.HashSet)

Aggregations

BreadthFirstSearch (priv.bajdcc.util.lexer.automata.BreadthFirstSearch)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 VisitBag (priv.bajdcc.util.VisitBag)4 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Collectors (java.util.stream.Collectors)2 NGAEdge (priv.bajdcc.LALR1.syntax.automata.nga.NGAEdge)2 NGAStatus (priv.bajdcc.LALR1.syntax.automata.nga.NGAStatus)2 Collection (java.util.Collection)1 ListIterator (java.util.ListIterator)1 Entry (java.util.Map.Entry)1 ISemanticAction (priv.bajdcc.LALR1.semantic.token.ISemanticAction)1 NGA (priv.bajdcc.LALR1.syntax.automata.nga.NGA)1 NGAEdgeType (priv.bajdcc.LALR1.syntax.automata.nga.NGAEdgeType)1 RuleExp (priv.bajdcc.LALR1.syntax.exp.RuleExp)1 TokenExp (priv.bajdcc.LALR1.syntax.exp.TokenExp)1 Rule (priv.bajdcc.LALR1.syntax.rule.Rule)1 RuleItem (priv.bajdcc.LALR1.syntax.rule.RuleItem)1 EdgeType (priv.bajdcc.util.lexer.automata.EdgeType)1