Exemple #1
0
            static bool _SymEq(_LRExtendedSymbol x, _LRExtendedSymbol y)
            {
                if (ReferenceEquals(x, y))
                {
                    return(true);
                }
                else if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
                {
                    return(false);
                }
                var lhs = x;

                if (ReferenceEquals(lhs, null))
                {
                    return(false);
                }
                var rhs = y;

                if (ReferenceEquals(rhs, null))
                {
                    return(false);
                }

                return(Equals(lhs.Id, rhs.Id));
            }
Exemple #2
0
        CfgDocument _LRFAToLRExtendedGrammar(_LRFA lrfa, IProgress <CfgLRProgress> progress)
        {
            var result   = new CfgDocument();
            var closure  = new List <_LRFA>();
            var itemSets = new List <_LR0ItemSet>();

            lrfa.FillClosure(closure);
            foreach (var p in closure)
            {
                itemSets.Add(p.Accept);
            }

            _LRExtendedSymbol start = null;
            int j = 0;

            foreach (var p in closure)
            {
                if (null != progress)
                {
                    progress.Report(new CfgLRProgress(CfgLRStatus.CreatingLRExtendedGrammar, j));
                }

                int si = itemSets.IndexOf(p.Accept);

                foreach (var item in p.Accept.Items)
                {
                    if (0 == item.RightIndex)
                    {
                        var next = item.Next;

                        if (!item.IsEnd || item.IsNil)
                        {
                            int   dst = -1;
                            _LRFA dsts;
                            if (p.Transitions.ContainsKey(item.Left))
                            {
                                dsts = p.Transitions[item.Left];
                                dst  = itemSets.IndexOf(dsts.Accept);
                            }

                            _LRExtendedSymbol left = new _LRExtendedSymbol(si, item.Left, dst);
                            if (null == start)
                            {
                                start = left;
                            }
                            var right = new List <string>();
                            var pc    = p;
                            foreach (var sym in item.Right)
                            {
                                int s1 = itemSets.IndexOf(pc.Accept);
                                var pt = pc.Transitions[sym];
                                int s2 = itemSets.IndexOf(pt.Accept);
                                _LRExtendedSymbol n = new _LRExtendedSymbol(s1, sym, s2);
                                right.Add(n.ToString());
                                pc = pt;
                            }

                            result.Rules.Add(new CfgRule(left.ToString(), right));
                        }
                    }
                }
                ++j;
            }
            result.StartSymbol = start.ToString();
            return(result);
        }