예제 #1
0
파일: Grammar.cs 프로젝트: sdao/LingParse
        public List<List<ParseNode>> Parse(List<ParseNode> input)
        {
            List<ParseNode> tree = input.GetRange(0, input.Count);
            List<List<ParseNode>> outputList = new List<List<ParseNode>>();
            HashQueue workQueue = new HashQueue();

            workQueue.Enqueue(tree);

            while (workQueue.Count > 0)
            {
                ParseOnePass(workQueue, outputList);
            }

            return outputList;
        }
예제 #2
0
파일: Grammar.cs 프로젝트: sdao/LingParse
        private void ParseOnePass(HashQueue work, List<List<ParseNode>> output)
        {
            List<ParseNode> input = work.Dequeue();

            for (int i = 0; i < input.Count; i++)
            {
                ParseNode l = input[i];
                ParseNode r = i + 1 < input.Count ? input[i + 1] : ParseNode.EmptyParseNode();

                foreach (GrammarDefinition rule in Rules)
                {
                    int modify = 0;
                    if (rule.Right == SyntaxCategories.SYNTAX_CATEGORY_EMPTY)
                    {
                        if (rule.Left == l.Type)
                        {
                            modify = 1;
                        }
                    }
                    else
                    {
                        if (rule.Left == l.Type && rule.Right == r.Type)
                        {
                            modify = 2;
                        }
                    }

                    if (modify > 0)
                    {
                        List<ParseNode> modifiedInput = input.GetRange(0, input.Count);
                        modifiedInput.RemoveRange(i, modify);

                        ParseNode parent = new ParseNode(rule.Parent, null, l, modify > 1 ? r : ParseNode.EmptyParseNode());
                        modifiedInput.Insert(i, parent);

                        if (modifiedInput.Count == 1 && modifiedInput[0].Type == SyntaxCategories.SYNTAX_CATEGORY_FULL)
                            output.Add(modifiedInput);
                        else
                            work.Enqueue(modifiedInput);
                    }
                }
            }
        }