Beispiel #1
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);
            }
        }
Beispiel #2
0
        public override void Extract(Grammar grammar)
        {
            _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);

            //Cloure_GoToTable
            _Production = new CLRProduction(grammar[0].Producer, grammar[0].Product,
                                       new EntityCollection<Terminal>(new[] { (Terminal) "$" }));

            _GotoCount = 0;
            _DotCount = 0;

            _ClosureCol =
                new ClosureCollection(new Closure[] { new CLRClosure(GotoTitle(), grammar, new SLRProduction[] { _Production }) });

            ++_GotoCount;
            _GotoEntries = new GotoEntry[0];
            for (var c = 0; c < _ClosureCol.Count; ++c)
            {
                var closure = (SLRClosure) _ClosureCol[c];
                var tmp = _EntityColO - (Terminal) "$";
                for (var e = 0; e < tmp.Count; ++e) //foreach (var entity in _entityColO - (Terminal) "$")
                {
                    var entity = tmp[e];
                    var gotoClosure = closure.GoToEntity(entity);
                    if (!gotoClosure.IsEmpty)
                    {
                        var index =
                            //_closureCol.List.FindIndex((Closure clr) => (clr == gotoClosure));
                            _ClosureCol.Closures.IndexOf(gotoClosure);
                        if (-1 == index)
                        {
                            // add new Goto State
                            gotoClosure.Title = GotoTitle();
                            _ClosureCol += gotoClosure;
                            ++_GotoCount;
                        }
                        else
                        {
                            // error here
                            //CLRClosure closureC = gotoClosure as CLRClosure;
                            //closureC.AddLookAhead(_ClosureCol[ index ].SLRProductions.ToArray());
                            //gotoClosure = closureC;

                            var closureC = gotoClosure as CLRClosure;
                            if (default(Closure) != closureC) closureC.AddLookAhead(_ClosureCol[index].SLRProductions.ToArray());
                            gotoClosure.Title = _ClosureCol[index].Title;
                        }

                        var length = _GotoEntries.Length;
                        var newGotoEntries = new GotoEntry[length + 1];
                        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 (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
                        _GoTotable[TerminalCol & (Terminal) "$", 1] = "!!";

                    if (production.DotPosition != production.Count) continue;
                    var followCol = grammar.Follow(production.Producer);
                    if (default(EntityCollection<Terminal>) == 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];
            }
        }
Beispiel #3
0
        public override void Extract()
        {
            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 CLRProduction(_grammar[0].Producer, _grammar[0].Product, new EntityCollection<Terminal>(new[] { (Terminal) "$" }));

            _gotoCount = 0;
            _dotCount = 0;

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

            ++_gotoCount;
            _gotoTable = new GotoEntry[0];
            for (int c = 0; c < _closureCol.Count; ++c)
            {
                var closure = (SLRClosure) _closureCol[c];
                var entityColO = _entityColO - (Terminal) "$";
                foreach (var entity in entityColO)
                {
                    var gotoClosure = closure.GoToEntity(entity);
                    if (!gotoClosure.IsNull && gotoClosure.Count != 0)
                    {
                        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
                        {
                            // error here
                            //var closureC = gotoClosure as CLRClosure;
                            //closureC.AddLookAhead(_closureCol[ index ].SLRProductions.ToArray());
                            //gotoClosure = closureC;

                            var closureC = gotoClosure as CLRClosure;
                            closureC.AddLookAhead(_closureCol[index].SLRProductions.ToArray());
                            gotoClosure.Title = _closureCol[index].Title;

                        }
                        var length = _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)
                {
                    SLRProduction 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.Where(gotoEntity => gotoEntity.X == NonTerminalCol[n] && gotoEntity.I == _closureCol[c]))
                    {
                        _tableForm[(terminalLength + 0) + n, c] = gotoEntity.Goto.Title.Split('[', ']')[1];
                    }
                }
            }
        }