Ejemplo n.º 1
0
        //tenhle overload se volá po posledním výpočtu lookaheadů, kde jsme ochotni tolerovat i shift/reduce
        //konflikty, kde budeme preferovat shift; proto tento overload zjišťuje, zda se v daném automatu
        //nacházejí i reduce/reduce konflikty, se kterými už bychom si neuměli rozumně poradit
        private bool checkForConflicts(LookaheadComplexity lastStage, out bool reduceReduceConflictsInAutomaton)
        {
            bool conflictsInAutomaton = false;

            reduceReduceConflictsInAutomaton = false;

            foreach (State state in parserStates)
            {
                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                {
                    BitVectorSet accumulator       = new BitVectorSet(grammar.NumTerminals);
                    BitVectorSet reduceAccumulator = new BitVectorSet(grammar.NumTerminals);

                    foreach (Transition trans in state.Transitions)
                    {
                        if (trans is TerminalTransition)
                        {
                            accumulator.Add(trans.TransitionSymbol);
                        }
                    }

                    bool conflictsInThisState = false;

                    foreach (BitVectorSet lookaheadSet in lookaheadSets[stateLookaheadIndex[state.StateNumber]])
                    {
                        if (!reduceAccumulator.IsDisjointWith(lookaheadSet))
                        {
                            conflictsInThisState             = true;
                            reduceReduceConflictsInAutomaton = true;
                            break;
                        }
                        else
                        {
                            if (!accumulator.IsDisjointWith(lookaheadSet))
                            {
                                conflictsInThisState = true;
                            }

                            reduceAccumulator.UnionWith(lookaheadSet);
                        }
                    }

                    if (conflictsInThisState)
                    {
                        conflictsInAutomaton = true;
                    }
                    else
                    {
                        numInconsistentStates--;
                        stateResolvedAt[state.StateNumber] = lastStage;
                    }
                }
            }

            return(conflictsInAutomaton);
        }
Ejemplo n.º 2
0
        //projde všechny dosud nevyřešené stavy a podívá se, jestli byla minulá fáze výpočtu dostatečná
        //na to, aby rozřešila konflikty, které se v nich objevují; parametr lastStage popisuje, jak vypadala
        //poslední fáze výpočtu a můžeme tak pro diagnostické důvody sledovat, které stavy jsou vyřešeny
        //v kterém stádiu výpočtu
        private bool checkForConflicts(LookaheadComplexity lastStage)
        {
            bool conflictsInAutomaton = false;

            foreach (State state in parserStates)
            {
                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                {
                    BitVectorSet accumulator = new BitVectorSet(grammar.NumTerminals);

                    //inicializujeme akumulační proměnnou na množinu terminálů, které v daném stavu můžeme přečíst
                    foreach (Transition trans in state.Transitions)
                    {
                        if (trans is TerminalTransition)
                        {
                            accumulator.Add(trans.TransitionSymbol);
                        }
                    }

                    bool conflictsInThisState = false;

                    //akumulační proměnná prochází kolem všech lookahead množin a testuje se s nimi na disjunktnost,
                    //sama potom přebírá jejich prvky (nemusíme tak testovat každou dvojici množin)
                    foreach (BitVectorSet lookaheadSet in lookaheadSets[stateLookaheadIndex[state.StateNumber]])
                    {
                        if (!accumulator.IsDisjointWith(lookaheadSet))
                        {
                            conflictsInThisState = true;
                            break;
                        }
                        else
                        {
                            accumulator.UnionWith(lookaheadSet);
                        }
                    }

                    if (conflictsInThisState)
                    {
                        conflictsInAutomaton = true;
                    }
                    else
                    {
                        numInconsistentStates--;
                        stateResolvedAt[state.StateNumber] = lastStage;
                    }
                }
            }

            return(conflictsInAutomaton);
        }
Ejemplo n.º 3
0
        private void printSymbolExplanations(State state, Item finalItem, BitVectorSet conflictSymbols, TextWriter writer)
        {
            //Vyrazíme ze všech vrcholů (vrcholy jsou tady neterminální hrany automatu)
            //ležících v lookback(state, finalItem), které mají ve follow množině nějaký konfliktní symbol.
            //Prohledáváním do hloubkyv grafu relace 'includes' najdeme nejbližší vrcholy, které mají v Read
            //množinách dohromady všechny hledané konfliktní symboly. Posléze vyrazíme z těchto nalezených vrcholů,
            //tentokrát po hranách relace 'reads' a budeme hledat nejbližší vrcholy, které mají v Direct Read množinách
            //dohromady všechny hledané symboly. Z posledně nalezených vrcholů už jsme vždy schopni vystopovat
            //cestu zpět přes reads, includes a lookback hrany až k původnímu konfliktnímu itemu.

            //pole předků a značky u navštívených vrcholů pro oba grafy
            NonterminalTransition[] includesPredecessors = new NonterminalTransition[numNonterminalTransitions];
            bool[] includesExplored = new bool[numNonterminalTransitions];
            NonterminalTransition[] readsPredecessors = new NonterminalTransition[numNonterminalTransitions];
            bool[] readsExplored = new bool[numNonterminalTransitions];

            BitVectorSet symbolsLeftToExplain = new BitVectorSet(conflictSymbols);

            //BFS fronty pro oba průchody obou grafů
            Queue <NonterminalTransition> includesTransitions = new Queue <NonterminalTransition>();
            Queue <NonterminalTransition> readsTransitions    = new Queue <NonterminalTransition>();

            //vrcholy, které mají v DR množinách konfliktní symboly
            List <NonterminalTransition> rootTransitions = new List <NonterminalTransition>();

            //procházení po lookback hranách
            foreach (NonterminalTransition trans in lookback(state, finalItem))
            {
                if (!follow[getTransNumber(trans)].IsDisjointWith(conflictSymbols))
                {
                    includesTransitions.Enqueue(trans);
                    includesExplored[getTransNumber(trans)] = true;
                }
            }

            //průchod přes includes hrany
            while ((includesTransitions.Count > 0) && (!symbolsLeftToExplain.IsEmpty()))
            {
                NonterminalTransition trans = includesTransitions.Dequeue();

                BitVectorSet readSet = read[getTransNumber(trans)];
                if (!readSet.IsDisjointWith(symbolsLeftToExplain))
                {
                    BitVectorSet symbolsJustExplained = readSet.GetIntersectionWith(symbolsLeftToExplain);
                    symbolsLeftToExplain -= symbolsJustExplained;
                    readsTransitions.Enqueue(trans);
                    readsExplored[getTransNumber(trans)] = true;
                }

                foreach (NonterminalTransition next in includes.GetNeighboursFor(trans))
                {
                    if (!includesExplored[getTransNumber(next)])
                    {
                        includesPredecessors[getTransNumber(next)] = trans;
                        includesExplored[getTransNumber(next)]     = true;
                        includesTransitions.Enqueue(next);
                    }
                }
            }

            //reset hledaných symbolů a průchod přes reads hrany
            symbolsLeftToExplain = new BitVectorSet(conflictSymbols);

            while ((readsTransitions.Count > 0) && (!symbolsLeftToExplain.IsEmpty()))
            {
                NonterminalTransition trans = readsTransitions.Dequeue();

                BitVectorSet DRSet = initDR(trans);
                if (!DRSet.IsDisjointWith(symbolsLeftToExplain))
                {
                    BitVectorSet symbolsJustExplained = DRSet.GetIntersectionWith(symbolsLeftToExplain);
                    symbolsLeftToExplain -= symbolsJustExplained;
                    rootTransitions.Add(trans);
                }

                foreach (NonterminalTransition next in reads.GetNeighboursFor(trans))
                {
                    if (!readsExplored[getTransNumber(next)])
                    {
                        readsPredecessors[getTransNumber(next)] = trans;
                        readsExplored[getTransNumber(next)]     = true;
                        readsTransitions.Enqueue(next);
                    }
                }
            }

            //teď už jen vystopujeme všechny potřebné cesty z neterminální hrany, která obsahovala konfliktní
            //symbol ve své DR množině, až k itemu, kde tímto symbolem přispěla a způsobila konflikt
            foreach (NonterminalTransition root in rootTransitions)
            {
                Stack <NonterminalTransition> readsPath    = new Stack <NonterminalTransition>();
                Stack <NonterminalTransition> includesPath = new Stack <NonterminalTransition>();

                NonterminalTransition trans = root;
                while (readsPredecessors[getTransNumber(trans)] != null)
                {
                    readsPath.Push(trans);
                    trans = readsPredecessors[getTransNumber(trans)];
                }

                while (includesPredecessors[getTransNumber(trans)] != null)
                {
                    includesPath.Push(trans);
                    trans = includesPredecessors[getTransNumber(trans)];
                }



                writer.Write("({0}, ", state.StateNumber);
                printItemHTML(finalItem, conflictSymbols, writer);
                writer.Write(") <b><i>lookback</i></b> (<a href=\"#State{0}\">{0}</a>, {1})", trans.Source.StateNumber,
                             getSymbolPrintName(trans.TransitionSymbol));

                foreach (NonterminalTransition transition in includesPath)
                {
                    writer.Write(" <b><i>includes</i></b> (<a href=\"#State{0}\">{0}</a>, {1})", transition.Source.StateNumber,
                                 getSymbolPrintName(transition.TransitionSymbol));
                }

                foreach (NonterminalTransition transition in readsPath)
                {
                    writer.Write(" <b><i>reads</i></b> (<a href=\"#State{0}\">{0}</a>, {1})", transition.Source.StateNumber,
                                 getSymbolPrintName(transition.TransitionSymbol));
                }

                writer.Write(" and {");

                StringBuilder explainedSymbolsString = new StringBuilder();
                foreach (int explainedSymbol in initDR(root).GetIntersectionWith(conflictSymbols))
                {
                    explainedSymbolsString.AppendFormat("<span style=\"color:red\">{0}</span>, ", getSymbolPrintName(explainedSymbol));
                }
                explainedSymbolsString.Remove(explainedSymbolsString.Length - 2, 2);

                writer.Write(explainedSymbolsString.ToString());
                writer.Write("} ⊂ <b>DR</b>(" + root.Source.StateNumber.ToString() + ", " + getSymbolPrintName(root.TransitionSymbol) + ")");

                writer.WriteLine("<br>");
            }
        }
Ejemplo n.º 4
0
        private void printStatesHTML(TextWriter writer)
        {
            writer.WriteLine("<h2>Parser states</h2>");

            if (numInconsistentStates > 0)
            {
                writer.Write("<b>States with inconsistencies:</b> ");

                StringBuilder inconsistentStatesString = new StringBuilder();
                foreach (State state in parserStates)
                {
                    if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                    {
                        inconsistentStatesString.AppendFormat("<a href=\"#State{0}\">{0}</a>, ", state.StateNumber);
                    }
                }
                inconsistentStatesString.Remove(inconsistentStatesString.Length - 2, 2);

                writer.WriteLine(inconsistentStatesString.ToString());
            }

            foreach (State state in parserStates)
            {
                writer.Write("<a name=\"State{0}\" id=\"State{0}\"/>", state.StateNumber);
                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                {
                    writer.WriteLine("<h3 style=\"color:red\">State {0}</h3>", state.StateNumber);
                }
                else
                {
                    writer.WriteLine("<h3>State {0}</h3>", state.StateNumber);
                }

                writer.WriteLine("<b>Items:</b>");
                writer.WriteLine("<ul>");

                BitVectorSet conflictSymbols = new BitVectorSet(grammar.NumTerminals);

                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                {
                    //najde symboly, pro které je tento stav nedeterministický, úpravou algoritmu na hledání
                    //konfliktů použitém v metodě checkForConflicts
                    BitVectorSet accumulator = new BitVectorSet(grammar.NumTerminals);

                    foreach (Transition trans in state.Transitions)
                    {
                        if (trans is TerminalTransition)
                        {
                            accumulator.Add(trans.TransitionSymbol);
                        }
                    }

                    foreach (BitVectorSet lookaheadSet in lookaheadSets[stateLookaheadIndex[state.StateNumber]])
                    {
                        BitVectorSet newConflictSymbols = accumulator.GetIntersectionWith(lookaheadSet);
                        accumulator.UnionWith(lookaheadSet);
                        conflictSymbols.UnionWith(newConflictSymbols);
                    }
                }

                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.LR0)
                {
                    //žádný lookahead
                    foreach (Item item in state.ItemSet)
                    {
                        writer.Write("<li>");
                        printItemHTML(item, conflictSymbols, writer);
                        writer.WriteLine("</li>");
                    }
                }
                else
                {
                    //nejdřív vypíšeme finální itemy a jejich lookahead množiny; pro každý konfliktní symbol
                    //v lookahead množině navíc vypíšeme cestu, jakou se do lookahead množiny dostal
                    for (int i = 0; i < conflictingItems[stateLookaheadIndex[state.StateNumber]].Count; i++)
                    {
                        writer.Write("<li>");
                        printItemHTML(conflictingItems[stateLookaheadIndex[state.StateNumber]][i], conflictSymbols, writer);

                        writer.WriteLine("<br>");
                        printLookahead(lookaheadSets[stateLookaheadIndex[state.StateNumber]][i], conflictSymbols, writer);

                        if (!conflictSymbols.IsDisjointWith(lookaheadSets[stateLookaheadIndex[state.StateNumber]][i]))
                        {
                            writer.WriteLine("<br>");
                            printSymbolExplanations(state, conflictingItems[stateLookaheadIndex[state.StateNumber]][i],
                                                    conflictSymbols.GetIntersectionWith(lookaheadSets[stateLookaheadIndex[state.StateNumber]][i]), writer);
                        }

                        writer.WriteLine("</li>");
                    }

                    //pak vypíšeme zbylé itemy
                    foreach (Item item in state.ItemSet)
                    {
                        if (!item.IsFinal)
                        {
                            writer.Write("<li>");
                            printItemHTML(item, conflictSymbols, writer);
                            writer.WriteLine("</li>");
                        }
                    }
                }

                writer.WriteLine("</ul>");

                writer.Write("<b>Accessing states:</b> ");

                StringBuilder accessingStatesString = new StringBuilder();
                foreach (State accessingState in state.AccessingStates)
                {
                    accessingStatesString.AppendFormat("<a href=\"#State{0}\">{0}</a>, ", accessingState.StateNumber);
                }
                if (accessingStatesString.Length > 0)
                {
                    accessingStatesString.Remove(accessingStatesString.Length - 2, 2);
                }

                writer.Write(accessingStatesString.ToString());
                writer.WriteLine("<br>");

                writer.Write("<b>Transitions:</b> ");

                StringBuilder transitionsString = new StringBuilder();
                foreach (Transition trans in state.Transitions)
                {
                    transitionsString.AppendFormat("<a href=\"#State{0}\">{0}</a>({1}), ",
                                                   trans.Destination.StateNumber, getSymbolPrintName(trans.TransitionSymbol));
                }
                if (transitionsString.Length > 0)
                {
                    transitionsString.Remove(transitionsString.Length - 2, 2);
                }

                writer.Write(transitionsString.ToString());
                writer.WriteLine("<br>");
            }
        }
Ejemplo n.º 5
0
        private void printStatesHTML(TextWriter writer)
        {
            writer.WriteLine("<h2>Parser states</h2>");

            if (numInconsistentStates > 0)
            {
                writer.Write("<b>States with inconsistencies:</b> ");

                StringBuilder inconsistentStatesString = new StringBuilder();
                foreach (State state in parserStates)
                    if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                        inconsistentStatesString.AppendFormat("<a href=\"#State{0}\">{0}</a>, ", state.StateNumber);
                inconsistentStatesString.Remove(inconsistentStatesString.Length - 2, 2);

                writer.WriteLine(inconsistentStatesString.ToString());
            }

            foreach (State state in parserStates)
            {
                writer.Write("<a name=\"State{0}\" id=\"State{0}\"/>", state.StateNumber);
                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                    writer.WriteLine("<h3 style=\"color:red\">State {0}</h3>", state.StateNumber);
                else
                    writer.WriteLine("<h3>State {0}</h3>", state.StateNumber);

                writer.WriteLine("<b>Items:</b>");
                writer.WriteLine("<ul>");

                BitVectorSet conflictSymbols = new BitVectorSet(grammar.NumTerminals);

                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.Unresolved)
                {
                    //najde symboly, pro které je tento stav nedeterministický, úpravou algoritmu na hledání
                    //konfliktů použitém v metodě checkForConflicts
                    BitVectorSet accumulator = new BitVectorSet(grammar.NumTerminals);

                    foreach (Transition trans in state.Transitions)
                        if (trans is TerminalTransition)
                            accumulator.Add(trans.TransitionSymbol);

                    foreach (BitVectorSet lookaheadSet in lookaheadSets[stateLookaheadIndex[state.StateNumber]])
                    {
                        BitVectorSet newConflictSymbols = accumulator.GetIntersectionWith(lookaheadSet);
                        accumulator.UnionWith(lookaheadSet);
                        conflictSymbols.UnionWith(newConflictSymbols);
                    }
                }

                if (stateResolvedAt[state.StateNumber] == LookaheadComplexity.LR0)
                {
                    //žádný lookahead
                    foreach (Item item in state.ItemSet)
                    {
                        writer.Write("<li>");
                        printItemHTML(item, conflictSymbols, writer);
                        writer.WriteLine("</li>");
                    }
                }
                else
                {
                    //nejdřív vypíšeme finální itemy a jejich lookahead množiny; pro každý konfliktní symbol
                    //v lookahead množině navíc vypíšeme cestu, jakou se do lookahead množiny dostal
                    for (int i = 0; i < conflictingItems[stateLookaheadIndex[state.StateNumber]].Count; i++)
                    {
                        writer.Write("<li>");
                        printItemHTML(conflictingItems[stateLookaheadIndex[state.StateNumber]][i], conflictSymbols, writer);

                        writer.WriteLine("<br>");
                        printLookahead(lookaheadSets[stateLookaheadIndex[state.StateNumber]][i], conflictSymbols, writer);

                        if (!conflictSymbols.IsDisjointWith(lookaheadSets[stateLookaheadIndex[state.StateNumber]][i]))
                        {
                            writer.WriteLine("<br>");
                            printSymbolExplanations(state, conflictingItems[stateLookaheadIndex[state.StateNumber]][i],
                                conflictSymbols.GetIntersectionWith(lookaheadSets[stateLookaheadIndex[state.StateNumber]][i]), writer);
                        }

                        writer.WriteLine("</li>");
                    }

                    //pak vypíšeme zbylé itemy
                    foreach (Item item in state.ItemSet)
                        if (!item.IsFinal)
                        {
                            writer.Write("<li>");
                            printItemHTML(item, conflictSymbols, writer);
                            writer.WriteLine("</li>");
                        }
                }

                writer.WriteLine("</ul>");

                writer.Write("<b>Accessing states:</b> ");

                StringBuilder accessingStatesString = new StringBuilder();
                foreach (State accessingState in state.AccessingStates)
                    accessingStatesString.AppendFormat("<a href=\"#State{0}\">{0}</a>, ", accessingState.StateNumber);
                if (accessingStatesString.Length > 0)
                    accessingStatesString.Remove(accessingStatesString.Length - 2, 2);

                writer.Write(accessingStatesString.ToString());
                writer.WriteLine("<br>");

                writer.Write("<b>Transitions:</b> ");

                StringBuilder transitionsString = new StringBuilder();
                foreach (Transition trans in state.Transitions)
                    transitionsString.AppendFormat("<a href=\"#State{0}\">{0}</a>({1}), ",
                                    trans.Destination.StateNumber, getSymbolPrintName(trans.TransitionSymbol));
                if (transitionsString.Length > 0)
                    transitionsString.Remove(transitionsString.Length - 2, 2);

                writer.Write(transitionsString.ToString());
                writer.WriteLine("<br>");
            }
        }