Exemple #1
0
 public bool IsEqual(Rule nr)
 {
     if (!Left.Equals(nr.Left) || (Right.Count != nr.Right.Count)) return false;
     bool eq = true;
     for (int i = 0; i < Right.Count; i++)
         if (!Right[i].Equals(nr.Right[i])) { eq = false; break; }
     return eq;
 }
        public void LoadRule(Grammar g, String s)
        {
            if (!s.Contains("->")) return;
            String left = s.Substring(0, s.IndexOf("->"));
            String right = s.Substring(s.IndexOf("->") + 2, s.Length - s.IndexOf("->") - 2);
            String action = String.Empty;
            if (s.Contains('`')) action = s.Substring(s.IndexOf('`') + 1);
            List<Int32> p = new List<int>();
            foreach (var param in action.Split('{'))
                if (param.Contains('}')) p.Add(Int32.Parse(param.Substring(0, param.IndexOf('}'))));
            if (action.Contains('{')) action = action.Substring(0, action.IndexOf('{'));

            var terms = left.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries);
            String lft = String.Empty;
            if (terms.Count() > 0 && terms[0].Contains("]"))
            {
                lft = terms[0].Substring(0, terms[0].IndexOf(']'));
                g.NonTerminals.Add(lft);
            }
            var rls = right.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (String rule in rls)
            {
                Rule r = new Rule(lft);
                r.Action = action;
                r.Params = p;
                foreach (String term in rule.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (term.Contains("]"))
                    {
                        var t = term.Substring(0, term.IndexOf(']'));
                        g.Terminals.Add(t);
                        r.Right.Add(t);
                    }
                }
                g.Rules.Add(r);
            }
        }