Пример #1
0
        private List <VertexTerminator> getFirst(VertexNonterminal vn)
        {
            if (this.first.ContainsKey(vn))
            {
                return(this.first[vn]);
            }

            List <VertexTerminator>   ret          = new List <VertexTerminator>();
            IEnumerable <GrammerRule> rulesAboutVn = this.GrammerRuleSet.Where(x => x.Left == vn);

            foreach (GrammerRule rule in rulesAboutVn)
            {
                var firstv = rule.Right[0];
                if (firstv.GetType() == typeof(VertexNonterminal))
                {
                    if (firstv != vn)
                    {
                        ret.AddRange(getFirst((VertexNonterminal)firstv));
                    }
                }
                else
                {
                    if (!ret.Contains(firstv))
                    {
                        ret.Add((VertexTerminator)firstv);
                    }
                }
            }
            if (!this.first.ContainsKey(vn))
            {
                this.first.Add(vn, ret);
            }
            return(ret);
        }
Пример #2
0
        /// <summary>
        /// 计算闭包,宽搜,在this.family.Register中被调用
        /// </summary>
        public void CalcClosure()
        {
            Queue <Item> queue = new Queue <Item>();

            //所有圆点后为非终结符的项目入队
            foreach (Item coreItem in this.items.Where(c => c.IsCore))
            {
                if (coreItem.AfterDot(0) != null && coreItem.AfterDot(0).GetType() == typeof(VertexNonterminal))
                {
                    queue.Enqueue(coreItem);
                }
            }
            while (queue.Count > 0) //宽搜
            {
                Item itemjToExpand = queue.Dequeue();
                VertexNonterminal         vnToExpand        = (VertexNonterminal)itemjToExpand.AfterDot(0);
                IEnumerable <GrammerRule> rulesForExpanding = this.family.Grammer.GrammerRuleSet.Where(c => c.Left == vnToExpand);

                foreach (GrammerRule r in rulesForExpanding)
                {
                    // 生成可能添加的project
                    Item projExpand = new Item()
                    {
                        Rule         = r,
                        DotPosition  = 0,
                        FowardSearch = GetFowardSearch(itemjToExpand, this.family.Grammer),
                        IsCore       = false
                    };
                    Boolean projExist = false;
                    // 检查项目是否存在
                    foreach (Item projCurrent in this.items)
                    {
                        if (projCurrent.IsSameItemWith(projExpand))
                        {
                            // 存在则合并向前搜索符
                            projCurrent.CombineFowardSearch(projExpand);
                            projExist = true;
                            break;
                        }
                    }
                    if (!projExist)
                    {
                        // 不存在则添加,如果是非终结符开始,还需加到队列里
                        this.items.Add(projExpand);
                        if (projExpand.AfterDot(0).GetType() == typeof(VertexNonterminal))
                        {
                            queue.Enqueue(projExpand);
                        }
                    }
                }
            }
        }
Пример #3
0
        private void btnStartAnalysis_Click(object sender, EventArgs e)
        {
            if (analyzer == null)
            {
                MessageBox.Show("请先分析文法!", "无法测试", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            listViewAnalysisProcess.Items.Clear();
            analyzer.CombineBySameCore();
            analyzer.OnAnalysisProcess = OnAnalysisProcess;

            VertexNonterminal grammerTree = null;

            try
            {
                grammerTree = analyzer.Test(txtTest.Text);
            }
            catch (TestFailedException ex)
            {
                MessageBox.Show(ex.Message, "测试失败", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }
        }
Пример #4
0
        public ProductionManager(string grammerContext, string[] deriver = null)
        {
            this.VertexNonterminalSet = new List <VertexNonterminal>();
            this.VertexTerminatorSet  = new List <VertexTerminator>();
            this.GrammerRuleSet       = new List <GrammerRule>();

            List <String> textLines = grammerContext.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (textLines.Count == 0)
            {
                throw new IllegalGrammerException("语法为空");
            }

            if (deriver == null)
            {
                deriver = new String[] { "->", "→" }
            }
            ;


            foreach (var textLine in textLines)
            {
                int    iComment     = textLines.IndexOf("//");
                string realTextLine = iComment >= 0 ? textLine.Substring(0, iComment) : textLine;
                if (string.IsNullOrWhiteSpace(realTextLine))
                {
                    continue;
                }

                string[] ruleArray = realTextLine.Split(deriver, StringSplitOptions.RemoveEmptyEntries);
                if (ruleArray == null)
                {
                    throw new IllegalGrammerException("无法分析的产生式");
                }

                string   strLeft   = ruleArray[0];
                string[] strRights = ruleArray[1].Split('|');
                if (strLeft.Length != 1 || strLeft.ToUpper() != strLeft)
                {
                    throw new IllegalGrammerException("推导符左边应该为一个大写字母作为非终结符");
                }

                VertexNonterminal ruleLeft = this.AddVertexNonterminal(strLeft[0]);
                foreach (string strRight in strRights)
                {
                    List <Vertex> ruleRight = new List <Vertex>();
                    foreach (char charVertex in strRight)
                    {
                        if ('A' <= charVertex && charVertex <= 'Z')
                        {
                            ruleRight.Add(this.AddVertexNonterminal(charVertex));
                        }
                        else
                        {
                            ruleRight.Add(this.AddVertexTerminator(charVertex));
                        }
                    }
                    GrammerRule newRule = new GrammerRule(ruleLeft, ruleRight);
                    newRule.IsAcceptRule = false;
                    this.GrammerRuleSet.Add(newRule);
                }
            }

            if (this.GrammerRuleSet.Count == 0)
            {
                throw new IllegalGrammerException("语法为空");
            }

            this.VertexTerminatorSet.Add(VertexTerminator.End);
            this.GrammerRuleSet[0].IsAcceptRule = true;
        }
Пример #5
0
        public VertexNonterminal Test(string input)
        {
            Stack <Vertex> inputStack = new Stack <Vertex>();

            inputStack.Push(VertexTerminator.End);
            for (int i = input.Length - 1; i >= 0; i--)
            {
                inputStack.Push(new VertexTerminator(input[i]));
            }

            Stack <ItemSet> statusStack = new Stack <ItemSet>();
            Stack <Vertex>  letterStack = new Stack <Vertex>();

            statusStack.Push(this.ItemFamily.FirstItemSet);
            letterStack.Push(VertexTerminator.End);

            while (true)
            {
                ItemSet currentStatus = statusStack.Peek();
                Vertex  currentInput  = inputStack.Peek().UniformBy(grammer);
                if (currentInput == null)
                {
                    throw new TestFailedException("输入'" + inputStack.Peek() + "'符号不合法", inputStack, statusStack, letterStack);
                }

                if (currentStatus.ActionTable.ContainsKey(currentInput) == false)
                {
                    throw new TestFailedException("无法找到合适的规约项或者移进项", inputStack, statusStack, letterStack);
                }

                Action action = currentStatus.ActionTable[currentInput];

                if (OnAnalysisProcess != null && currentInput.GetType() == typeof(VertexTerminator))
                {
                    OnAnalysisProcess(statusStack, letterStack, inputStack, action);
                }

                if (action.GetType() == typeof(GotoAction))
                {
                    letterStack.Push(inputStack.Pop());
                    statusStack.Push(((GotoAction)action).ItemSet);
                }
                else
                {       // AC 或者 规约
                    GrammerRule rule;

                    rule = ((ConvertionAction)action).Rule;
                    int length           = rule.Right.Count;
                    VertexNonterminal vn = new VertexNonterminal(rule.Left.Name);
                    vn.Children = new List <Vertex>();
                    while (length > 0)
                    {
                        Vertex child = letterStack.Pop();
                        child.Father = vn;
                        vn.Children.Add(child);
                        statusStack.Pop();
                        length--;
                    }
                    vn.Children.Reverse();
                    if (((ConvertionAction)action).Acceptable)
                    {
                        return(vn);
                    }
                    else
                    {
                        inputStack.Push(vn);
                    }
                }
            }
        }