Exemplo n.º 1
0
        public void SetRuleTest()
        {
            Rule[] expectedRules =
            {
                new Rule(0, new[] { 1, 2, 3 }),       // "S -> C if W"
            };

            GrammarLoader loader = new GrammarLoader();

            loader = loader.SetRule("S -> C if W");

            for (int i = 0; i < loader.Rules.Length; i++)
            {
                for (int wordIndex = 0; wordIndex < expectedRules[i].CountOfWords; wordIndex++)
                {
                    Assert.AreEqual(expectedRules[i].RuleList[wordIndex], loader.Rules[i].RuleList[wordIndex]);
                }
            }

            Word[] expectedWords = { new Word(0, "S"), new Word(1, "C"), new Word(2, "if"), new Word(3, "W") };

            for (int ruleIndex = 0; ruleIndex < loader.Rules.Length; ruleIndex++)
            {
                Assert.AreEqual(expectedWords[ruleIndex].Value, loader.Words[ruleIndex].Value);
                Assert.AreEqual(expectedWords[ruleIndex].Number, loader.Words[ruleIndex].Number);
            }
        }
Exemplo n.º 2
0
            /// <summary>
            /// Imports production tules from another grammar file
            /// </summary>
            /// <param name="directive">Specifies the import method used</param>
            /// <param name="nonTerminal">The non-terminal in which the grammar file will be imported</param>
            /// <param name="path">Path to the grammar file to be imported</param>
            /// be dumped</param>
            private void ImportSubGrammar(string directive, string path, string nonTerminal = null)
            {
                if (directive != "import")
                {
                    return;
                }
                if (!String.IsNullOrEmpty(nonTerminal))
                {
                    ImportSubGrammarIntoNT(directive, path, nonTerminal);
                    return;
                }
                if (!File.Exists(path))
                {
                    return;
                }

                Grammar subGrammar = new GrammarLoader().FromFile(path, false);

                if (subGrammar == null)
                {
                    return;
                }
                subGrammar.productionRules.Remove("$Main");
                foreach (var item in subGrammar.productionRules)
                {
                    if (grammar.productionRules.ContainsKey(item.Key))
                    {
                        grammar.productionRules[item.Key].AddReplacements(item.Value);
                    }
                    else
                    {
                        grammar.productionRules.Add(item.Key, item.Value);
                    }
                }
            }
Exemplo n.º 3
0
            /// <summary>
            /// Loads another grammar file within the existing one
            /// </summary>
            /// <param name="directive">Specifies the import method used</param>
            /// <param name="nonTerminal">The non-terminal in which the grammar file will be imported</param>
            /// <param name="path">Path to the grammar file to be imported</param>
            /// be dumped</param>
            private void ImportGrammar(string directive, string path, string nonTerminal = null)
            {
                if (directive == "import")
                {
                    ImportSubGrammar(directive, path, nonTerminal);
                }
                else if ((directive != "load") || !File.Exists(path))
                {
                    return;
                }

                Grammar subGrammar = new GrammarLoader().FromFile(path, false);

                if (subGrammar == null)
                {
                    return;
                }
                foreach (var item in subGrammar.productionRules)
                {
                    if (grammar.productionRules.ContainsKey(item.Key))
                    {
                        grammar.productionRules[item.Key].AddReplacements(item.Value);
                    }
                    else
                    {
                        grammar.productionRules.Add(item.Key, item.Value);
                    }
                }
            }
Exemplo n.º 4
0
        public void parseGrammarFile(string fileName)
        {
            GrammarLoader grammarLoader = new GrammarLoader();

            Grammar = grammarLoader.loadGrammarFile(fileName);
            Grammar.parseGrammar();
        }
Exemplo n.º 5
0
 public ParserLoader(string pathToWords, string pathToRules, string pathToTable)
 {
     GrammarLoader = new GrammarLoader(pathToWords, pathToRules);
     Words         = GrammarLoader.Words;
     Rules         = GrammarLoader.Rules;
     ControlTable  = LoadTable(pathToTable);
 }
Exemplo n.º 6
0
        public DataLoader(
            ref Dictionary <int, Dictionary <string, string> > table,
            ref Dictionary <int, Dictionary <string, List <string> > > grammar)
        {
            string tablePath   = "./utils/LR1 Table.csv";
            string grammarPath = "./utils/Grammar.csv";

            table   = new LR1TableLoader(tablePath).getTable();
            grammar = new GrammarLoader(grammarPath).getGrammar();
        }
Exemplo n.º 7
0
        public void SetDuplicatedWordTest()
        {
            Word[]        expectedWords = { new Word(0, "S") };
            GrammarLoader loader        = new GrammarLoader();

            loader = loader.SetWord("S").SetWord("S");

            for (int i = 0; i < loader.Words.Length; i++)
            {
                Assert.AreEqual(expectedWords[i].Value, loader.Words[i].Value);
                Assert.AreEqual(expectedWords[i].Number, loader.Words[i].Number);
            }
        }
Exemplo n.º 8
0
        public void GetNotExistedWordTest()
        {
            GrammarLoader loader = new GrammarLoader();

            loader = loader
                     .SetWord("S")
                     .SetWord("S")
                     .SetWord("C")
                     .SetWord("if");

            Word srchWord = loader.GetWord("W");

            Assert.IsNull(srchWord);
        }
Exemplo n.º 9
0
        public LrParserTests()
        {
            grammarLoader = new GrammarLoader();
            ParserLoader loader = new ParserLoader("words.txt", "rules.txt", "table.txt");

            ruleTable     = loader.LoadTable("table.txt");
            grammarLoader = grammarLoader
                            .SetWord("S")
                            .SetWord("C")
                            .SetWord("W")
                            .SetWord("X")
                            .SetWord("A")
                            .SetWord("L")
                            .SetWord("R")
                            .SetWord("if")
                            .SetWord(":=")
                            .SetWord("id")
                            .SetWord("[")
                            .SetWord("]")
                            .SetWord("const")
                            .SetWord("sqr")
                            .SetWord("=")
                            .SetWord("!")
                            .SetWord(">")
                            .SetWord("<")
                            .SetWord("-")
                            .SetWord("$");

            grammarLoader = grammarLoader
                            .SetRule("S -> C if W")
                            .SetRule("S -> C if W S")
                            .SetRule("C -> L A")
                            .SetRule("W -> X A")
                            .SetRule("W -> X L")
                            .SetRule("X -> := id")
                            .SetRule("X -> := id [ A ]")
                            .SetRule("A -> id")
                            .SetRule("A -> const")
                            .SetRule("A -> id [ A ]")
                            .SetRule("A -> sqr A")
                            .SetRule("A -> R A")
                            .SetRule("L -> = A")
                            .SetRule("L -> ! A")
                            .SetRule("L -> > A")
                            .SetRule("L -> < A")
                            .SetRule("R -> - A");

            textParser = new TextParser(grammarLoader);
        }
Exemplo n.º 10
0
        public void CheckCountOfWordsTest()
        {
            GrammarLoader loader = new GrammarLoader();

            loader = loader
                     .SetWord("S")
                     .SetWord("S")
                     .SetWord("C")
                     .SetWord("if")
                     .SetWord("E")
                     .SetWord("if")
                     .SetWord("S")
                     .SetWord("L");

            Assert.AreEqual(loader.Words.Length, 5);
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            var grammar = GrammarLoader.Load("mangle-rule.txt");

            grammar.AddTag(new Number());
            grammar.AddTag(new SourceName());
            var parser = new Parser(grammar);

            while (true)
            {
                Console.Write("> ");
                string input = Console.ReadLine();
                var    tree  = parser.Parse(input);
                if (tree.Status == ParseTree.ParseStatus.Success)
                {
                    print(tree.Tree, 0);
                }
                else
                {
                    Console.WriteLine("Parse fail...");
                }
            }
        }
Exemplo n.º 12
0
            /// <summary>
            /// Imports production tules from another grammar file into a non-terminal
            /// </summary>
            /// <param name="directive">Specifies the import method used</param>
            /// <param name="nonTerminal">The non-terminal in which the grammar file will be imported</param>
            /// <param name="path">Path to the grammar file to be imported</param>
            /// be dumped</param>
            private void ImportSubGrammarIntoNT(string directive, string path, string nonTerminal)
            {
                if (String.IsNullOrEmpty(nonTerminal))
                {
                    ImportSubGrammar(directive, path, nonTerminal);
                    return;
                }
                if (directive != "import")
                {
                    return;
                }

                ProductionRule pr     = null;
                string         errMsg = "#ERROR! {void meta:{0}}";

                if (!File.Exists(path))
                {
                    errMsg = String.Format(errMsg, String.Format("File {0} not found", path));
                    pr     = new ProductionRule(nonTerminal, new String[] { errMsg });
                    if (grammar.productionRules.ContainsKey(nonTerminal))
                    {
                        grammar.productionRules[nonTerminal].AddReplacements(pr);
                    }
                    else
                    {
                        grammar.productionRules.Add(nonTerminal, pr);
                    }
                    return;
                }

                Grammar subGrammar = new GrammarLoader().FromFile(path, true);

                if (subGrammar == null)
                {
                    errMsg = String.Format(errMsg, String.Format("Cannot load grammar file {0}", path));
                    pr     = new ProductionRule(nonTerminal, new String[] { errMsg });
                    if (grammar.productionRules.ContainsKey(nonTerminal))
                    {
                        grammar.productionRules[nonTerminal].AddReplacements(pr);
                    }
                    else
                    {
                        grammar.productionRules.Add(nonTerminal, pr);
                    }
                    return;
                }

                errMsg = String.Format(errMsg, "Not implemented. Sorry =(");
                pr     = new ProductionRule(nonTerminal, new String[] { errMsg });
                if (grammar.productionRules.ContainsKey(nonTerminal))
                {
                    grammar.productionRules[nonTerminal].AddReplacements(pr);
                }
                else
                {
                    grammar.productionRules.Add(nonTerminal, pr);
                }

                // var main = subGrammar.productionRules["$Main"];
                // subGrammar.productionRules.Remove("$Main");
                // foreach(var item in subGrammar.productionRules){
                //  if(grammar.productionRules.ContainsKey(item.Key))
                //      grammar.productionRules[item.Key].AddReplacements(item.Value);
                //  else
                //      grammar.productionRules.Add(item.Key, item.Value);
                // }
            }
Exemplo n.º 13
0
        public void StringParseTest()
        {
            GrammarLoader grammarLoader = new GrammarLoader();

            grammarLoader = grammarLoader
                            .SetWord("S")
                            .SetWord("C")
                            .SetWord("W")
                            .SetWord("X")
                            .SetWord("A")
                            .SetWord("L")
                            .SetWord("R")
                            .SetWord("if")
                            .SetWord(":=")
                            .SetWord("id")
                            .SetWord("[")
                            .SetWord("]")
                            .SetWord("const")
                            .SetWord("sqr")
                            .SetWord("=")
                            .SetWord("!")
                            .SetWord(">")
                            .SetWord("<")
                            .SetWord("-")
                            .SetWord("$");

            grammarLoader = grammarLoader
                            .SetRule("S -> C if W")
                            .SetRule("S -> C if W S")
                            .SetRule("C -> L A")
                            .SetRule("W -> X A")
                            .SetRule("W -> X L")
                            .SetRule("X -> := id")
                            .SetRule("X -> := id [ A ]")
                            .SetRule("A -> id")
                            .SetRule("A -> const")
                            .SetRule("A -> id [ A ]")
                            .SetRule("A -> sqr A")
                            .SetRule("A -> R A")
                            .SetRule("L -> = A")
                            .SetRule("L -> ! A")
                            .SetRule("L -> > A")
                            .SetRule("L -> < A")
                            .SetRule("R -> - A");



            ParserLoader loader = new ParserLoader("words.txt", "rules.txt", "table.txt");

            int[,] table = loader.LoadTable("table.txt");

            TextParser parser = new TextParser(grammarLoader);

            string textToParse = "= k 12 if := num 10 = 3 3 if := id 6";

            Queue <Word> actualWords = parser.Parse(textToParse);

            //LrParser analysis = new LrParser(rules, words, table, 7);

            //Queue<Word> wordsActual =  analysis.Up("= k 12 if := num 10 = 3 3 if := id 6");

            Queue <Word> expectedwords = new Queue <Word>(
                new[]
            {
                new Word(14, "="),
                new Word(9, "id")
                {
                    Temp = "k"
                },
                new Word(12, "const")
                {
                    Temp = 12.ToString()
                },
                new Word(7, "if"),
                new Word(8, ":="),
                new Word(9, "id")
                {
                    Temp = "num"
                },
                new Word(12, "const")
                {
                    Temp = 10.ToString()
                },
                new Word(14, "="),
                new Word(12, "const")
                {
                    Temp = 3.ToString()
                },
                new Word(12, "const")
                {
                    Temp = 3.ToString()
                },
                new Word(7, "if"),
                new Word(8, ":="),
                new Word(9, "id")
                {
                    Temp = "id"
                },
                new Word(12, "const")
                {
                    Temp = 6.ToString()
                },
                new Word(19, "$")
            }
                );

            Assert.AreEqual(expectedwords.Count, actualWords.Count, $"Длинна очередей разная.\nОжидается: {expectedwords.Count}\nТекущая: {actualWords.Count}");

            int count = 0;

            while (expectedwords.Count > 0)
            {
                Word expectedWord = expectedwords.Dequeue();
                Word actualWord   = actualWords.Dequeue();
                Assert.AreEqual(expectedWord.Value, actualWord.Value, GetErrorMessage(expectedWord, actualWord, count));
                Assert.AreEqual(expectedWord.Number, actualWord.Number, GetErrorMessage(expectedWord, actualWord, count));
                Assert.AreEqual(expectedWord.Temp, actualWord.Temp, GetErrorMessage(expectedWord, actualWord, count));
                count++;
            }
        }
Exemplo n.º 14
0
        public void CheckCountOfRulesWithLoadFromFileTest()
        {
            GrammarLoader loader = new GrammarLoader("words.txt", "rules.txt");

            Assert.AreEqual(loader.CountOfRules, 7);
        }
Exemplo n.º 15
0
        public void LoadFromFileTest()
        {
            Word[] expectedWords =
            {
                new Word(0,  "S"),
                new Word(1,  "C"),
                new Word(2,  "W"),
                new Word(3,  "X"),
                new Word(4,  "A"),
                new Word(5,  "L"),
                new Word(6,  "R"),
                new Word(7,  "if"),
                new Word(8,  ":="),
                new Word(9,  "id"),
                new Word(10, "["),
                new Word(11, "]"),
                new Word(12, "const"),
                new Word(13, "sqr"),
                new Word(14, "="),
                new Word(15, "!"),
                new Word(16, ">"),
                new Word(17, "<"),
                new Word(18, "-"),
                new Word(19, "$")
            };

            Rule[] expectedRules =
            {
                new Rule(0, new[] {  1,  7,  2 }), // S-> C if W
                new Rule(0, new[] {  1,  7,2, 0 }),   // S-> C if W S
                new Rule(1, new[] {  5,4 }),          // C -> L A
                new Rule(2, new[] {  3,4 }),          // W -> X A
                new Rule(2, new[] {  3,5 }),          // W -> X L
                new Rule(3, new[] {  8,9 }),          // X -> := id
                new Rule(3, new[] {  8,  9,10, 4, 11 }), // X -> := id [ A ]
                new Rule(4, new[] {9 }),              // A -> id
                new Rule(4, new[] {12 }),             // A -> const
                new Rule(4, new[] {  9, 10,4, 11 }),  // A -> id [ A ]
                new Rule(4, new[] { 13,4 }),          // A -> sqr A
                new Rule(4, new[] {  6,4 }),          // A -> R A
                new Rule(5, new[] { 14,4 }),           // L -> = A
                new Rule(5, new[] { 15,4 }),          // L -> ! A
                new Rule(5, new[] { 16,4 }),          // L -> > A
                new Rule(5, new[] { 17,4 }),          // L -> < A
                new Rule(6, new[] { 18,4 }),          // R -> - A
            };


            GrammarLoader loader = new GrammarLoader("words.txt", "rules.txt");


            for (int wordIndex = 0; wordIndex < loader.Words.Length; wordIndex++)
            {
                Assert.AreEqual(expectedWords[wordIndex].Value, loader.Words[wordIndex].Value);
                Assert.AreEqual(expectedWords[wordIndex].Number, loader.Words[wordIndex].Number);
                Assert.AreEqual(expectedWords[wordIndex].Temp, loader.Words[wordIndex].Temp);
            }

            for (int ruleIndex = 0; ruleIndex < loader.Rules.Length; ruleIndex++)
            {
                Assert.AreEqual(
                    expectedRules[ruleIndex].RuleWordNumber,
                    loader.Rules[ruleIndex].RuleWordNumber,
                    $"В правиле {ruleIndex} не сошелся номер правила"
                    );
                for (int i = 0; i < loader.Rules[ruleIndex].RuleList.Length; i++)
                {
                    Assert.AreEqual(
                        loader.Rules[ruleIndex].RuleList[i],
                        expectedRules[ruleIndex].RuleList[i],
                        $"В правиле {ruleIndex} не сошелся порядок правил по индексу {i}"
                        );
                }
            }
        }
Exemplo n.º 16
0
        public void CheckWordsLoadWithRulesTest()
        {
            Word[] expectedWords =
            {
                new Word(0,  "S"),
                new Word(1,  "C"),
                new Word(2,  "W"),
                new Word(3,  "X"),
                new Word(4,  "A"),
                new Word(5,  "L"),
                new Word(6,  "R"),
                new Word(7,  "if"),
                new Word(8,  ":="),
                new Word(9,  "id"),
                new Word(10, "["),
                new Word(11, "]"),
                new Word(12, "const"),
                new Word(13, "sqr"),
                new Word(14, "="),
                new Word(15, "!"),
                new Word(16, ">"),
                new Word(17, "<"),
                new Word(18, "-"),
                new Word(19, "$")
            };

            GrammarLoader loader = new GrammarLoader();

            loader = loader
                     .SetWord("S")
                     .SetWord("C")
                     .SetWord("W")
                     .SetWord("X")
                     .SetWord("A")
                     .SetWord("L")
                     .SetWord("R")
                     .SetWord("if")
                     .SetWord(":=")
                     .SetWord("id")
                     .SetWord("[")
                     .SetWord("]")
                     .SetWord("const")
                     .SetWord("sqr")
                     .SetWord("=")
                     .SetWord("!")
                     .SetWord(">")
                     .SetWord("<")
                     .SetWord("-")
                     .SetWord("$");

            loader = loader
                     .SetRule("S -> C if W")
                     .SetRule("S -> C if W S")
                     .SetRule("C -> L A")
                     .SetRule("W -> X A")
                     .SetRule("W -> X L")
                     .SetRule("X -> := id")
                     .SetRule("X -> := id [ A ]")
                     .SetRule("A -> id")
                     .SetRule("A -> const")
                     .SetRule("A -> id [ A ]")
                     .SetRule("A -> sqr A")
                     .SetRule("A -> R A")
                     .SetRule("L -> = A")
                     .SetRule("L -> ! A")
                     .SetRule("L -> > A")
                     .SetRule("L -> < A")
                     .SetRule("R -> - A");

            for (int wordIndex = 0; wordIndex < loader.Words.Length; wordIndex++)
            {
                Assert.AreEqual(expectedWords[wordIndex].Value, loader.Words[wordIndex].Value);
                Assert.AreEqual(expectedWords[wordIndex].Number, loader.Words[wordIndex].Number);
                Assert.AreEqual(expectedWords[wordIndex].Temp, loader.Words[wordIndex].Temp);
            }
        }
Exemplo n.º 17
0
        public void CheckRulesLoadWithWordsTest()
        {
            Rule[] expectedRules =
            {
                new Rule(0, new[] {  1,  7,  2 }), // S-> C if W
                new Rule(0, new[] {  1,  7,2, 0 }),   // S-> C if W S
                new Rule(1, new[] {  5,4 }),          // C -> L A
                new Rule(2, new[] {  3,4 }),          // W -> X A
                new Rule(2, new[] {  3,5 }),          // W -> X L
                new Rule(3, new[] {  8,9 }),          // X -> := id
                new Rule(3, new[] {  8,  9,10, 4, 11 }), // X -> := id [ A ]
                new Rule(4, new[] {9 }),              // A -> id
                new Rule(4, new[] {12 }),             // A -> const
                new Rule(4, new[] {  9, 10,4, 11 }),  // A -> id [ A ]
                new Rule(4, new[] { 13,4 }),          // A -> sqr A
                new Rule(4, new[] {  6,4 }),          // A -> R A
                new Rule(5, new[] { 14,4 }),           // L -> = A
                new Rule(5, new[] { 15,4 }),          // L -> ! A
                new Rule(5, new[] { 16,4 }),          // L -> > A
                new Rule(5, new[] { 17,4 }),          // L -> < A
                new Rule(6, new[] { 18,4 }),          // R -> - A
            };

            GrammarLoader loader = new GrammarLoader();

            loader = loader
                     .SetWord("S")
                     .SetWord("C")
                     .SetWord("W")
                     .SetWord("X")
                     .SetWord("A")
                     .SetWord("L")
                     .SetWord("R")
                     .SetWord("if")
                     .SetWord(":=")
                     .SetWord("id")
                     .SetWord("[")
                     .SetWord("]")
                     .SetWord("const")
                     .SetWord("sqr")
                     .SetWord("=")
                     .SetWord("!")
                     .SetWord(">")
                     .SetWord("<")
                     .SetWord("-")
                     .SetWord("$");

            loader = loader
                     .SetRule("S -> C if W")
                     .SetRule("S -> C if W S")
                     .SetRule("C -> L A")
                     .SetRule("W -> X A")
                     .SetRule("W -> X L")
                     .SetRule("X -> := id")
                     .SetRule("X -> := id [ A ]")
                     .SetRule("A -> id")
                     .SetRule("A -> const")
                     .SetRule("A -> id [ A ]")
                     .SetRule("A -> sqr A")
                     .SetRule("A -> R A")
                     .SetRule("L -> = A")
                     .SetRule("L -> ! A")
                     .SetRule("L -> > A")
                     .SetRule("L -> < A")
                     .SetRule("R -> - A");

            for (int ruleIndex = 0; ruleIndex < loader.Rules.Length; ruleIndex++)
            {
                Assert.AreEqual(
                    expectedRules[ruleIndex].RuleWordNumber,
                    loader.Rules[ruleIndex].RuleWordNumber,
                    $"В правиле {ruleIndex} не сошелся номер правила"
                    );
                for (int i = 0; i < loader.Rules[ruleIndex].RuleList.Length; i++)
                {
                    Assert.AreEqual(
                        loader.Rules[ruleIndex].RuleList[i],
                        expectedRules[ruleIndex].RuleList[i],
                        $"В правиле {ruleIndex} не сошелся порядок правил по индексу {i}"
                        );
                }
            }
        }