public static void Closure(CachedGrammar inst, LinkedHashSet <LRxItem> items) { Grammar g = inst.m_value; IForwardIterator <LRxItem> e1 = items.Begin(); IForwardIterator <LRxItem> e2 = items.Begin(); for (; e1.IsAlive; e1.Add()) { LRxItem current = e1.Value; if (current.IsEnd) { continue; } int ms = current.MarkSymbol; if (!ms.IsTerminal()) { CHashSet <int> la = current.IsLast ? new CHashSet <int>(current.lookahead) : inst.GetFirst(current.express, current.index + 1); IForwardIterator <GItem> e3 = g.Find(ms); do { LRxItem newit = new LRxItem(e3.Value, la); for (; e2.IsAlive; e2.Add()) { if (newit.CoreEquals(e2.Value)) { CHashSet <int> ela = e2.Value.lookahead; if (!ela.IsSupersetOf(la)) { ela.UnionWith(la); } goto exit; } } items.Add(newit); exit: e2.Reset(); } while (e3.LazyAdd()); e3.Dispose(); } } e1.Dispose(); }
protected override bool OnParsePrepare() { if (!readyToParse) { CachedGrammar g = new CachedGrammar(m_grammar); g.ParallelCaching(threadLimit); List <LALRState> states = CreateLALRState(g); gtb = CreateGOTO(states); atb = CreateATB(states); m_cachedG = g; readyToParse = true; } return(true); }
protected override void Dispose(bool disposing) { if (m_cachedG != null) { if (leaveOpen) { m_cachedG.m_value = null; } m_cachedG.Dispose(); m_cachedG = null; } int x; if (gtb != null) { x = gtb.Length; while (--x >= 0) { gtb[x].Dispose(); gtb[x] = null; } gtb = null; } if (atb != null) { x = atb.Length; while (--x >= 0) { atb[x].Dispose(); atb[x] = null; } atb = null; } threadLimit = 0; readyToParse = false; base.Dispose(disposing); }
private List <LALRState> CreateLALRState(CachedGrammar cg) { List <LALRState> clr = CreateCLRState(cg); List <LALRState> lalr = new List <LALRState>(); int cnt = clr.Count; for (int x = 0; x < cnt; x++) { LALRState current_state = clr[x]; for (int z = x + 1; z < cnt;) { LinkedHashSet <LRxItem> xlist = current_state.items; LinkedHashSet <LRxItem> zlist = clr[z].items; IEqualityComparer <LRxItem> save = xlist.Comparer; xlist.Comparer = LRxItemComparerSlim.Instance; zlist.Comparer = LRxItemComparerSlim.Instance; if (xlist.SetEquals(zlist)) { IEnumerator <LRxItem> e1 = xlist.GetEnumerator(); while (e1.MoveNext()) { LRxItem now = e1.Current; IEnumerator <LRxItem> e2 = zlist.GetEnumerator(); while (e2.MoveNext()) { if (now.CoreEquals(e2.Current)) { now.lookahead.UnionWith(e2.Current.lookahead); break; } } e2.Dispose(); } e1.Dispose(); xlist.Comparer = save; zlist.Comparer = save; for (int a = 0; a < cnt; a++) { Map <LALRState> map = clr[a].map; KeyValuePair <int, LALRState>[] kvs = map.ToArray(); for (int b = 0, bm = kvs.Length; b < bm; b++) { if (kvs[b].Value.items.SetEquals(zlist)) { map.Add(kvs[b].Key, current_state); } } } clr.RemoveAt(z); cnt--; } else { xlist.Comparer = save; zlist.Comparer = save; z++; } } lalr.Add(current_state); } clr.Clear(); return(lalr); }
private List <LALRState> CreateCLRState(CachedGrammar cg) { LALRState now = new LALRState(new LRxItem(cg.m_value.StartItem, new CHashSet <int> { ExpressInt.end_symbol })); LR1Helper.Closure(cg, now.items); List <LALRState> list = new List <LALRState> { now }; for (int x = 0; x < list.Count; x++) { now = list[x]; IForwardIterator <LRxItem> iter = now.items.Begin(); IForwardIterator <LRxItem> e1 = now.items.Begin(); do { if (iter.Value.IsEnd) { continue; } LinkedHashSet <LRxItem> nextit = new LinkedHashSet <LRxItem>(); int ms = iter.Value.MarkSymbol; do { if (e1.Value.MarkSymbol == ms) { nextit.Add(e1.Value.GetNext()); } } while (e1.LazyAdd()); e1.Reset(); LR1Helper.Closure(cg, nextit); bool exist = false; for (int z = 0; z < list.Count; z++) { if (list[z].items.SetEquals(nextit)) { exist = true; now.map.Add(ms, list[z]); } } if (!exist) { LALRState next = new LALRState(nextit); list.Add(next); now.map.Add(ms, next); } } while (iter.LazyAdd()); iter.Dispose(); e1.Dispose(); } return(list); }