コード例 #1
0
        private Rule ParseString(string str, int idx)
        {
            Regex           stateReg     = new Regex(@"<\$[n,t,o]\$[^$]*>");
            Regex           stateTypeReg = new Regex(@"\$(n|t|o)\$");
            Regex           nameReg      = new Regex(@"\$[^\$]*>");
            Rule            Rule         = new Rule();
            MatchCollection matches      = stateReg.Matches(str);
            string          str_match    = matches[0].ToString();
            string          type         = stateTypeReg.Match(str_match).ToString();

            type = type.Substring(1, type.Length - 2);
            if (type == "n")
            {
                string name = nameReg.Match(str_match).ToString();
                name = name.Substring(1, name.Length - 4);
                NonTerminal nonterm = new NonTerminal(name);
                Rule.LeftPart = nonterm;
                LoadedNonTerminals.Add(nonterm);
            }
            else
            {
                throw new Exception("terminal cannot spawn rules");
            }
            for (int j = 1; j < matches.Count; j++)
            {
                type      = stateTypeReg.Match(matches[j].ToString()).ToString();
                type      = type.Substring(1, type.Length - 2);
                str_match = matches[j].ToString();
                string name = nameReg.Match(str_match).ToString();
                name = name.Substring(1, name.Length - 2);
                switch (type)
                {
                case "n":
                    NonTerminal nonterm = new NonTerminal(name);
                    LoadedNonTerminals.Add(nonterm);
                    Rule.RightPart.Add(nonterm);
                    break;

                case "t":
                    Terminal term = new Terminal(name);
                    LoadedTerminals.Add(term);
                    Rule.RightPart.Add(term);
                    break;

                case "o":
                    Operation operation = new Operation(name);
                    Rule.RightPart.Add(operation);
                    break;

                default:
                    throw new Exception(string.Format("In {0} line type of state is not valid", idx + 1));
                }
                ;
            }
            return(Rule);
        }
コード例 #2
0
        public void Load(string filepath)
        {
            IList <string> text = new List <string>();

            using (StreamReader sr = new StreamReader(filepath))
            {
                while (!sr.EndOfStream)
                {
                    text.Add(sr.ReadLine());
                }
            }
            if (text.Count > 0)
            {
                for (int i = 0; i < text.Count; i++)
                {
                    Rule newRule = ParseString(text[i], i);
                    LoadedNonTerminals.Add(newRule.LeftPart);
                    foreach (IState state in newRule.RightPart)
                    {
                        if (state is NonTerminal)
                        {
                            LoadedNonTerminals.Add(state as NonTerminal);
                        }
                        else if (state is Terminal)
                        {
                            LoadedTerminals.Add(state as Terminal);
                        }
                    }
                    LoadedRules.Add(newRule);
                }
                LoadedNonTerminals = LoadedNonTerminals.DistinctBy(x => x.Name).ToList();
                LoadedTerminals    = LoadedTerminals.DistinctBy(x => x.Name).ToList();
                //CreateRecognizeTable();
            }
            else
            {
                throw new Exception("Empty file");
            }
        }