Exemplo n.º 1
0
 public static void Derive(string axiom, float angle, int derivations, Dictionary <string, List <Production> > productions, out string moduleString)
 {
     moduleString = axiom;
     for (int i = 0; i < Math.Max(1, derivations); i++)
     {
         string newModuleString = "";
         for (int j = 0; j < moduleString.Length; j++)
         {
             string module = moduleString[j] + "";
             if (!productions.ContainsKey(module))
             {
                 newModuleString += module;
                 continue;
             }
             var production = ProductionMatcher.Match(module, productions);
             newModuleString += production.successor;
         }
         moduleString = newModuleString;
     }
 }
Exemplo n.º 2
0
    public static void Parse(string content, out string axiom, out float angle, out int derivations, out Dictionary <string, List <Production> > productions)
    {
        axiom       = "";
        angle       = 0;
        derivations = 0;
        productions = new Dictionary <string, List <Production> >();
        var lines = content.Split('\n');

        foreach (string rawLine in lines)
        {
            string line = rawLine.Trim();
            if (line.Length == 0)
            {
                continue;
            }
            else if (line.Length == 1 && line[0] == '\r')
            {
                continue;
            }
            else if (line[0] == '/' && line[1] == '/')
            {
                continue;
            }
            string value;
            if (line.IndexOf("axiom") != -1)
            {
                value = line.Substring(line.IndexOf("=") + 1);
                value = value.Trim();
                axiom = value;
            }
            else if (line.IndexOf("angle") != -1)
            {
                value = line.Substring(line.IndexOf("=") + 1);
                value = value.Trim();
                angle = float.Parse(value);
            }
            else if (line.IndexOf("number of derivations") != -1)
            {
                value       = line.Substring(line.IndexOf("=") + 1);
                value       = value.Trim();
                derivations = int.Parse(value);
            }
            else
            {
                string[] tokens = line.Split('=');
                if (tokens.Length != 2)
                {
                    continue;
                }
                string predecessor = tokens[0].Trim();
                tokens = tokens[1].Trim().Split(')');
                string probabilityString = tokens[0].Substring(1);
                string successor         = tokens[1];
                float  probability       = float.Parse(probabilityString);
                if (!productions.ContainsKey(predecessor))
                {
                    productions[predecessor] = new List <Production>();
                }
                productions[predecessor].Add(new Production(predecessor, successor, probability));
            }
        }
        if (!ProductionMatcher.CheckProbabilities(productions))
        {
            throw new Exception("There's one of more production rules with probability < 1");
        }
    }
Exemplo n.º 3
0
        public void TestMapAndReduce()
        {
            string xml = @"
            <MapReduce>
                <Map>
                    <MapRule Type = 'MapRuleOnT1IfTrue' />
                </Map>
             <Reduce>
                    <ReduceRule Type = 'ReduceRuleOnT1' />
                    <ReduceRule Type = 'AssignRuleOnT1' />
                </Reduce>
            </MapReduce>";
            XDocument _xDoc = _xDoc = XDocument.Parse(xml);
            XElement source = _xDoc.Element("MapReduce");
            IMatcher rule = new TerminalRuleMatcher(new Pattern("Rule", TokenType.RULE));
            IMatcher mapRule = new TerminalRuleMatcher(new Pattern("MapRule", TokenType.MAPRULE));
            IMatcher reduceRule = new TerminalRuleMatcher(new Pattern("ReduceRule", TokenType.REDUCERULE));
            IMatcher map = new ProductionMatcher(new Pattern("Map", TokenType.MAP));
            IMatcher reduce = new ProductionMatcher(new Pattern("Reduce", TokenType.REDUCE));
            IMatcher mapReduce = new ProductionMatcher(new Pattern("MapReduce", TokenType.MAPREDUCE));

            List<Token> results = new List<Token>();
            var t = new Tokenizer(source, results, rule, mapRule, reduceRule, map, reduce, mapReduce);
            t.Parse();
            Assert.AreEqual(TokenType.MAPREDUCE, results[0].TokenType);
            Assert.AreEqual(TokenType.MAP, results[1].TokenType);
            Assert.AreEqual(TokenType.MAPRULE, results[2].TokenType);
            Assert.AreEqual(TokenType.EOF, results[3].TokenType);
            Assert.AreEqual(TokenType.REDUCE, results[4].TokenType);
            Assert.AreEqual(TokenType.REDUCERULE, results[5].TokenType);
            Assert.AreEqual(TokenType.REDUCERULE, results[6].TokenType);
            Assert.AreEqual(TokenType.EOF, results[7].TokenType);
            Assert.AreEqual(TokenType.EOF, results[8].TokenType);
        }
Exemplo n.º 4
0
 public void TestReduce()
 {
     string xml = @"
             <Reduce>
     <ReduceRule Type = 'ReduceRuleOnT1' />
     <ReduceRule Type = 'AssignRuleOnT1' />
     </Reduce>";
     XDocument _xDoc = _xDoc = XDocument.Parse(xml);
     XElement source = _xDoc.Element("Reduce");
     IMatcher reduceRule = new TerminalRuleMatcher(new Pattern("ReduceRule", TokenType.REDUCERULE));
     IMatcher reduce = new ProductionMatcher(new Pattern("Reduce", TokenType.REDUCE));
     List<Token> results = new List<Token>();
     var t = new Tokenizer(source, results, reduceRule, reduce);
     t.Parse();
     Assert.AreEqual(TokenType.REDUCE, results[0].TokenType);
     Assert.AreEqual(TokenType.REDUCERULE, results[1].TokenType);
     Assert.AreEqual(TokenType.REDUCERULE, results[2].TokenType);
     Assert.AreEqual(TokenType.EOF, results[3].TokenType);
 }
Exemplo n.º 5
0
 public void TestMapRulesTwoRules()
 {
     string xml = @"
         <Map>
             <Rule Type = 'IninValueOnT2' />
             <Rule Type = 'IninValueOnT2Add' />
         </Map>";
     XDocument _xDoc = _xDoc = XDocument.Parse(xml); XElement source = _xDoc.Element("Map");
     IMatcher rule = new TerminalRuleMatcher(new Pattern("Rule", TokenType.RULE));
     IMatcher map = new ProductionMatcher(new Pattern("Map", TokenType.MAP));
     List<Token> results = new List<Token>();
     var t = new Tokenizer(source, results, rule, map);
     t.Parse();
     Assert.AreEqual(TokenType.MAP, results[0].TokenType);
     Assert.AreEqual(TokenType.RULE, results[1].TokenType);
     Assert.AreEqual(TokenType.RULE, results[2].TokenType);
     Assert.AreEqual(TokenType.EOF, results[3].TokenType);
 }