Example #1
0
        public override void Extract()
        {
            try
            {
                EntityCol = _grammar.Entities;
                TerminalCol = _grammar.Terminals;
                NonTerminalCol = _grammar.NonTerminals;

                // removing augmented entity
                var augment = EntityCol[0];
                _entityColO = EntityCol - augment;
                NonTerminalCol.Remove((NonTerminal) augment);

                //Cloure_GoToTable
                _prods = new SLRProduction(_grammar[0].Producer, _grammar[0].Product);

                _gotoCount = 0;
                _dotCount = 0;

                _closureCol = new ClosureCollection(new Closure[] { new SLRClosure(GotoTitle(), _grammar, new[] { _prods }) });

                ++_gotoCount;
                for (var c = 0; c < _closureCol.Count; ++c)
                {
                    var closure = (SLRClosure) _closureCol[c];
                    foreach (var entity in _entityColO - (Terminal) "$")
                    {
                        var gotoClosure = closure.GoToEntity(entity);
                        if (!gotoClosure.IsNull)
                        {
                            var index =
                                //_closureCol.List.FindIndex((Closure clr) => (clr == gotoClosure));
                                _closureCol.List.IndexOf(gotoClosure);
                            if (index == -1)
                            {
                                // add new Goto State
                                gotoClosure.Title = GotoTitle();
                                _closureCol += gotoClosure;
                                ++_gotoCount;
                            }
                            else
                            {
                                gotoClosure = _closureCol[index];
                            }

                            var length = (_gotoTable == default(GotoEntry[])) ? 0 : _gotoTable.Length;
                            var newTable = new GotoEntry[length + 1];
                            for (var g = 0; g < length; ++g)
                            {
                                newTable[g] = _gotoTable[g];
                            }
                            newTable[length] = new GotoEntry(closure, entity, gotoClosure);
                            _gotoTable = newTable;
                        }
                        ++_dotCount;
                    }
                }

                // LR Table
                _tableForm = new String[EntityCol.Count, _closureCol.Count];

                for (var c = 0; c < _closureCol.Count; ++c)
                {
                    var terminalLength = TerminalCol.Count;

                    //Shift
                    for (var t = 0; t < terminalLength; ++t)
                    {
                        foreach (var gotoEntity in _gotoTable)
                        {
                            if (gotoEntity.X == TerminalCol[t] && gotoEntity.I == _closureCol[c])
                            {
                                _tableForm[t, c] = "s" + gotoEntity.Goto.Title.Split('[', ']')[1];
                                break;
                            }
                        }
                    }

                    //Reduce
                    for (var p = 0; p < _closureCol[c].Count; ++p)
                    {
                        var production = _closureCol[c][p];
                        if (production.DotPosition == (production.Count - 1)
                        && production == _closureCol[1][0] // S' --> S .$
                          )  // Accepted
                        {
                            _tableForm[TerminalCol & (Terminal) "$", 1] = "!!";
                        }
                        if (production.DotPosition == production.Count)
                        {
                            var followCol = _grammar.Follow(production.Producer);
                            if (followCol == default(EntityCollection<Terminal>)) continue;
                            //followCol.Remove( (Terminal)"$" );
                            foreach (var follow in followCol)
                            {
                                _tableForm[TerminalCol & follow, c] = "r" + (_grammar & production);
                            }
                        }
                    }

                    // Goto
                    for (var n = 0; n < NonTerminalCol.Count; ++n)
                    {
                        foreach (var gotoEntity in _gotoTable)
                        {
                            if (gotoEntity.X == NonTerminalCol[n] && gotoEntity.I == _closureCol[c])
                            {
                                _tableForm[(terminalLength + 0) + n, c] = gotoEntity.Goto.Title.Split('[', ']')[1];
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Example #2
0
        public override void Extract(Grammar grammar)
        {
            try
            {
                _Grammar = grammar;
                EntityCol = grammar.Entities;
                TerminalCol = grammar.Terminals;
                NonTerminalCol = grammar.NonTerminals;

                // removing augmented entity
                var augment = EntityCol[0];
                _EntityColO = EntityCol - augment;

                //_nonterminalCol.Remove((NonTerminal) augment);
                NonTerminalCol = NonTerminalCol - augment;

                //Cloure_GoToTable
                _Production = new SLRProduction(grammar[0].Producer, grammar[0].Product);

                _GotoCount = 0;
                _DotCount = 0;

                ClosureCol = new ClosureCollection(new Closure[] { new SLRClosure(GotoTitle(), grammar, new[] { _Production }) });

                // TODO:: closure production is wrong
                ++_GotoCount;
                _GotoEntries = new GotoEntry[0];
                for (var c = 0; c < ClosureCol.Count; ++c)
                {
                    var closure = (SLRClosure) ClosureCol[c];
                    foreach (var entity in _EntityColO - (Terminal) "$")
                    {
                        var gotoClosure = closure.GoToEntity(entity);
                        if (!gotoClosure.IsEmpty)
                        {
                            var index =
                                //_closureCol.List.FindIndex((Closure clr) => (clr == gotoClosure));
                                ClosureCol.Closures.IndexOf(gotoClosure);
                            if (index == -1)
                            {
                                // add new Goto State
                                gotoClosure.Title = GotoTitle();
                                ClosureCol += gotoClosure;
                                ++_GotoCount;
                            }
                            else gotoClosure = ClosureCol[index];

                            var length = (_GotoEntries == default(GotoEntry[])) ? 0 : _GotoEntries.Length;
                            var newGotoEntries = new GotoEntry[length + 1];

                            //for (var g = 0; g < length; ++g) newGotoTable[g] = _gotoTable[g];
                            Array.Copy(_GotoEntries, newGotoEntries, length);

                            newGotoEntries[length] = new GotoEntry(closure, entity, gotoClosure);
                            _GotoEntries = newGotoEntries;
                        }
                        ++_DotCount;
                    }
                }

                // LR Table
                _GoToTable = new String[EntityCol.Count, ClosureCol.Count];

                for (var c = 0; c < ClosureCol.Count; ++c)
                {
                    var terminalLength = TerminalCol.Count;

                    //Shift
                    for (var t = 0; t < terminalLength; ++t)
                        foreach (var gotoEntity in _GotoEntries)
                        {
                            if (gotoEntity.X != TerminalCol[t] || gotoEntity.I != ClosureCol[c]) continue;

                            _GoToTable[t, c] = "s" + gotoEntity.Goto.Title.Split('[', ']')[1];
                            break;
                        }

                    //Reduce
                    //for (int p = 0; p < _closureCol[c].Count; ++p)
                    foreach (SLRProduction production in ClosureCol[c])
                    {
                        //SLRProduction production = _closureCol[c][p];
                        if (production.DotPosition == (production.Count - 1)
                            && production == ClosureCol[1][0] // S' --> S .$
                            ) // Accepted
                            _GoToTable[TerminalCol & (Terminal) "$", 1] = "!!";

                        if (production.DotPosition != production.Count) continue;

                        var followCol = grammar.Follow(production.Producer);
                        if (default(EntityCollection<Entity>) == followCol) continue;

                        //followCol.Remove( (Terminal)"$" );
                        foreach (var follow in followCol)
                            _GoToTable[TerminalCol & follow, c] = "r" + (grammar & production);
                    }

                    // Goto
                    for (var n = 0; n < NonTerminalCol.Count; ++n)
                        foreach (var gotoEntity in _GotoEntries)
                            if (gotoEntity.X == NonTerminalCol[n] && gotoEntity.I == ClosureCol[c])
                                _GoToTable[(terminalLength + 0) + n, c] = gotoEntity.Goto.Title.Split('[', ']')[1];
                }
            }
            catch (Exception exp)
            {
                Console.Write(exp);
            }
        }
Example #3
0
 public bool Equals(SLRProduction prod)
 {
     return base.Equals(prod) && (_DotPosition == prod.DotPosition);
 }
Example #4
0
 public bool NotEquals(SLRProduction prod)
 {
     return !Equals(prod);
 }
Example #5
0
        public void AddLookAhead(SLRProduction[] prodLALR)
        {
            for (var index = 0; index < Count; ++index)
            {
                var prod1 = this[index] as CLRProduction;
                var prod2 = prodLALR[index] as CLRProduction;

                //foreach (Terminal lookahead in prod2.LookAheads)
                if (default(Production) == prod1) continue;
                if (default(Production) == prod2) continue;

                prod1.LookAheads.Entities.AddRange(prod2.LookAheads);
                prod1 = prod1.LookAheads.RemoveRedundancy() as CLRProduction;
            }
        }
Example #6
0
 public bool Equals(SLRProduction prod)
 {
     return base.Equals(prod) && _positionDot == prod.DotPosition;
 }
Example #7
0
 public int IndexOf(SLRProduction prod, int index = 0)
 {
     //return _productions.FindIndex(index, delegate(SLRProduction production) { return (production == prod); });
     return SLRProductions.IndexOf(prod, index);
 }
Example #8
0
 public void Add(SLRProduction production)
 {
     if (default(Production) != production) SLRProductions.Add(production);
 }
Example #9
0
        public void AddLookAhead(SLRProduction[] prodLALR)
        {
            for (var index = 0; index < Count; ++index)
            {
                var prod1 = this[index] as CLRProduction;
                var prod2 = prodLALR[index] as CLRProduction;

                //foreach (Terminal lookahead in prod2.LookAheads)
                prod1.LookAheads.List.AddRange(prod2.LookAheads);
                prod1 = prod1.LookAheads.RemoveRedundancy() as CLRProduction;
            }
        }
Example #10
0
 public int FindFirstIndex(SLRProduction find, int startIndex)
 {
     return SLRProductions.FindIndex(startIndex, production => (production == find));
 }
Example #11
0
 public void Add(SLRProduction production)
 {
     if (production != default(CLRProduction)) SLRProductions.Add(production);
 }