Example #1
0
 internal void Clear()
 {
     Symbols.Clear();
     CharacterSets.Clear();
     Productions.Clear();
     FAStates.Clear();
     LRActionLists.Clear();
     Groups.Clear();
 }
Example #2
0
 /// <summary>
 /// Clear all game data
 /// </summary>
 public static void ClearEvrething()
 {
     m_sEverything.Clear();
     m_sUnparsed.Clear();
     Productions.Clear();
 }
Example #3
0
        public virtual CnfGrammar Load(string[] specs)
        {
            var splitter = new Regex("\\s+");
            var binaries = new Dictionary <string, IList <CnfProduction> >();
            Func <string, string>   trim  = s => s.Trim(new[] { '\t', ' ' });
            Func <string, string[]> split = s => splitter.Split(s);

            Productions.Clear();
            for (var at = 0; at < specs.Length; at++)
            {
                var    spec = specs[at];
                string line;
                if (!(line = trim(spec)).StartsWith("#"))
                {
                    var  separatorIndex = line.IndexOf("->");
                    bool arrow;
                    separatorIndex = (arrow = (separatorIndex > 0)) ? separatorIndex : line.IndexOf(":");
                    var sides =
                        separatorIndex > 0 ?
                        new[]
                    {
                        line.Substring(0, separatorIndex),
                        line.Substring(separatorIndex + (arrow ? 2 : 1))
                    }
                        :
                    null;
                    if (sides != null)
                    {
                        var lhs         = trim(sides[0]);
                        var rhs         = trim(sides[1]);
                        var commentMark = rhs.IndexOf('#');
                        rhs = trim(commentMark >= 0 ? rhs.Substring(0, commentMark) : rhs);
                        var normalizedRhs = split(rhs);
                        var isBinaryRhs   = normalizedRhs.Length > 1;
                        var binLeft       = normalizedRhs[0];
                        var binRight      = isBinaryRhs ? normalizedRhs[1] : null;
                        var reduction     = new CnfProduction(lhs, binLeft, binRight, at);
                        var binaryKey     = isBinaryRhs ? string.Concat(binLeft, ' ', binRight) : null;
                        var reduceKey     = ReductionKey(binaryKey ?? reduction.Rhs, lhs);
                        if (Productions.Count < 1)
                        {
                            Start = reduction;
                        }
                        Productions.Add(reduceKey, reduction);
                        if (binaryKey != null)
                        {
                            IList <CnfProduction> reductions;
                            if (!binaries.TryGetValue(binaryKey, out reductions))
                            {
                                binaries.Add(binaryKey, new List <CnfProduction>());
                            }
                            binaries[binaryKey].Add(reduction);
                        }
                    }
                }
            }
            Binaries = binaries.Values.SelectMany(productions => productions).ToArray();
            Lexicon  =
                new CnfLexicon
                (
                    Productions.
                    Values.
                    Where(production => production.IsLexical).
                    OrderByDescending(production => !production.IsLiteral ? production.Pattern.Length : production.Literal.Length).
                    ToArray()
                );
            return(this);
        }