Exemple #1
0
        private void CFGReductionPhase1(CFG cfg)
        {
            //phase 1
            HashSet <CFGVariable> allNeededTransitions = new HashSet <CFGVariable>();

            //create first list containing terminal symbols
            foreach (KeyValuePair <string, CFGVariable> valuePair in cfg.AllTransitions)
            {
                if (valuePair.Value.ContainsTerminals())
                {
                    allNeededTransitions.Add(valuePair.Value);
                }
            }

            //create hashset of variables that can reach a terminal
            int transitionLength = 0;

            while (transitionLength != allNeededTransitions.Count())
            {
                transitionLength = allNeededTransitions.Count();
                foreach (KeyValuePair <string, CFGVariable> valuePair in cfg.AllTransitions)
                {
                    foreach (List <ILetterOrVariable> transition in valuePair.Value.ToVariablesOrLetters)
                    {
                        int counter = 0;
                        foreach (ILetterOrVariable letOrVar in transition)
                        {
                            if (allNeededTransitions.Contains(letOrVar))
                            {
                                counter++;
                            }
                        }
                        if (counter == transition.Count)
                        {
                            allNeededTransitions.Add(valuePair.Value);
                        }
                    }
                }
            }

            cfg.PruneNotIncludedVariables(allNeededTransitions);
        }