Exemple #1
0
        public void GeneratorTest()
        {
            var generator = new Generator(PascalDefinition.ProductionRules);
            var slr1table = generator.Generate(PascalDefinition.NonTerminalKey.Start);
            var driver    = new Slr1Driver(slr1table);
            List <LexicalElement> lexicalElements = new List <LexicalElement>();

            using (var file = new FileStream("test_source1.txt", FileMode.Open)) {
                var reader = new StreamReader(file);
                var l      = new LexerStateMachine(reader);
                l.AdvanceChar();
                LexicalElement le;
                while ((le = l.NextToken()) != null)
                {
                    if (le is LineFeedElement)
                    {
                        continue;
                    }
                    lexicalElements.Add(le);
                }
            }
            var q        = new Queue <LexicalElement>(lexicalElements);
            var treeRoot = driver.Parse(q,
                                        CommonUtils.Closure(
                                            new HashSet <Item>()
            {
                new Item()
                {
                    ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0
                }
            },
                                            driver.ProductionDictionary), typeof(SNode), new List <ParserConfiguration>());

            Assert.IsInstanceOfType(treeRoot, typeof(SNode));
        }
Exemple #2
0
        public void NumberTest()
        {
            LexicalElement        a;
            List <LexicalElement> res = new List <LexicalElement>();

            using (var ms = StringToMemoryStream("123 0123 00101 00125 00768 002f1 0x123 1.234"))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    while ((a = l.NextToken()) != null)
                    {
                        res.Add(a);
                    }
                }
            }
            Assert.AreEqual(8, res.Count);
            Assert.AreEqual(123, (res[0] as IntegerLiteral).Value);
            Assert.AreEqual(83, (res[1] as IntegerLiteral).Value);
            Assert.AreEqual(0b101, (res[2] as IntegerLiteral).Value);
            Assert.AreEqual(85, (res[3] as IntegerLiteral).Value);
            Assert.AreEqual(768, (res[4] as IntegerLiteral).Value);
            Assert.AreEqual(0x2f1, (res[5] as IntegerLiteral).Value);
            Assert.AreEqual(0x123, (res[6] as IntegerLiteral).Value);
            Assert.AreEqual(1.234, (res[7] as RealLiteral).Value);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            LinkedList <LexicalElement> result = new LinkedList <LexicalElement>();
            LinkedList <KeyValuePair <string, IdentifierElement> > symbolTable =
                new LinkedList <KeyValuePair <string, IdentifierElement> >();

            try {
                if (args.Length != 1)
                {
                    var ms     = new MemoryStream();
                    var writer = new StreamWriter(ms);
                    int ch;
                    while ((ch = Console.Read()) != -1)
                    {
                        writer.Write(Convert.ToChar(ch));
                    }

                    writer.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    LexerStateMachine l = new LexerStateMachine(new StreamReader(ms));
                    l.AdvanceChar();
                    LexicalElement le;
                    while ((le = l.NextToken()) != null)
                    {
                        result.AddLast(le);
                    }
                }
                else
                {
                    string filename = args[0];

                    using (var fs = new FileStream(filename, FileMode.Open)) {
                        using (var ss = new StreamReader(fs)) {
                            LexerStateMachine l = new LexerStateMachine(ss);
                            l.AdvanceChar();
                            LexicalElement le;
                            while ((le = l.NextToken()) != null)
                            {
                                result.AddLast(le);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                Console.WriteLine("Error: ");
                Console.WriteLine(e.Message);
                return;
            }

            BuildSymbolTable(symbolTable, result);
            PrintLexeme(result);
            Console.WriteLine();
            PrintSymbolTable(symbolTable);
            return;
        }
Exemple #4
0
        public void StringMalformedTest1()
        {
            LexicalElement a;

            using (var ms = StringToMemoryStream("\"abc\\nab\\\""))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    a = l.NextToken();
                }
            }
        }
Exemple #5
0
        public void StringEscapeTest1()
        {
            LexicalElement a;

            using (var ms = StringToMemoryStream("\"abc\\nab\\\"\"")) {
                using (var reader = new StreamReader(ms)) {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    a = l.NextToken();
                }
            }
            Assert.IsInstanceOfType(a, typeof(StringLiteral));
            Assert.AreEqual("abc\nab\"", (a as StringLiteral).Value);
        }
Exemple #6
0
 public void CommentTest()
 {
     using (var ms = StringToMemoryStream("{ F**k }"))
     {
         using (var reader = new StreamReader(ms))
         {
             var l = new LexerStateMachine(reader);
             l.AdvanceChar();
             Assert.IsNull(l.NextToken());
         }
     }
     using (var ms = StringToMemoryStream("{ F**k \n F**k }"))
     {
         using (var reader = new StreamReader(ms))
         {
             var l = new LexerStateMachine(reader);
             l.AdvanceChar();
             Assert.IsNull(l.NextToken());
         }
     }
 }
Exemple #7
0
        public void LexTest()
        {
            /*var l = new Lexer1();
             * LinkedList<LexicalElement> result;
             * using (var fileStream = new FileStream("source.txt", FileMode.Open)) {
             *  result = l.Lex(new StreamReader(fileStream));
             * }*/
            LinkedList <LexicalElement> res = new LinkedList <LexicalElement>();

            using (var fs = new FileStream("source.txt", FileMode.Open)) {
                using (var ss = new StreamReader(fs)) {
                    var l = new LexerStateMachine(ss);
                    l.AdvanceChar();
                    LexicalElement le;
                    while ((le = l.NextToken()) != null)
                    {
                        res.AddLast(le);
                    }
                }
            }
            Assert.AreEqual(44, res.Count);
        }
Exemple #8
0
        public void MalformedNumberTest()
        {
            LexicalElement        a;
            List <LexicalElement> res = new List <LexicalElement>();

            using (var ms = StringToMemoryStream("0012k"))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    Assert.ThrowsException <LexicalException>(() => {
                        l.NextToken();
                    });
                }
            }
            using (var ms = StringToMemoryStream("0123f"))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    Assert.ThrowsException <LexicalException>(() => {
                        l.NextToken();
                    });
                }
            }
            using (var ms = StringToMemoryStream("0x12k"))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    Assert.ThrowsException <LexicalException>(() => {
                        l.NextToken();
                    });
                }
            }
            using (var ms = StringToMemoryStream("0.213.213"))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    Assert.ThrowsException <LexicalException>(() => {
                        l.NextToken();
                    });
                }
            }
            using (var ms = StringToMemoryStream("28743685636583658698"))
            {
                using (var reader = new StreamReader(ms))
                {
                    var l = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    Assert.ThrowsException <LexicalException>(() => {
                        l.NextToken();
                    });
                }
            }
        }
        private void ExecuteCode()
        {
            var                    globals = new Dictionary <string, GCHandle>();
            LLVMModuleRef          mod;
            LLVMExecutionEngineRef engine = null;

            try {
                SymbolTable.IsReadOnly = true;
                mod = LLVMModuleRef.CreateWithName("main");
                var options = LLVMMCJITCompilerOptions.Create();
                engine = mod.CreateMCJITCompiler(ref options);
                var builder      = mod.Context.CreateBuilder();
                var globalSource = SymbolTable.ItemsSource as ICollection <SymbolEntry>;
                var globalStore  = new Dictionary <string, object>();
                foreach (var k in globalSource)
                {
                    globalStore.Add(k.Name, k.Value);
                }

                foreach (var kv in globalStore)
                {
                    globals.Add(kv.Key, GCHandle.Alloc(kv.Value, GCHandleType.Pinned));
                }

                var fpm = mod.CreateFunctionPassManager();
                fpm.AddPromoteMemoryToRegisterPass();
                fpm.AddInstructionCombiningPass();
                fpm.AddReassociatePass();
                fpm.AddGVNPass();
                fpm.AddCFGSimplificationPass();
                fpm.InitializeFunctionPassManager();
                var symT     = ProcessGlobals(mod, engine, globals);
                var lexicals = new List <LexicalElement>();
                try {
                    using (var ms = new MemoryStream()) {
                        var writer = new StreamWriter(ms);
                        writer.Write(Editor.Document.CurrentSnapshot.GetText(LineTerminator.Newline));
                        writer.Flush();
                        ms.Seek(0L, SeekOrigin.Begin);
                        var reader = new StreamReader(ms);
                        var l      = new LexerStateMachine(reader);
                        l.AdvanceChar();
                        LexicalElement t;
                        while ((t = l.NextToken()) != null)
                        {
                            if (t is LineFeedElement)
                            {
                                continue;
                            }
                            lexicals.Add(t);
                        }
                    }
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "词法错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                var        exceptions = new List <SyntaxException>();
                SyntaxNode treeRoot;
                try {
                    treeRoot = slr1Driver.Parse(new Queue <LexicalElement>(lexicals),
                                                CommonUtils.Closure(
                                                    new HashSet <Item>()
                    {
                        new Item()
                        {
                            ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0
                        }
                    },
                                                    generator.ProductionDict), typeof(SNode), null, exceptions);
                    if (exceptions.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append($"语法分析器共检测到{exceptions.Count}个错误\n\n");
                        foreach (var exception in exceptions)
                        {
                            sb.Append(exception.Message);
                            sb.Append('\n');
                        }

                        MessageBox.Show(sb.ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                } catch (SyntaxException ex) {
                    StringBuilder sb = new StringBuilder();
                    sb.Append($"语法分析器共检测到{exceptions.Count}个错误,无法恢复\n\n");
                    foreach (var exception in exceptions)
                    {
                        sb.Append(exception.Message);
                        sb.Append('\n');
                    }

                    MessageBox.Show(sb.ToString(), $"语法分析错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                var translator = new LLVMTranslator.LLVMTranslator(mod, engine, builder, symT);
                translator.Visit(treeRoot);
                fpm.RunFunctionPassManager(translator.func);
                IrBox.Text = mod.PrintToString();
                PM main = engine.GetPointerToGlobal <PM>(translator.func);
                main();
                foreach (var symbolEntry in globalSource)
                {
                    symbolEntry.Value = (int)globalStore[symbolEntry.Name];
                }
            } catch (Exception e) {
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                SymbolTable.IsReadOnly = false;
                if (engine != null)
                {
                    engine.Dispose();
                }
                foreach (var kv in globals)
                {
                    kv.Value.Free();
                }
            }
        }
Exemple #10
0
        public void LLVMTest2()
        {
            LLVM.LinkInMCJIT();
            LLVM.InitializeX86TargetMC();
            LLVM.InitializeX86Target();
            LLVM.InitializeX86TargetInfo();
            LLVM.InitializeX86AsmParser();
            LLVM.InitializeX86AsmPrinter();
            var mod     = LLVMModuleRef.CreateWithName("Test1");
            var options = LLVMMCJITCompilerOptions.Create();

            options.NoFramePointerElim = 1;
            var engine   = mod.CreateMCJITCompiler(ref options);
            var builder  = mod.Context.CreateBuilder();
            var lexicals = new List <LexicalElement>();
            var globals  = new LLVMSymbolTable(null);

            globals.Add("z", mod.AddGlobal(LLVMTypeRef.Int32, "z"));
            object z      = 0;
            var    handle = GCHandle.Alloc(z, GCHandleType.Pinned);

            engine.AddGlobalMapping(globals["z"], handle.AddrOfPinnedObject());
            using (var fs = new FileStream("D:\\Repos\\PascalCompiler\\SyntaxAnalyzerTest\\test_source2.txt", FileMode.Open)) {
                using (var ss = new StreamReader(fs)) {
                    var l = new LexerStateMachine(ss);
                    l.AdvanceChar();
                    LexicalElement t;
                    while ((t = l.NextToken()) != null)
                    {
                        if (t is LineFeedElement)
                        {
                            continue;
                        }
                        lexicals.Add(t);
                    }
                }
            }
            var        history    = new List <ParserConfiguration>();
            var        exceptions = new List <SyntaxException>();
            SyntaxNode treeRoot;
            var        generator = new Generator(PascalDefinition.ProductionRules);
            var        slr1Table = generator.Generate(PascalDefinition.NonTerminalKey.Start);

            slr1Table.AllowedErrorRecoveryKey.Add(PascalDefinition.NonTerminalKey.Statement, () => new StatementNode(new SyntaxNode[0]));
            var slr1Driver = new Slr1Driver(slr1Table);

            treeRoot = slr1Driver.Parse(new Queue <LexicalElement>(lexicals),
                                        CommonUtils.Closure(
                                            new HashSet <Item>()
            {
                new Item()
                {
                    ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0
                }
            },
                                            generator.ProductionDict), typeof(SNode), history, exceptions);
            var translator =
                new LLVMTranslator.LLVMTranslator(mod, engine, builder, globals);

            translator.Visit(treeRoot);
            if (!mod.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error))
            {
                Trace.WriteLine(error);
            }
            Trace.WriteLine(mod.PrintToString());
            PM a = engine.GetPointerToGlobal <PM>(translator.func);

            a();
            builder.Dispose();
            engine.Dispose();
            handle.Free();
        }
Exemple #11
0
        private void TranslateLR(object sender, RoutedEventArgs e)
        {
            var lexicals = new List <LexicalElement>();

            try
            {
                using (var ms = new MemoryStream())
                {
                    var writer = new StreamWriter(ms);
                    writer.Write(Editor.Document.CurrentSnapshot.GetText(LineTerminator.Newline));
                    writer.Flush();
                    ms.Seek(0L, SeekOrigin.Begin);
                    var reader = new StreamReader(ms);
                    var l      = new LexerStateMachine(reader);
                    l.AdvanceChar();
                    LexicalElement t;
                    while ((t = l.NextToken()) != null)
                    {
                        if (t is LineFeedElement)
                        {
                            continue;
                        }
                        lexicals.Add(t);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "词法错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            var        history    = new List <ParserConfiguration>();
            var        exceptions = new List <SyntaxException>();
            SyntaxNode treeRoot;

            try
            {
                var translator = new TranslatorLR();
                treeRoot = slr1Driver.Parse(new Queue <LexicalElement>(lexicals),
                                            CommonUtils.Closure(
                                                new HashSet <Item>()
                {
                    new Item()
                    {
                        ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0
                    }
                },
                                                generator.ProductionDict), typeof(SNode), history, exceptions, translator);
                if (exceptions.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append($"语法分析器共检测到{exceptions.Count}个错误\n\n");
                    foreach (var exception in exceptions)
                    {
                        sb.Append(exception.Message);
                        sb.Append('\n');
                    }

                    MessageBox.Show(sb.ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    new SyntaxTreeView(treeRoot).Show();
                    var cl = Translator.ResolveLabels(translator.properties[treeRoot].code);
                    new GarbageTranslatorView(cl).Show();
                    if (translator.Warnings.Count != 0)
                    {
                        MessageBox.Show(String.Join("\n", translator.Warnings), "警告", MessageBoxButton.OK,
                                        MessageBoxImage.Warning);
                    }
                }
            }
            catch (SyntaxException ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append($"语法分析器共检测到{exceptions.Count}个错误,无法恢复\n\n");
                foreach (var exception in exceptions)
                {
                    sb.Append(exception.Message);
                    sb.Append('\n');
                }
                MessageBox.Show(sb.ToString(), $"语法分析错误", MessageBoxButton.OK, MessageBoxImage.Error);
                //return;
            }
            new AnalyzerHistory(history, slr1Driver.Slr1Table).Show();
        }