当前位置: 首页>>代码示例>>C#>>正文


C# Column.AddState方法代码示例

本文整理汇总了C#中Column.AddState方法的典型用法代码示例。如果您正苦于以下问题:C# Column.AddState方法的具体用法?C# Column.AddState怎么用?C# Column.AddState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Column的用法示例。


在下文中一共展示了Column.AddState方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Complete

        private void Complete(Column col, State state)
        {
            if (state.Rule.Name.NonTerminal == GammaRule)
            {
                col.GammaStates.Add(state);
                return;
            }

            foreach (var st in state.StartColumn)
            {
                var term = st.NextProductionTerm();
                NonTerminalStack intersection;
                if (IsCompletedTermConsistentWithNextTerm(state.Rule.Name, term, out intersection))
                {
                    if (state.Node.LogProbability < 0)
                    {
                        throw new LogException(
                            string.Format(
                                "trrrr! NODE log probability lower than 0: {0}, reductor state: {1}, predecessor state {2}",
                                state.Node.LogProbability, state, st));
                    }
                    var y = State.MakeNode(st, state.EndColumn.Index, state.Node);
                    var newState = new State(st.Rule, st.DotIndex + 1, st.StartColumn, y);
                    newState.Rule.Production[st.DotIndex].Stack = intersection;

                    col.AddState(newState, ParsingOperation.Complete);
                    if (Debug)
                        Console.WriteLine("{0} & {1} & {2} & Completed from States {3} and {4}\\\\",
                            newState.StateNumber,
                            newState, col.Index, st.StateNumber, state.StateNumber);
                }
            }
        }
开发者ID:JosephPotashnik,项目名称:LIG,代码行数:33,代码来源:Parser.cs

示例2: Predict

        private void Predict(Column col, List<Rule> ruleList, State state, NonTerminalObject currObject)
        {
            if (generator)
            {
                var rule = Grammar.GetRandomRuleForAGivenLHS(currObject.NonTerminal, true);
                ruleList = new List<Rule> {rule};
            }

            foreach (var rule in ruleList)
            {
                //TODO: change later to skip -all- rules whose derivation leads to the empty string.
                //I.e,  A-> B.C , C -> D E. D -> epsilon, E -> epsilon. C itself is not an epsilon rule.
                if (rule.IsEpsilonRule() && Grammar.nullableProductions.ContainsKey(currObject))
                {
                    //states that are the result of a spontenous dot shift (due to nullable production) 
                    //have already been added to the agendas in Column.Add()
                    continue;
                }


                //if current stack is empty but predicted stack is not, mismatch - do not predict this rule.
                if (currObject.IsStackEmpty() && !rule.IsInitialOrDotStack()) continue;


                //prepare new rule based on the stack information contained in the current state
                //and based on the predicted rule.
                var createdRule = new Rule(rule);


                //if the rule is not a stack manipulating rule, 
                if (rule.IsInitialRule())
                {
                    var complementPositionObject = createdRule.Production[createdRule.ComplementPosition];

                    //if current stack is not empty, but the complement position does not allow for stacks (POS),
                    //mismatch - do not predict this rule.
                    if (!currObject.IsStackEmpty() && Grammar.IsPOS(complementPositionObject.NonTerminal)) continue;

                    //the stack of the LHS of the created rule is the stack of the current object:
                    createdRule.Name = currObject;

                    //copy the stack to the complement position.
                    complementPositionObject.Stack = currObject.Stack;
                }
                else
                {
                    if (createdRule.Name.Stack.Peek() != "." && currObject.Stack.Top != Grammar.Epsilon &&
                        currObject.Stack.Top != createdRule.Name.Stack.Peek())
                        continue;
                    //if tops of the stacks do not match, continue. e.g created rule PP[PP] -> epsilon, current object: PP[NP].

                    //create left hand side of new rule.
                    createdRule.Name.Stack = currObject.Stack;
                    createdRule.Name.NonTerminal = currObject.NonTerminal;

                    //create right hand side of new rule.
                    NonTerminalStack contentOfDot;
                    if (rule.Name.Stack.Peek() == ".")
                        contentOfDot = currObject.Stack; //e.g. A[..]
                    else
                        contentOfDot = currObject.Stack.GetPrefixListStackObjectOfGivenTop(rule.Name.Stack.Peek());
                            //e.g A[..X]

                    for (var i = 0; i < rule.Production.Length; i++)
                    {
                        var s = rule.Production[i].Stack;

                        if (s != null)
                        {
                            if (s.Peek() == ".")
                                createdRule.Production[i].Stack = contentOfDot; // e.g, A[..] pop rule.
                            else if (s.PrefixList == null)
                                createdRule.Production[i].Stack = s; //e.g. A[X] //secondary constituent.
                            else
                                createdRule.Production[i].Stack = new NonTerminalStack(s.Peek(), contentOfDot);
                            //e.g. A[..X] - push rule.

                            //calculate the new weight of the top of the stack from the weights of its sons.
                            //if (createdRule.Production[i].Stack != null)
                            //    createdRule.Production[i].Stack.Weight = createdRule.Production[i].Stack.PrefixList != null ? createdRule.Production[i].Stack.PrefixList.Sum(x => x.Weight) : 1;
                        }
                    }
                }


                var newState = new State(createdRule, 0, col, null) {LogProbability = ruleLogProbabilities[rule.Number]};

                if (newState.LogProbability < 0)
                    throw new Exception("wrong probability");

                var added = col.AddState(newState, ParsingOperation.Predict);

                if (Debug)
                    Console.WriteLine("{0} & {1} & {2} & Predicted from State {3}, added: {4}\\\\", newState.StateNumber,
                        newState,
                        col.Index, state.StateNumber, added);
            }
        }
开发者ID:JosephPotashnik,项目名称:LIG,代码行数:98,代码来源:Parser.cs

示例3: Scan

        private void Scan(Column col, State state, string term, string token)
        {
            //if there is nonempty stack arriving to this part of speech term, stop here - the derivation is wrong.
            //SEFI - note: this is a restriction on the form of your grammar.
            //consider removing it to see the conequences.

            //if (Grammar.isPOS(term))
            {
                if (!state.NextProductionTerm().IsStackEmpty()) return;
            }

            var v = new Node(term, col.Index - 1, col.Index)
            {
                AssociatedTerminal = token,
                LogProbability = 0.0f,
                Bits = 1
            };
            var y = State.MakeNode(state, col.Index, v);
            var newState = new State(state.Rule, state.DotIndex + 1, state.StartColumn, y);

            col.AddState(newState, ParsingOperation.Scan);
            if (Debug)
                Console.WriteLine("{0} & {1} & {2} & Scanned from State {3}, word: {4}\\\\", newState.StateNumber,
                    newState, col.Index,
                    state.StateNumber, token);

            if (newState.Node.LogProbability < 0)
            {
                throw new LogException(string.Format("scanarrrr! NODE log probability lower than 0: {0}, state: {1}",
                    newState.Node.LogProbability, newState));
            }
        }
开发者ID:JosephPotashnik,项目名称:LIG,代码行数:32,代码来源:Parser.cs


注:本文中的Column.AddState方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。