Exemple #1
0
        public static cSet <cLexem> cm_FirstTerminals(List <cLexem> a_listLexem)
        {
            cSet <cLexem> _tempSet = new cSet <cLexem>();

            if (a_listLexem.Count > 0)
            {
                int    _pos       = 0;
                cLexem _currLexem = a_listLexem[_pos];
                do
                {
                    _tempSet.Remove(cLexem.cc_EpsilonLexem);
                    _currLexem = a_listLexem[_pos];
                    _tempSet.UnionWith(_currLexem.cp_FirstCache);
                    _pos++;
                } while (_tempSet.Contains(cLexem.cc_EpsilonLexem) && (_pos < a_listLexem.Count));
            }
            cSet <cLexem> _retSet = new cSet <cLexem>();

            foreach (cLexem _lexem in _tempSet)
            {
                if (_lexem.cp_Type != eLexType.NonTerminal)
                {
                    _retSet.Add(_lexem);
                }
            }
            return(_retSet);
        }
Exemple #2
0
        public object Clone()
        {
            cSet <T> _retSet = new cSet <T>();

            _retSet.UnionWith(this);
            return(_retSet);
        }
Exemple #3
0
        private cSet <cLexem> cm_First(List <cLexem> a_listLexem)
        {
            cSet <cLexem> _retSet = new cSet <cLexem>();

            if (a_listLexem.Count > 0)
            {
                int    _pos       = a_listLexem.Count - 1;
                cLexem _currLexem = a_listLexem[_pos];
                do
                {
                    _retSet.Remove(cLexem.cc_EpsilonLexem);
                    _currLexem = a_listLexem[_pos];
                    _retSet.UnionWith(_currLexem.cp_FirstCache);
                    _pos--;
                } while (_retSet.Contains(cLexem.cc_EpsilonLexem) && (_pos >= 0));
            }
            return(_retSet);
        }
Exemple #4
0
        private Dictionary <cLexem, cSet <cLexem> > cm_Follow()
        {
            Dictionary <cLexem, cSet <cLexem> > _retDic = new Dictionary <cLexem, cSet <cLexem> >();

            foreach (cLexem _nonTerminal in cp_Lexems)
            {
                if (_nonTerminal.cp_Type == eLexType.NonTerminal)
                {
                    _retDic[_nonTerminal] = new cSet <cLexem>();
                }
            }

            _retDic[cf_root].Add(cLexem.cc_StopLexem);

            // 2
            foreach (cLexem _nonTerminal in _retDic.Keys)
            {
                foreach (cProduction _production in _nonTerminal.cp_ListProducts)
                {
                    int           _count          = _production.cp_RightPart.Count - 1;
                    List <cLexem> _revListProduct = new List <cLexem>();
                    for (int i = _count; i >= 0; i--)
                    {
                        cLexem _lex = _production.cp_RightPart[i];
                        switch (_lex.cp_Type)
                        {
                        case eLexType.NonTerminal:
                            cSet <cLexem> _first = cm_First(_revListProduct);
                            _first.Remove(cLexem.cc_EpsilonLexem);
                            _retDic[_lex].AddRange(_first);
                            break;

                        default:
                            break;
                        }
                        _revListProduct.Add(_lex);
                    }
                }
            }

            // 3
            bool _added = true;

            while (_added)
            {
                _added = false;
                foreach (cLexem _nonTerminal in _retDic.Keys)
                {
                    foreach (cProduction _production in _nonTerminal.cp_ListProducts)
                    {
                        int           _count          = _production.cp_RightPart.Count - 1;
                        List <cLexem> _revListProduct = new List <cLexem>();
                        bool          _break          = false;      //?
                        for (int i = _count; i >= 0; i--)
                        {
                            cLexem _lex = _production.cp_RightPart[i];
                            switch (_lex.cp_Type)
                            {
                            case eLexType.NonTerminal:
                                cSet <cLexem> _first = cm_First(_revListProduct);
                                if (_first.Contains(cLexem.cc_EpsilonLexem) | (i == _count))
                                {
                                    //_production.cp_Root.cm_First(_first);
                                    //_first.UnionWith(_production.cp_Root.cp_FirstCache);
                                    _first.Remove(cLexem.cc_EpsilonLexem);
                                    _first.UnionWith(_retDic[_nonTerminal]);
                                }
                                _added |= _retDic[_lex].AddRange(_first);
                                break;

                            case eLexType.Action:
                                break;

                            default:
                                _break = true;
                                break;
                            }
                            _revListProduct.Add(_lex);
                            if (_break)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return(_retDic);
        }