Of() public static method

public static Of ( string v ) : Terminal
v string
return Terminal
Example #1
0
        /// <summary>
        /// Create a new sentence where string in the given list is treated as a separate terminal
        /// </summary>
        /// <param name="tokens"></param>
        /// <returns></returns>
        public static Sentence FromTokens(IEnumerable <string> tokens)
        {
            var l = new List <Terminal>();

            foreach (var token in tokens)
            {
                // l.Add(Terminal.Of(c.ToString()));
                l.Add(Terminal.Of(token));
            }
            return(new Sentence(l));
        }
Example #2
0
        /// <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);
        }
Example #3
0
        public void RandomTesting()
        {
            var randg = new GrammarGenerator();

            int _maxNonterminals = 8;
            int _maxProductions  = 8;
            int _maxTerminals    = 20;
            int _step            = 1;

            for (int numProductions = 0; numProductions < _maxProductions; numProductions += _step)
            {
                for (int numNonterminals = 1; numNonterminals < _maxNonterminals; numNonterminals += _step)
                {
                    for (int numTerminals = 1; numTerminals < _maxTerminals; numTerminals += _step)
                    {
                        var range     = Enumerable.Range(0, numTerminals);
                        var terminals = new List <Terminal>(range.Select((x) => Terminal.Of("x" + x)));
                        Console.WriteLine("{0}, {1}, {2}", numNonterminals, numProductions, numTerminals);
                        var rg = randg.NextCNF(numNonterminals, numProductions, terminals);
                        TestGrammar(rg);
                    }
                }
            }
        }