예제 #1
0
        public DecGrammar(DecFile file)
        {
            CompileFile(file);
            GrammarAction.Init();

            InitNonTerminals();
        }
예제 #2
0
        public static void CompileDec(ParsedCommandOptions opts)
        {
            var Dec = new DecFile(@"C:\Users\yveem\source\repos\Redmond\TestDec.dec");

            CompileSettings.InitSettings(Dec.SettingsLines);
            var gram = new DecGrammar(Dec);

            Console.WriteLine("Generating parsing table...");

            ParseFile parseFile = new ParseFile(@"C:\Users\yveem\source\repos\Redmond\TestParse.parse");

            parseFile.SetLexLines(Dec.LexLines);
            parseFile.SetParseTableLines(gram.SerializeParsingTable());
            parseFile.SetTokenIdLines(ProductionEntry.Register.Serialize());
            parseFile.Save();

            Console.WriteLine("Done!");
            Console.Beep();

            var parser = gram.GetParser();

            var input = new MultiFileInputStream(new List <string>(new string[] { @"C:\Users\yveem\source\repos\Redmond\TestInput.txt" }));
            //var input = GetAllCSFiles(@"C:\Users\yveem\source\repos\CompileTestProject");


            TokenStream Input = new TokenStream(input, Dec.LexLines, new string(Enumerable.Range('\x1', 127).Select(i => (char)i).ToArray()));

            parser.Parse(Input);
            var tree = SyntaxTreeNode.CurrentNode;

            Console.WriteLine(tree.ToTreeString());
            Console.WriteLine("============\n");
            new IntermediateGenerator(new ConsoleStream()).Start(tree);
            Console.WriteLine("============\n");
        }
예제 #3
0
        private void CompileFile(DecFile file)
        {
            _tokenNames = new List <string>();
            foreach (var line in file.TokenLines)
            {
                _tokenNames.AddRange(line.Split(' '));
            }


            var lines      = file.GrammarLines;
            int linesIndex = 0;

            CompileGrammarHeader(lines, ref linesIndex);

            string all = "";

            while (linesIndex < lines.Length)
            {
                all += lines[linesIndex++];
            }

            all = all.Replace("\n", "").Replace("\t", " ");

            string firstLhs = "";

            int index = 0;

            while (index < all.Length - 1)
            {
                string lhs = all.ReadUntil(ref index, c => c == ':').Trim();
                index++;
                all.ReadWhile(ref index, c => " \t".Contains(c));
                List <string> prods = new List <string>();

                string prod = "";
                bool   end  = false;
                while (index < all.Length && !end)
                {
                    char c = all[index++];



                    switch (c)
                    {
                    case ';':
                    case '|':
                        if (prod.Length == 0)
                        {
                            prod = GrammarConstants.EmptyChar + "";
                        }
                        prods.Add(prod);
                        prod = "";
                        if (c == ';')
                        {
                            end = true;
                        }
                        break;

                    case '{':
                        index--;
                        prod += c + all.ReadUntilClosingBracket(ref index, '{', '}', true);
                        break;

                    case '\'':
                        string s = all.ReadUntil(ref index, c => c == '\'');
                        index++;
                        prod += '\'' + s + '\'';
                        break;

                    default:
                        prod += c;
                        break;
                    }
                }

                if (firstLhs == "")
                {
                    firstLhs = lhs;
                }

                AddNonTerminal(new NonTerminal(this, lhs, prods.ToArray()));
            }

            startSymbol = new NonTerminal(this, "Start", firstLhs);
            AddNonTerminal(startSymbol);
        }