Exemple #1
0
        private List <GeneratorState> CreateStates(IGrammar grammar)
        {
            List <GeneratorState> states            = new List <GeneratorState>();
            List <GeneratorState> unevaluatedStates = new List <GeneratorState>();
            int stateId = 0;

            // Compute start state.
            //
            {
                HashSet <GeneratorStateItem> items = new HashSet <GeneratorStateItem>();
                foreach (RuleType rule in grammar.StartNonterminal.Rules)
                {
                    items.Add(new GeneratorStateItem(new GeneratorRuleItem(rule, 0)));
                }
                ComputeClosure(grammar, items);

                GeneratorState startState = new GeneratorState(stateId++, items);
                states.Add(startState);
                unevaluatedStates.Add(startState);
            }

            List <LanguageElementType> languageElements = new List <LanguageElementType>();

            languageElements.AddRange(grammar.GetTerminals());
            languageElements.AddRange(grammar.GetNonterminals());

            while (unevaluatedStates.Count > 0)
            {
                // Remove one of the evaluated states and process it.
                //
                GeneratorState state = unevaluatedStates[0];
                unevaluatedStates.RemoveAt(0);

                foreach (LanguageElementType languageElement in languageElements)
                {
                    HashSet <GeneratorStateItem> items = state.Apply(languageElement);
                    if (items != null)
                    {
                        ComputeClosure(grammar, items);

                        GeneratorState toState = null;
                        foreach (GeneratorState existingState in states)
                        {
                            if (existingState.Items.SetEquals(items))
                            {
                                toState = existingState;
                                break;
                            }
                        }
                        if (toState == null)
                        {
                            toState = new GeneratorState(stateId++, items);
                            states.Add(toState);
                            unevaluatedStates.Add(toState);
                        }

                        state.Transitions.Add(languageElement, toState);
                    }
                }
            }

            if (LinguaTrace.TraceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                foreach (GeneratorState state in states)
                {
                    LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_GENERATE_STATE, "{0}", state);
                }
            }

            return(states);
        }
Exemple #2
0
        private List<GeneratorState> CreateStates(IGrammar grammar)
        {
            List<GeneratorState> states = new List<GeneratorState>();
            List<GeneratorState> unevaluatedStates = new List<GeneratorState>();
            int stateId = 0;

            // Compute start state.
            //
            {
                HashSet<GeneratorStateItem> items = new HashSet<GeneratorStateItem>();
                foreach (RuleType rule in grammar.StartNonterminal.Rules)
                {
                    items.Add(new GeneratorStateItem(new GeneratorRuleItem(rule, 0)));
                }
                ComputeClosure(grammar, items);

                GeneratorState startState = new GeneratorState(stateId++, items);
                states.Add(startState);
                unevaluatedStates.Add(startState);
            }

            List<LanguageElementType> languageElements = new List<LanguageElementType>();
            languageElements.AddRange(grammar.GetTerminals());
            languageElements.AddRange(grammar.GetNonterminals());

            while (unevaluatedStates.Count > 0)
            {
                // Remove one of the evaluated states and process it.
                //
                GeneratorState state = unevaluatedStates[0];
                unevaluatedStates.RemoveAt(0);

                foreach (LanguageElementType languageElement in languageElements)
                {
                    HashSet<GeneratorStateItem> items = state.Apply(languageElement);
                    if (items != null)
                    {
                        ComputeClosure(grammar, items);

                        GeneratorState toState = null;
                        foreach (GeneratorState existingState in states)
                        {
                            if (existingState.Items.SetEquals(items))
                            {
                                toState = existingState;
                                break;
                            }
                        }
                        if (toState == null)
                        {
                            toState = new GeneratorState(stateId++, items);
                            states.Add(toState);
                            unevaluatedStates.Add(toState);
                        }

                        state.Transitions.Add(languageElement, toState);
                    }
                }
            }

            if (LinguaTrace.TraceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                foreach (GeneratorState state in states)
                {
                    LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_GENERATE_STATE, "{0}", state);
                }
            }

            return states;
        }