private void cm_doA2() { cLexem _lexem = cf_lisTokens[cf_lisTokens.Count - 2].cf_Value as cLexem; cf_leftLex.cm_AddChildLexem(_lexem as cLexem, true); cf_firstLex = _lexem; }
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); }
public cProduction(cLexem a_root) { cf_root = a_root; cf_rightPart = new List <cLexem>(); cf_actionList = new List <cLexem>(); cf_epsilonProduct = false; }
//private void cm_showGram(cParserException _ex) //{ // f_rtbGram.Document.Blocks.Clear(); // foreach (cToken _token in _ex.cf_Tokens) // { // switch (_token.cf_Type) // { // case eTokenType.перевод_строки: // f_rtbGram.AppendText(Environment.NewLine); // break; // case eTokenType.стрелка: // f_rtbGram.AppendText(" -> "); // break; // case eTokenType.Null: // f_rtbGram.AppendText(" -> "); // break; // default: // cm_addLexemToRichText(_token.cf_Value as cLexem, f_rtbGram); // break; // } // } //} private void cm_addLexemToRichText(cLexem a_lexem, RichTextBox a_rtb) { if (a_lexem.cp_Type == eLexType.NonTerminal) { a_rtb.AppendText("<"); } else if (a_lexem.cp_Type == eLexType.Action) { a_rtb.AppendText("{"); } //a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Bold); //a_rtb.AppendText(a_lexem.ToString()); TextRange _tr = new TextRange(a_rtb.Document.ContentEnd, a_rtb.Document.ContentEnd); _tr.Text = a_lexem.ToString(); _tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); _tr = new TextRange(a_rtb.Document.ContentEnd, a_rtb.Document.ContentEnd); _tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular); //a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Regular); if (a_lexem.cp_Type == eLexType.NonTerminal) { a_rtb.AppendText(">"); } else if (a_lexem.cp_Type == eLexType.Action) { a_rtb.AppendText("}"); } a_rtb.AppendText(" "); }
private object cm_getAction() { cLexem _retLex = cLexem.cm_GetLexem(cf_name); _retLex.cp_Type = eLexType.Action; return(_retLex); }
public cProduction(cLexem a_root) { cf_root = a_root; cf_rightPart = new List<cLexem>(); cf_actionList = new List<cLexem>(); cf_epsilonProduct = false; }
public void cm_AddAction(cLexem a_action) { if (a_action.cp_Type != eLexType.Action) { throw new Exception("Попытка добавить к продукции лексемы " + cf_root.cf_Name + " в качестве действия лексему типа " + a_action.cp_Type.ToString()); } cf_actionList.Add(a_action); }
public static cLexem cm_ExtendGrammatic(cLexem a_root) { cLexem _newRoot = cLexem.cm_GetLexem(" E'"); _newRoot.cf_type = eLexType.NonTerminal; _newRoot.cm_AddChildLexem(a_root, true); //_newRoot.cm_AddChildLexem(cLexem.cc_StopLexem, false); return(_newRoot); }
private void cm_doA1() { cf_leftLex = cf_lisTokens[cf_lisTokens.Count - 3].cf_Value as cLexem; cf_leftLex.cp_Type = eLexType.NonTerminal; if (cf_root == null) { cf_root = cf_leftLex; } }
public void cm_AddAction(cLexem a_action) { if (a_action.cp_Type != eLexType.Action) { throw new Exception("Попытка добавить к лексеме " + cf_Name + " в качестве действия лексему типа " + cp_Type.ToString()); } cProduction _production = cf_listProducts[cf_listProducts.Count - 1]; _production.cm_AddAction(a_action); }
private void cm_doA2() { cLexem _lexem = cf_lisTokens[cf_lisTokens.Count - 2].cf_Value as cLexem; if (!cf_leftLex.cm_AddChildLexem(null, _lexem as cLexem)) { throw new cNotLL1Exception(_lexem, cf_leftLex, "Несколько продукций для " + cf_leftLex + " имеют направляющий символ " + _lexem); } cf_firstLex = _lexem; }
void cm_checkProducts(cProductInfo[] a_data) { int _count = a_data.Length; Dictionary <cLexem, cLexem> _dic = new Dictionary <cLexem, cLexem>(); for (int k = 0; k < _count; k++) { cProductInfo _info = a_data[k]; KeyValuePair <cLexem, int>[] _arrLexems = _info.cp_ArrLexems; int _lexCount = _arrLexems.Length; bool _clearDic = true; for (int j = 0; j < _lexCount; j++) { if (_clearDic) { _dic.Clear(); } _clearDic = true; cLexem _lexem = _arrLexems[j].Key; switch (_lexem.cp_Type) { case eLexType.Terminal: if (_dic.ContainsKey(_lexem)) { throw new cNotLL1Exception(cLexem.cc_EpsilonLexem, cLexem.cc_EpsilonLexem, "Невозможно однозначно определить продукцию для символа " + _lexem.ToString() + " правой части " + _info.cp_Root.Key.ToString() + " из-за " + cLexem.cc_Epsilon + "-порождающих нетерминалов."); } _dic.Add(_lexem, _lexem); break; case eLexType.NonTerminal: foreach (cLexem _lex in _lexem.cp_LeadLexems) { if (_lex.cp_Type == eLexType.Terminal) { if (_dic.ContainsKey(_lex)) { throw new cNotLL1Exception(cLexem.cc_EpsilonLexem, cLexem.cc_EpsilonLexem, "Невозможно однозначно определить продукцию для символа " + _lex.ToString() + " правой части " + _info.cp_Root.Key.ToString() + " из-за " + cLexem.cc_Epsilon + "-порождающих нетерминалов."); } _dic.Add(_lex, _lex); } else if (_lex.cp_Type == eLexType.Epsilon) { _clearDic = false; } } break; case eLexType.Epsilon: _clearDic = true; break; } } } }
public static cProductInfo[] cm_getProductsByRoot(cLexem a_root, ref int a_Counter) { List<cProductInfo> _retLst = new List<cProductInfo>(); int _counterRoots = a_Counter; a_Counter += a_root.cp_ListProducts.Count; if (a_root.cp_ListProducts.ContainsKey(cLexem.cc_EpsilonLexem)) a_Counter--; foreach (KeyValuePair<cLexem, List<cLexem>> _kvp in a_root.cp_ListProducts) { // В список не добавляются пустые продукции if (_kvp.Key.cp_Type == eLexType.Epsilon) continue; cProductInfo _newPro = new cProductInfo(); _newPro.cf_root = new KeyValuePair<cLexem,int>(a_root, _counterRoots++); _newPro.cf_lstLexems = new List<KeyValuePair<cLexem, int>>(); int i = _kvp.Value.Count; foreach (cLexem _lex in _kvp.Value) { _newPro.cf_lstLexems.Add(new KeyValuePair<cLexem, int>(_lex, a_Counter++)); // Для последнего нетерминала if (--i == 0) { bool _flag = false; if (_lex.cp_Type == eLexType.NonTerminal) { foreach (cLexem _leadLex in _lex.cp_LeadLexems) if (_leadLex.cp_Type == eLexType.Epsilon) { _flag = true; break; } } if (_flag) { a_Counter++; } } } if (_kvp.Key.cp_Type == eLexType.NonTerminal) _newPro.cf_arrDirLexems = _newPro.cf_root.Key.cm_GetLeadLexems(_kvp.Key); else _newPro.cf_arrDirLexems = new cLexem[]{ _kvp.Key }; _retLst.Add(_newPro); } return _retLst.ToArray(); }
public bool cm_IsLeadingLexem(cLexem a_lexem) { if (this.cp_LeadLexems != null) { foreach (cLexem _lex in this.cp_LeadLexems) { if (_lex == a_lexem) { return(true); } } } return(false); }
public void cm_Add(cLexem a_lexem) { if ((cf_rightPart.Count == 0) & (a_lexem == cLexem.cc_EpsilonLexem)) { cf_root.cf_EpsilonCount++; cf_epsilonProduct = true; } if (cf_epsilonProduct & (a_lexem != cLexem.cc_EpsilonLexem)) { cf_epsilonProduct = false; cf_root.cf_EpsilonCount--; } cf_rightPart.Add(a_lexem); }
private cToken cm_readToken() { cToken _retToken = new cToken(eTokenType.Null, null); cf_reader.MoveToContent(); if (cf_reader.IsStartElement(cc_product)) { cf_reader.ReadStartElement(cc_product); cf_reader.MoveToContent(); cf_buffer.Push(new cToken(eTokenType.стрелка, null)); cLexem _lex = cLexem.cm_GetLexem(cf_reader.Value.Trim()); _retToken = new cToken(eTokenType.лексема, _lex); cf_reader.Skip(); } else if (cf_reader.IsStartElement(cc_epsilon)) { cf_reader.Skip(); _retToken = new cToken(eTokenType.пустая_лексема, cLexem.cc_EpsilonLexem); } else if (cf_reader.IsStartElement(cc_lexem)) { XmlReader _subTree = cf_reader.ReadSubtree(); _subTree.MoveToContent(); _subTree.ReadStartElement(cc_lexem); _subTree.MoveToContent(); _retToken = new cToken(eTokenType.лексема, cLexem.cm_GetLexem(_subTree.Value)); _subTree.Close(); cf_reader.Skip(); } else if (cf_reader.IsStartElement(cc_action)) { XmlReader _subTree = cf_reader.ReadSubtree(); _subTree.MoveToContent(); _subTree.ReadStartElement(cc_action); _subTree.MoveToContent(); cLexem _lex = cLexem.cm_GetLexem(cf_reader.Value); _lex.cp_Type = eLexType.Action; _retToken = new cToken(eTokenType.действие, _lex); _subTree.Close(); cf_reader.Skip(); } else if (cf_reader.NodeType == XmlNodeType.EndElement && cf_reader.Name == cc_product) { _retToken = new cToken(eTokenType.перевод_строки, null); cf_reader.ReadEndElement(); } return(_retToken); }
private void cm_addLexemToRichText(cLexem a_lexem, RichTextBox a_rtb) { if (a_lexem.cp_Type == eLexType.NonTerminal) a_rtb.AppendText("<"); else if (a_lexem.cp_Type == eLexType.Action) a_rtb.AppendText("{"); a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Bold); a_rtb.AppendText(a_lexem.ToString()); a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Regular); if (a_lexem.cp_Type == eLexType.NonTerminal) a_rtb.AppendText(">"); else if (a_lexem.cp_Type == eLexType.Action) a_rtb.AppendText("}"); a_rtb.AppendText(" "); }
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); }
public void cm_AddChildLexem(cLexem a_lexem, bool a_newProduct) { if (a_lexem.cp_Type == eLexType.Action) { throw new Exception("Попытка добавить к продукции лексемы " + cf_Name + " лексему типа " + a_lexem.cp_Type.ToString()); } if (a_newProduct) { cProduction _production = new cProduction(this); _production.cm_Add(a_lexem); cf_listProducts.Add(_production); } else { cProduction _production = cf_listProducts[cf_listProducts.Count - 1]; _production.cm_Add(a_lexem); } }
public cLexem[] cm_GetLeadLexems(cLexem a_productFirstLex) { List <cLexem> _watchedLexems = new List <cLexem>(); _watchedLexems.Add(this); List <cLexem> _tempLst = new List <cLexem>(); cm_fillNonTerminalLeadingLexList(_watchedLexems, cf_listProducts[a_productFirstLex], 0, _tempLst); List <cLexem> _retLst = new List <cLexem>(); foreach (cLexem _lex in _tempLst) { if (_lex.cp_Type == eLexType.Terminal || _lex.cp_Type == eLexType.Epsilon) { _retLst.Add(_lex); } } return(_retLst.ToArray()); }
public bool cm_AddChildLexem(cLexem a_key, cLexem a_nextLex) { if (a_key == null) { if (cf_listProducts.ContainsKey(a_nextLex)) { return(false); } else { cf_listProducts.Add(a_nextLex, new List <cLexem>(new cLexem[] { a_nextLex })); } } else { cf_listProducts[a_key].Add(a_nextLex); } return(true); }
public static cLexem cm_GetLexem(string a_name) { if (a_name == cc_Epsilon) { return(cc_EpsilonLexem); } cLexem _retLex = null; if (cf_LexemDic.ContainsKey(a_name)) { _retLex = cf_LexemDic[a_name]; } else { _retLex = new cLexem(a_name, eLexType.Terminal); cf_LexemDic.Add(a_name, _retLex); } return(_retLex); }
public static cSet <cConfiguration> cm_Closure(cSet <cConfiguration> a_listConfiguration) { bool _added = false; if (a_listConfiguration != null) { cSet <cConfiguration> _retList = a_listConfiguration.Clone() as cSet <cConfiguration>; cSet <cConfiguration> _addRetList = new cSet <cConfiguration>(); do { _addRetList.Clear(); foreach (cConfiguration _configuration in _retList) { if (_configuration.cf_Production.cp_RightPart.Count > _configuration.cf_Position) { cLexem _lexem = _configuration.cf_Production.cp_RightPart[_configuration.cf_Position]; if (_lexem.cp_Type == eLexType.NonTerminal) { List <cLexem> _listTerminals = new List <cLexem>(); _listTerminals.AddRange(_configuration.cf_Production.cp_RightPart.GetRange(_configuration.cf_Position + 1, _configuration.cf_Production.cp_RightPart.Count - _configuration.cf_Position - 1)); _listTerminals.Add(_configuration.cf_Terminal); cSet <cLexem> _firstTerminals = cm_FirstTerminals(_listTerminals); foreach (cProduction _production in _lexem.cp_ListProducts) { foreach (cLexem _terminal in _firstTerminals) { cConfiguration _newConfiguration = cConfiguration.cm_GetConfiguration(_production, 0, _terminal); _addRetList.Add(_newConfiguration); } } } } } _added = _retList.AddRange(_addRetList); } while (_added == true); return(_retList); } else { return(new cSet <cConfiguration>()); } }
public void cm_Init(cParser a_parser) { cf_valid = false; cf_root = cLexem.cm_ExtendGrammatic(a_parser.cp_Root); // Заполнение множества FOLLOW cf_follow = cm_Follow(); //cLexem _lexem = cLexem.cm_GetLexem("Es"); //cLexem _lexem2 = cLexem.cm_GetLexem("E"); //cLexem _lexem3 = cLexem.cm_GetLexem("+"); //cSet<cConfiguration> _test = cConfiguration.cm_Goto(new cSet<cConfiguration>() { cConfiguration.cm_GetConfiguration(_lexem.cp_ListProducts[0], 1), cConfiguration.cm_GetConfiguration(_lexem2.cp_ListProducts[0], 1) }, _lexem3); //int i; //i = 1; KeyValuePair <List <cSet <cConfiguration> >, Dictionary <cSet <cConfiguration>, Dictionary <cLexem, cSet <cConfiguration> > > > _pair = cm_Items(cf_root); cf_listItems = _pair.Key; cf_gotoResults = _pair.Value; cf_valid = true; }
private void cm_addLexemToRichText(cLexem a_lexem, RichTextBox a_rtb) { if (a_lexem.cp_Type == eLexType.NonTerminal) { a_rtb.AppendText("<"); } else if (a_lexem.cp_Type == eLexType.Action) { a_rtb.AppendText("{"); } a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Bold); a_rtb.AppendText(a_lexem.ToString()); a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Regular); if (a_lexem.cp_Type == eLexType.NonTerminal) { a_rtb.AppendText(">"); } else if (a_lexem.cp_Type == eLexType.Action) { a_rtb.AppendText("}"); } a_rtb.AppendText(" "); }
public cNotLL1Exception(cLexem a_lexem, cLexem a_leftLexem, string a_customInfo) : base("Грамматика не является LL(1).\n" + a_customInfo) { cf_Lexem = a_lexem; cf_LeftLexem = a_leftLexem; }
private void cm_doA4() { cLexem _lexem = cf_lisTokens[cf_lisTokens.Count - 2].cf_Value as cLexem; cf_leftLex.cm_AddChildLexem(_lexem as cLexem, false); }
private bool cm_addItemGroup(List<cSet<cConfiguration>> a_items, cSet<cConfiguration> a_item, cLexem a_lexem, cSet<cConfiguration> a_set, Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>> a_goto) { bool _notSubset = true; foreach (cSet<cConfiguration> _set in a_items) { if (_set.IsSupersetOf(a_set)) { _notSubset = false; cm_addToGoto(a_goto, a_item, a_lexem, _set); break; } } if (_notSubset) { a_items.Add(a_set); cm_addToGoto(a_goto, a_item, a_lexem, a_set); } return _notSubset; }
private bool cm_addToGoto(Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>> a_goto, cSet<cConfiguration> a_item, cLexem a_lexem, cSet<cConfiguration> a_gotoItem) { bool _retVal; if (!a_goto.ContainsKey(a_item)) { a_goto.Add(a_item, new Dictionary<cLexem, cSet<cConfiguration>>()); } if (!a_goto[a_item].ContainsKey(a_lexem)) { a_goto[a_item].Add(a_lexem, a_gotoItem); _retVal = true; } else { _retVal = false; throw new Exception("Попытка заменить значение в дереве Goto при построении списка пунктов"); } return _retVal; }
public static cProductInfo[] cm_getProductsByRoot(cLexem a_root, ref int a_Counter) { List <cProductInfo> _retLst = new List <cProductInfo>(); int _counterRoots = a_Counter; a_Counter += a_root.cp_ListProducts.Count; if (a_root.cp_ListProducts.ContainsKey(cLexem.cc_EpsilonLexem)) { a_Counter--; } foreach (KeyValuePair <cLexem, List <cLexem> > _kvp in a_root.cp_ListProducts) { // В список не добавляются пустые продукции if (_kvp.Key.cp_Type == eLexType.Epsilon) { continue; } cProductInfo _newPro = new cProductInfo(); _newPro.cf_root = new KeyValuePair <cLexem, int>(a_root, _counterRoots++); _newPro.cf_lstLexems = new List <KeyValuePair <cLexem, int> >(); int i = _kvp.Value.Count; foreach (cLexem _lex in _kvp.Value) { _newPro.cf_lstLexems.Add(new KeyValuePair <cLexem, int>(_lex, a_Counter++)); // Для последнего нетерминала if (--i == 0) { bool _flag = false; if (_lex.cp_Type == eLexType.NonTerminal) { foreach (cLexem _leadLex in _lex.cp_LeadLexems) { if (_leadLex.cp_Type == eLexType.Epsilon) { _flag = true; break; } } } if (_flag) { a_Counter++; } } } if (_kvp.Key.cp_Type == eLexType.NonTerminal) { _newPro.cf_arrDirLexems = _newPro.cf_root.Key.cm_GetLeadLexems(_kvp.Key); } else { _newPro.cf_arrDirLexems = new cLexem[] { _kvp.Key } }; _retLst.Add(_newPro); } return(_retLst.ToArray()); }
private void cm_doA2() { cLexem _lexem = cf_lisTokens[cf_lisTokens.Count - 2].cf_Value as cLexem; if (!cf_leftLex.cm_AddChildLexem(null, _lexem as cLexem)) throw new cNotLL1Exception(_lexem, cf_leftLex, "Несколько продукций для " + cf_leftLex + " имеют направляющий символ " + _lexem); cf_firstLex = _lexem; }
public static cLexem cm_ExtendGrammatic(cLexem a_root) { cLexem _newRoot = cLexem.cm_GetLexem(" E'"); _newRoot.cf_type = eLexType.NonTerminal; _newRoot.cm_AddChildLexem(a_root, true); //_newRoot.cm_AddChildLexem(cLexem.cc_StopLexem, false); return _newRoot; }
private cConfiguration(cProduction a_production, int a_position, cLexem a_terminal) { cf_Production = a_production; cf_Position = a_position; cf_Terminal = a_terminal; }
public static cSet <cConfiguration> cm_Goto(cSet <cConfiguration> a_listConfiguration, cLexem a_lexem) { cSet <cConfiguration> _validConfigurations = new cSet <cConfiguration>(); foreach (cConfiguration _configuration in a_listConfiguration) { if (_configuration.cf_Production.cp_RightPart.Count > _configuration.cf_Position) { if (_configuration.cf_Production.cp_RightPart[_configuration.cf_Position] == a_lexem) { cConfiguration _newConfiguration = cConfiguration.cm_GetConfiguration(_configuration.cf_Production, _configuration.cf_Position + 1, _configuration.cf_Terminal); _validConfigurations.Add(_newConfiguration); } } } return(cm_Closure(_validConfigurations)); }
private void cm_doA5() { cLexem _lexem = cf_lisTokens[cf_lisTokens.Count - 2].cf_Value as cLexem; cf_leftLex.cm_AddChildLexem(cf_firstLex, _lexem as cLexem); }
public static cConfiguration cm_GetConfiguration(cProduction a_production, int a_position, cLexem a_terminal) { if (!cf_configurationsCache.ContainsKey(a_production)) { cf_configurationsCache.Add(a_production, new Dictionary<int,Dictionary<cLexem,cConfiguration>>()); } if (!cf_configurationsCache[a_production].ContainsKey(a_position)) { cf_configurationsCache[a_production].Add(a_position,new Dictionary<cLexem, cConfiguration>()); } if (!cf_configurationsCache[a_production][a_position].ContainsKey(a_terminal)) { if (a_production.cp_EpsilonProduct & (a_position != 0)) { throw new Exception("Попытка создать некорректную конфигурацию из эпсилон-продукции"); } cf_configurationsCache[a_production][a_position].Add(a_terminal, new cConfiguration(a_production, a_position, a_terminal)); } return cf_configurationsCache[a_production][a_position][a_terminal]; }
public static cSet<cConfiguration> cm_Goto(cSet<cConfiguration> a_listConfiguration, cLexem a_lexem) { cSet<cConfiguration> _validConfigurations = new cSet<cConfiguration>(); foreach (cConfiguration _configuration in a_listConfiguration) { if (_configuration.cf_Production.cp_RightPart.Count > _configuration.cf_Position) { if (_configuration.cf_Production.cp_RightPart[_configuration.cf_Position] == a_lexem) { cConfiguration _newConfiguration = cConfiguration.cm_GetConfiguration(_configuration.cf_Production, _configuration.cf_Position + 1, _configuration.cf_Terminal); _validConfigurations.Add(_newConfiguration); } } } return cm_Closure(_validConfigurations); }
public bool cm_IsLeadingLexem(cLexem a_lexem) { if (this.cp_LeadLexems != null) { foreach (cLexem _lex in this.cp_LeadLexems) if (_lex == a_lexem) return true; } return false; }
public static cLexem cm_GetLexem(string a_name) { if (a_name == cc_Epsilon) return cc_EpsilonLexem; cLexem _retLex = null; if (cf_LexemDic.ContainsKey(a_name)) _retLex = cf_LexemDic[a_name]; else { _retLex = new cLexem(a_name, eLexType.Terminal); cf_LexemDic.Add(a_name, _retLex); } return _retLex; }
public static cConfiguration cm_GetConfiguration(cProduction a_production, int a_position, cLexem a_terminal) { if (!cf_configurationsCache.ContainsKey(a_production)) { cf_configurationsCache.Add(a_production, new Dictionary <int, Dictionary <cLexem, cConfiguration> >()); } if (!cf_configurationsCache[a_production].ContainsKey(a_position)) { cf_configurationsCache[a_production].Add(a_position, new Dictionary <cLexem, cConfiguration>()); } if (!cf_configurationsCache[a_production][a_position].ContainsKey(a_terminal)) { if (a_production.cp_EpsilonProduct & (a_position != 0)) { throw new Exception("Попытка создать некорректную конфигурацию из эпсилон-продукции"); } cf_configurationsCache[a_production][a_position].Add(a_terminal, new cConfiguration(a_production, a_position, a_terminal)); } return(cf_configurationsCache[a_production][a_position][a_terminal]); }
private void cm_doA1() { cf_leftLex = cf_lisTokens[cf_lisTokens.Count - 3].cf_Value as cLexem; cf_leftLex.cp_Type = eLexType.NonTerminal; if (cf_root == null) cf_root = cf_leftLex; }
private KeyValuePair<List<cSet<cConfiguration>>, Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>>> cm_Items(cLexem a_root) { List<cSet<cConfiguration>> _retList = new List<cSet<cConfiguration>>(); cConfiguration _rootConfiguration=cConfiguration.cm_GetConfiguration(a_root.cp_ListProducts[0], 0, cLexem.cc_StopLexem); cSet<cConfiguration> _rootClosure = cConfiguration.cm_Closure(new cSet<cConfiguration>() { _rootConfiguration }); Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>> _gotoResults = new Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>>(); _retList.Add(_rootClosure); bool _added=false; int _lastChecked=0; do { _added = false; for (; _lastChecked < _retList.Count; _lastChecked++) { cSet<cConfiguration> _currentItem = _retList[_lastChecked]; cSet<cLexem> _currentChecked = cm_getAllPossibleGotoLexems(_currentItem); foreach (cLexem _lexem in _currentChecked) { cSet<cConfiguration> _possibleItem = cConfiguration.cm_Goto(_currentItem, _lexem); if (cm_addItemGroup(_retList, _currentItem, _lexem, _possibleItem, _gotoResults)) { _added = true; } } } } while (_added); return new KeyValuePair<List<cSet<cConfiguration>>, Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>>>(_retList, _gotoResults); }
//private void cm_showGram(cParserException _ex) //{ // f_rtbGram.Document.Blocks.Clear(); // foreach (cToken _token in _ex.cf_Tokens) // { // switch (_token.cf_Type) // { // case eTokenType.перевод_строки: // f_rtbGram.AppendText(Environment.NewLine); // break; // case eTokenType.стрелка: // f_rtbGram.AppendText(" -> "); // break; // case eTokenType.Null: // f_rtbGram.AppendText(" -> "); // break; // default: // cm_addLexemToRichText(_token.cf_Value as cLexem, f_rtbGram); // break; // } // } //} private void cm_addLexemToRichText(cLexem a_lexem, RichTextBox a_rtb) { if (a_lexem.cp_Type == eLexType.NonTerminal) a_rtb.AppendText("<"); else if (a_lexem.cp_Type == eLexType.Action) a_rtb.AppendText("{"); //a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Bold); //a_rtb.AppendText(a_lexem.ToString()); TextRange _tr = new TextRange(a_rtb.Document.ContentEnd,a_rtb.Document.ContentEnd); _tr.Text = a_lexem.ToString(); _tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); _tr = new TextRange(a_rtb.Document.ContentEnd, a_rtb.Document.ContentEnd); _tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular); //a_rtb.SelectionFont = new Font(a_rtb.SelectionFont, FontStyle.Regular); if (a_lexem.cp_Type == eLexType.NonTerminal) a_rtb.AppendText(">"); else if (a_lexem.cp_Type == eLexType.Action) a_rtb.AppendText("}"); a_rtb.AppendText(" "); }
public void cm_Init(cParser a_parser) { cf_valid = false; cf_root = a_parser.cp_Root; // Заполнение множества FOLLOW cf_follow = new Dictionary<cLexem,cSet<cLexem>>(); foreach (cLexem _nonTerminal in cp_Lexems) { if (_nonTerminal.cp_Type == eLexType.NonTerminal) { cf_follow[_nonTerminal] = new cSet<cLexem>(); } } // 2 cSet<cLexem> _first = new cSet<cLexem>(); foreach (cLexem _nonTerminal in cp_Lexems) { if (_nonTerminal.cp_Type == eLexType.NonTerminal) { foreach (List<cLexem> _lstLex in _nonTerminal.cp_ListProducts.Values) { int _count = _lstLex.Count; _first.Clear(); for (int i = _lstLex.Count - 1; i >= 0; i--) { cLexem _lex = _lstLex[i]; switch (_lex.cp_Type) { case eLexType.NonTerminal: cf_follow[_lex].AddRange(_first); if (!_lex.cm_IsLeadingLexem(cLexem.cc_EpsilonLexem)) { _first.Clear(); } _first.AddRange(_lex.cp_LeadLexems); break; case eLexType.Terminal: _first.Clear(); _first.Add(_lex); break; case eLexType.Epsilon: _first.Clear(); break; } } } } } // 3 bool _added = true; while (_added) { _added = false; foreach (cLexem _nonTerminal in cp_Lexems) { if (_nonTerminal.cp_Type == eLexType.NonTerminal) { foreach (List<cLexem> _lstLex in _nonTerminal.cp_ListProducts.Values) { int _count = _lstLex.Count; _first.Clear(); bool _break = false; for (int i = _lstLex.Count - 1; i >= 0; i--) { cLexem _lex = _lstLex[i]; switch (_lex.cp_Type) { case eLexType.NonTerminal: cf_follow[_lex].AddRange(_first); if (!_lex.cm_IsLeadingLexem(cLexem.cc_EpsilonLexem)) { _break = true; } //else _added = cf_follow[_lex].AddRange(cf_follow[_nonTerminal]) | _added; break; case eLexType.Action: break; default: _break = true; break; } if (_break) break; } } } } } cf_valid = true; }
public bool cm_AddChildLexem(cLexem a_key, cLexem a_nextLex) { if (a_key == null) { if (cf_listProducts.ContainsKey(a_nextLex)) return false; else cf_listProducts.Add(a_nextLex, new List<cLexem>(new cLexem[] { a_nextLex })); } else { cf_listProducts[a_key].Add(a_nextLex); } return true; }
public void cm_Init(cParser a_parser) { cf_valid = false; cf_root = cLexem.cm_ExtendGrammatic(a_parser.cp_Root); // Заполнение множества FOLLOW cf_follow = cm_Follow(); //cLexem _lexem = cLexem.cm_GetLexem("Es"); //cLexem _lexem2 = cLexem.cm_GetLexem("E"); //cLexem _lexem3 = cLexem.cm_GetLexem("+"); //cSet<cConfiguration> _test = cConfiguration.cm_Goto(new cSet<cConfiguration>() { cConfiguration.cm_GetConfiguration(_lexem.cp_ListProducts[0], 1), cConfiguration.cm_GetConfiguration(_lexem2.cp_ListProducts[0], 1) }, _lexem3); //int i; //i = 1; KeyValuePair<List<cSet<cConfiguration>>, Dictionary<cSet<cConfiguration>, Dictionary<cLexem, cSet<cConfiguration>>>> _pair = cm_Items(cf_root); cf_listItems = _pair.Key; cf_gotoResults = _pair.Value; cf_valid = true; }
public cLexem[] cm_GetLeadLexems(cLexem a_productFirstLex) { List<cLexem> _watchedLexems = new List<cLexem>(); _watchedLexems.Add(this); List<cLexem> _tempLst = new List<cLexem>(); cm_fillNonTerminalLeadingLexList(_watchedLexems, cf_listProducts[a_productFirstLex], 0, _tempLst); List<cLexem> _retLst = new List<cLexem>(); foreach (cLexem _lex in _tempLst) if (_lex.cp_Type == eLexType.Terminal || _lex.cp_Type == eLexType.Epsilon) _retLst.Add(_lex); return _retLst.ToArray(); }
private void cm_doA5() { cLexem _lexem = cf_lisTokens[cf_lisTokens.Count - 2].cf_Value as cLexem; cf_leftLex.cm_AddAction(_lexem as cLexem); }