Beispiel #1
0
        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();
        }
Beispiel #2
0
        private static int IndexOf(Grammar g, LRxItem item)
        {
            ExpressInt express = item.express;

#if DEBUG
            int    debug     = item.produce.ToIndex();
            string debug_str = Grammar.bag[item.produce];
#endif
            Range rng = g.m_nonterminals[item.produce.ToIndex()];
            int   st  = rng.start;
            int   ed  = rng.end;
            do
            {
                if (g.m_items[st].express.Equals(express))
                {
                    return(st);
                }
            } while (++st < ed);

            return(-1);
        }
Beispiel #3
0
        private Map <TableItem>[] CreateATB(List <LALRState> state)
        {
            Grammar g = m_grammar;

            int cnt   = state.Count;
            int stnon = g.startIdx.ToNonterminal();

            Map <TableItem>[] res = new Map <TableItem> [cnt];
            int x = 0;

            for (; x < cnt; x++)
            {
                Map <TableItem> item = new Map <TableItem>();

                IEnumerator <KeyValuePair <int, LALRState> > e1 = state[x].map.GetEnumerator();
                while (e1.MoveNext())
                {
                    if (e1.Current.Key.IsTerminal())
                    {
                        item.Add(e1.Current.Key, new TableItem(act_SHIFT, IndexOf(state, e1.Current.Value)));
                    }
                }

                e1.Dispose();
                res[x] = item;
            }

            for (x = 0; x < cnt; x++)
            {
                IEnumerator <LRxItem> e1  = state[x].items.GetEnumerator();
                Map <TableItem>       ctb = res[x];

                while (e1.MoveNext())
                {
                    LRxItem cit = e1.Current;
                    if (cit.IsEnd)
                    {
                        if (cit.produce == stnon)
                        {
                            ctb.Add(ExpressInt.end_symbol, new TableItem(act_ACCEPT, 0));
                        }
                        else
                        {
                            int       index  = IndexOf(g, cit);
                            TableItem reduce = new TableItem(act_REDUCE, index);

                            IEnumerator <int> e2 = cit.lookahead.GetEnumerator();
                            while (e2.MoveNext())
                            {
                                if (ctb.Contains(e2.Current))
                                {
#if DEBUG
                                    LALRState current_state = state[x];
                                    TokenType debug_tok     = (TokenType)e2.Current;
#endif
                                    m_handler.Fail($"SHIFT-REDUCE CONFILCT -> state: {x}");
                                }
                                else
                                {
                                    ctb.Add(e2.Current, reduce);
                                }
                            }

                            e2.Dispose();
                        }
                    }
                }

                e1.Dispose();
            }

            return(res);
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
 public LALRTableView(LRxItem item) => this.item = item;
 public LALRState(LRxItem first) => items = new LinkedHashSet <LRxItem>