示例#1
0
        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
                                ));
            }
        }
示例#2
0
        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");
                }
            }
        }
示例#3
0
        private bool LeoReducer(EarleyItem completed, int setNumber)
        {
            int       position       = completed.GetOrignPosition();
            Symbol    lhs            = completed.GetRule().GetLeftHandSideOfRule();
            EarleySet set            = Sets[position];
            LeoItem   transitiveItem = set.FindLeoItemBySymbol(lhs);

            if (transitiveItem == null)
            {
                return(false);
            }

            EarleyItem newEarleyItem = new EarleyItem(
                setNumber,
                transitiveItem.GetDottedRule(),
                transitiveItem.GetOrignPosition()
                );

            EarleyItem current = Sets[transitiveItem.GetOrignPosition()].GetEarleyItemList()
                                 .Find(el => el.GetDottedRule().Equals(transitiveItem.GetDottedRule()) && el.GetOrignPosition() == transitiveItem.GetOrignPosition());

            if (current.GetCurrentPrevSymbolList() != null && current.GetCurrentPrevSymbolList().Count > 0)
            {
                newEarleyItem.AddPredecessorLink(current, position);
            }

            newEarleyItem.AddReducerLink(completed, position);

            AddToSet(
                newEarleyItem,
                setNumber,
                "LeoReducer"
                );
            return(true);
        }
 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));
     }
 }
示例#5
0
        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");
            }
        }
示例#6
0
        private LeoItem FindLeoItemPredecessor(EarleyItem earleyItem)
        {
            EarleySet predecessorSet = Sets[earleyItem.GetOrignPosition()];

            return(predecessorSet.FindLeoItemBySymbol(earleyItem.GetRule().GetLeftHandSideOfRule()));
        }
        public override bool Equals(object obj)
        {
            EarleyItem earleyItem = obj as EarleyItem;

            return(GetDottedRule().Equals(earleyItem.GetDottedRule()) && GetOrignPosition() == earleyItem.GetOrignPosition());
        }
示例#8
0
        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);
                        }
                    });
                });
            }
            ;
        }
示例#9
0
 public LeoItem(EarleyItem ei, Symbol symbol)
 {
     _symbol        = symbol;
     _dotteRule     = ei.GetDottedRule();
     _orignPosition = ei.GetOrignPosition();
 }