void Closure(ProjectSet set) { Queue <Project> q = new Queue <Project>(); foreach (var item in set.projects) { q.Enqueue(item); } while (q.Count() != 0) { Project item = q.Dequeue(); if (!item.isReduce() && item.RightSymbol() < 0) { var edges = grammer.GetEdgesOf(item.RightSymbol()); foreach (var edge in edges) { Project newProject = new Project(edge); if (set.Contains(newProject)) { continue; } set.Add(newProject); q.Enqueue(newProject); } } } }
private void MakeRule() { ProjectSet initSet = new ProjectSet(); foreach (var edge in grammer.GetEdgesOf(-1)) { initSet.Add(new Project(edge)); } Closure(initSet); MainSet.Add(initSet); //构造一个队列做BFS,MainSet用来做簇的去重,next指向下一个簇的引用 Queue <ProjectSet> q = new Queue <ProjectSet>(); q.Enqueue(initSet); while (q.Count() != 0) { ProjectSet nowState = q.Dequeue(); Dictionary <int, List <Project> > classification = new Dictionary <int, List <Project> >(); foreach (var project in nowState.projects) { if (project.isReduce()) { foreach (var followItem in grammer.GetFollow(project.GetFrom())) { if (project.GetFrom() == -1) { AddSheetItem(nowState.id, followItem, new Accept()); } else { AddSheetItem(nowState.id, followItem, new Reduce(project.GetEdge())); } } } else { if (!classification.ContainsKey(project.RightSymbol())) { classification[project.RightSymbol()] = new List <Project>(); } classification[project.RightSymbol()].Add(project.NextProject()); } } foreach (var item in classification) { var newSet = new ProjectSet(); foreach (var project in item.Value) { newSet.Add(project); } Closure(newSet); var findItem = FindSetByObject(newSet); if (findItem != null) { newSet = findItem; ProjectSet.SubCount(); } else { MainSet.Add(newSet); q.Enqueue(newSet); } AddSheetItem(nowState.id, item.Key, new Shift(newSet.id)); } } }