Beispiel #1
0
 public DFAGraphFromLR0(InputGrammer inputGrammer) : base(inputGrammer)
 {
     DFANodes.Add(new DFANode(closure(new List <ProductionInLR0> {
         new ProductionInLR0(inputGrammer.userProductions[0])
     }), Convert.ToString(DFANodes.Count)));
     generateDFAGraph <ProductionInLR0>(DFANodes);
 }
Beispiel #2
0
 public DFAGraphFromLR1(InputGrammer inputGrammer) : base(inputGrammer)
 {
     generatedFirst = new GenerateFirst(inputGrammer);
     DFANodes.Add(new DFANode(closure(new List <ProductionInLR0> {
         new ProductionInLR1(inputGrammer.userProductions[0], new List <string> {
             PublicFunc.ENDSYMBOL
         })
     }), DFANodes.Count.ToString()));
     generateDFAGraph <ProductionInLR1>(DFANodes);
 }
Beispiel #3
0
        private void Get_DFAGraphFromEClosureGraph(E_CLOSURE_GRAPH eClosureGraph)
        {
            HashSet <E_CLOSURE_NODE> visitedNodes           = new HashSet <E_CLOSURE_NODE>();
            Stack <E_CLOSURE_NODE>   EClosureNodesToProcess = new Stack <E_CLOSURE_NODE>();
            Stack <NODE>             NodesToProcess         = new Stack <NODE>();

            E_CLOSURE_NODE eClosureNodeInProcess = null;
            NODE           node    = null;
            NODE           newNode = null;

            EClosureNodesToProcess.Push(eClosureGraph.StartNode);

            startNode = NODE.CreateNode(eClosureGraph.StartNode.nodeType, eClosureGraph.StartNode.NodeID);
            NodesToProcess.Push(startNode);

            while (EClosureNodesToProcess.Count != 0)
            {
                eClosureNodeInProcess = EClosureNodesToProcess.Pop();
                node = NodesToProcess.Pop();
                visitedNodes.Add(eClosureNodeInProcess);
                DFANodes.Add(node);

                foreach (EDGE edge in eClosureNodeInProcess.edges)
                {
                    if (!visitedNodes.Contains((E_CLOSURE_NODE)edge.nextNode))
                    {
                        newNode = NODE.CreateNode(edge.nextNode.nodeType, edge.nextNode.NodeID);
                    }
                    else
                    {
                        newNode = DFANodes.Find(findNode => findNode.NodeID == edge.nextNode.NodeID);
                    }
                    node.edges.Add(new EDGE(edge.transitionCharacter, newNode));

                    if (!visitedNodes.Contains((E_CLOSURE_NODE)edge.nextNode))
                    {
                        EClosureNodesToProcess.Push((E_CLOSURE_NODE)edge.nextNode);
                        NodesToProcess.Push(newNode);
                    }
                }
            }
        }
Beispiel #4
0
    protected void mergeCore()
    {
        Dictionary <int, List <int> > relations = new Dictionary <int, List <int> >();
        HashSet <string> merged = new HashSet <string>();

        for (int i = 0; i < DFANodes.Count; i++)
        {
            for (int j = i + 1; j < DFANodes.Count; j++)
            {
                if (bothHasSameCore(DFANodes[i].productions, DFANodes[j].productions))
                {
                    // 合并搜索符
                    foreach (ProductionInLR1 productionOne in DFANodes[i].productions)
                    {
                        foreach (ProductionInLR1 productionTwo in DFANodes[j].productions)
                        {
                            // 同心 但搜索符不同
                            if (productionTwo.hasSameCoreWith(productionOne) && !productionTwo.Equals(productionOne))
                            {
                                // 搜索符取并集
                                productionOne.searchTokens = productionOne.searchTokens.Union(productionTwo.searchTokens).ToList();
                                break;
                            }
                        }
                    }
                    // 建立 被合并与合并关系
                    if (!relations.ContainsKey(i))
                    {
                        relations.Add(i, new List <int>()
                        {
                            j
                        });
                    }
                    else
                    {
                        relations[i].Add(j);
                    }
                    // 添加即将删除的项集ID
                    merged.Add(DFANodes[j].ID);
                }
            }
        }
        foreach (var relation in relations)
        {
            // 更新边信息
            foreach (int value in relation.Value)
            {
                string valueStr = value.ToString();
                // 更新边信息
                foreach (DFANode node in DFANodes)
                {
                    foreach (Degree degree in node.degrees)
                    {
                        if (valueStr.Equals(degree.degreeOut))
                        {
                            degree.degreeOut = relation.Key.ToString();
                        }
                    }
                }
                // 新增被合并的项集的信息
                DFANodes[relation.Key].degrees = DFANodes[relation.Key].degrees.Union(DFANodes[value].degrees).ToList();
            }
        }
        // 移除被合并的项集
        DFANodes.RemoveAll(i => merged.Contains(i.ID));
        //更新ID,从0顺序排列
        for (int i = 0; i < DFANodes.Count; i++)
        {
            string oldID = DFANodes[i].ID;
            DFANodes[i].ID = Convert.ToString(i);
            foreach (DFANode node in DFANodes)
            {
                foreach (Degree degree in node.degrees)
                {
                    if (oldID.Equals(degree.degreeOut))
                    {
                        degree.degreeOut = DFANodes[i].ID;
                    }
                }
            }
        }
    }