private void LeoMemoization(EarleyItem earleyItem, int setNumber) { if (!Sets[setNumber].IsItemLeoEligible(earleyItem)) { return; } Symbol penult = earleyItem.GetItemPenult(); LeoItem predecessorLeoItem = FindLeoItemPredecessor(earleyItem); if (predecessorLeoItem != null) { Sets[predecessorLeoItem.GetOrignPosition()] .AddLeoItem(new LeoItem( predecessorLeoItem.GetDottedRule(), predecessorLeoItem.GetOrignPosition(), penult )); } else { Sets[earleyItem.GetOrignPosition()] .AddLeoItem(new LeoItem( new DottedRule(earleyItem.GetRule(), earleyItem.GetRulePosition() + 1), earleyItem.GetOrignPosition(), penult )); } }
private void EarleyReducer(EarleyItem completed, int setNumber) { int position = completed.GetOrignPosition(); EarleySet set = Sets[position]; List <EarleyItem> items = set.GetEarleyItemList(); for (int j = 0; j < items.Count; j++) { EarleyItem current = items[j]; Symbol next = current.GetCurrentNextSymbol(); if (next != null && next.Equals(completed.GetRule().GetLeftHandSideOfRule())) { EarleyItem newEarleyItem = new EarleyItem( setNumber, new DottedRule(current.GetRule(), current.GetRulePosition() + 1), current.GetOrignPosition() ); if (current.GetCurrentPrevSymbolList() != null && current.GetCurrentPrevSymbolList().Count > 0) { newEarleyItem.AddPredecessorLink(current, position); } newEarleyItem.AddReducerLink(completed, position); AddToSet(newEarleyItem, setNumber, "EarleyReducer"); } } }
internal void AddEarleyItem(EarleyItem earleyItem, String operationType) { if ( _earleyItemList.Find(erl => erl.GetRule().Equals(earleyItem.GetRule()) && erl.GetRulePosition() == earleyItem.GetRulePosition() && erl.GetOrignPosition() == earleyItem.GetOrignPosition() ) == null ) { _earleyItemList.Add(earleyItem); _earleyItemReportList.Add(new EarleyItemRepresentaion(earleyItem, operationType)); } }
private void Scanner(EarleyItem current, int setNumber, Char inputSymbol) { Symbol nextSymbol = current.GetCurrentNextSymbol(); if (nextSymbol != null && nextSymbol.Equals(inputSymbol.ToString())) { EarleyItem newEarleyItem = new EarleyItem( setNumber + 1, new DottedRule(current.GetRule(), current.GetRulePosition() + 1), current.GetOrignPosition() ); if (newEarleyItem.GetCurrentPrevSymbolList() != null && newEarleyItem.GetCurrentPrevSymbolList().Count > 0) { newEarleyItem.AddPredecessorLink(current, setNumber); } AddToSet(newEarleyItem, setNumber + 1, "Scanner"); } }
private void Predictor(EarleyItem current, int setNumber) { Symbol sym = current.GetCurrentNextSymbol(); List <Rule> filteredRules = Grammar.GetRulesWithSpecificStartSymbol(sym); filteredRules.ForEach((Rule r) => { List <Symbol> symList = r.GetRightHandSideOfRule(); if (symList.Count == 1 && Grammar.CheckIsSymbolANullStringSymbol(symList[0])) { AddToSet(new EarleyItem(setNumber, new DottedRule(current.GetRule(), current.GetRulePosition() + 1), setNumber), setNumber, "Predictor"); } else { EarleyItem ei = new EarleyItem(setNumber, r, setNumber); AddToSet(ei, setNumber, "Predictor"); LeoMemoization(ei, setNumber); } }); }
private void BuildTree(TreeNode parent, EarleyItem current) { current.SetItemIsProcessed(); Symbol lhs = current.GetRule().GetLeftHandSideOfRule(); List <Symbol> prev = current.GetCurrentPrevSymbolList(); int setNumber = current.GetSetNumber(); if (IsEmptyRule(current)) { if (!parent.DoesChildExists(new TreeNode(lhs, setNumber, setNumber))) { parent.AddChild(new TreeNode(_grammar.GetNullStringSymbol(), setNumber, setNumber)); } } else if (prev.Count == 1 && IsLastSymbolBeforeDotTerminal(prev)) { Symbol symBeforeDot = prev[prev.Count - 1]; if (!parent.DoesChildExists(new TreeNode(symBeforeDot, setNumber, setNumber))) { parent.AddChild(new TreeNode(symBeforeDot, setNumber - 1, setNumber)); } } else if (prev.Count == 1 && IsLastSymbolBeforeDotNonTerminal(prev)) { Symbol symBeforeDot = prev[prev.Count - 1]; TreeNode newNode = new TreeNode(symBeforeDot, current.GetOrignPosition(), setNumber); if (!parent.DoesChildExists(newNode)) { parent.AddChild(newNode); } current.GetReducerLinks().ForEach(el => { if (el._label == current.GetOrignPosition() && !el._link.IsItemProcessed()) { BuildTree(newNode, el._link); } }); } else if (IsLastSymbolBeforeDotTerminal(prev)) { Symbol symBeforeDot = prev[prev.Count - 1]; TreeNode newNode = new TreeNode(symBeforeDot, setNumber - 1, setNumber); if (!parent.DoesChildExists(newNode)) { parent.AddChild(newNode); } TreeNode newComplexNode = new TreeNode( new DottedRule(current.GetRule(), current.GetRulePosition() - 1), current.GetOrignPosition(), setNumber - 1 ); if (!parent.DoesChildExists(newComplexNode)) { parent.AddChild(newComplexNode); } current.GetPredecessorLinks().ForEach(link => { if (link._label == setNumber - 1 && !link._link.IsItemProcessed()) { BuildTree(newComplexNode, link._link); } }); } else if (IsLastSymbolBeforeDotNonTerminal(prev)) { Symbol symBeforeDot = prev[prev.Count - 1]; current.GetReducerLinks().ForEach(item => { TreeNode newNode = new TreeNode(symBeforeDot, item._label, setNumber); if (!parent.DoesChildExists(newNode)) { parent.AddChild(newNode); } if (!item._link.IsItemProcessed()) { BuildTree(newNode, item._link); } TreeNode newComplexNode = new TreeNode( new DottedRule(current.GetRule(), current.GetRulePosition() - 1), current.GetOrignPosition(), item._label ); if (!parent.DoesChildExists(newComplexNode)) { parent.AddChild(newComplexNode); } current.GetPredecessorLinks().ForEach(predItem => { if (!predItem._link.IsItemProcessed()) { BuildTree(newComplexNode, predItem._link); } }); }); } ; }