Of() public static method

public static Of ( string v ) : Nonterminal
v string
return Nonterminal
コード例 #1
0
        // todo: horrible
        private Nonterminal GetFresh()
        {
            var         originalNonterminals = _grammar.GetNonterminals();
            Nonterminal result;

            do
            {
                result = Nonterminal.Of("X_" + _freshx);
                _freshx++;
            } while (originalNonterminals.Contains(result));
            return(result);
        }
コード例 #2
0
        private Nonterminal RandomNonterminal(int numNonterminals, bool allowStart = true)
        {
            int num;

            if (allowStart)
            {
                num = _rand.Next(0, numNonterminals);
            }
            else
            {
                num = _rand.Next(1, numNonterminals);
            }
            return(Nonterminal.Of("X_" + num));
        }
コード例 #3
0
ファイル: CFGParser.cs プロジェクト: simple555a/CFGLib
        /// <summary>
        /// Turns a string like
        /// &lt;X> -> &lt;X> 'a' &lt;X>
        /// into a production.
        /// Nonterminals must be surrounded by angled brackets, terminals must be surrounded by single quotes, and everything must be separated by spaces.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Production Production(string s)
        {
            var match = ProductionRegex.Match(s);

            if (!match.Success)
            {
                throw new Exception("Didn't find valid string");
            }

            var lhsMatch    = match.Groups["lhs"];
            var ntMatch     = match.Groups["nt"];
            var tMatch      = match.Groups["t"];
            var weightMatch = match.Groups["weight"];

            if (!lhsMatch.Success)
            {
                throw new Exception("Didn't find LHS");
            }

            double weight;

            if (!double.TryParse(weightMatch.Value, out weight))
            {
                weight = 1.0;
            }

            var rhsList = new SortedList <int, Word>();

            foreach (Capture capture in ntMatch.Captures)
            {
                var word = Nonterminal.Of(capture.Value);
                rhsList.Add(capture.Index, word);
            }
            foreach (Capture capture in tMatch.Captures)
            {
                var word = Terminal.Of(capture.Value);
                rhsList.Add(capture.Index, word);
            }
            var rhs = new Sentence(rhsList.Values);
            var lhs = Nonterminal.Of(lhsMatch.Value);

            var retval = new Production(lhs, rhs, weight);

            return(retval);
        }
コード例 #4
0
 public Production(string lhsName, Word rhsOnlyWord, double weight = 1.0) : this(Nonterminal.Of(lhsName), new Sentence(rhsOnlyWord), weight)
 {
 }
コード例 #5
0
 public Production(string lhsName, Sentence rhs, double weight = 1.0) : this(Nonterminal.Of(lhsName), rhs, weight)
 {
 }