Пример #1
0
        public MainWindow()
        {
            InitializeComponent();

            var asmb     = Assembly.GetExecutingAssembly();
            var xshdfile = asmb.GetName().Name + ".Code.xshd";

            using (var s = asmb.GetManifestResourceStream(xshdfile))
            {
                using (var reader = new XmlTextReader(s))
                {
                    CodeEditor.SyntaxHighlighting           = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                    CodeEditor_Converted.SyntaxHighlighting = CodeEditor.SyntaxHighlighting;
                }
            }

            DataGrid_DFAPriorityTable.ItemsSource = DataGrid_DFAPriorityTableData;
            DataGrid_LexerTestResult.ItemsSource  = DataGrid_LexerTestResultData;

            CodeEditor.TextArea.TextEntering += CodeEditor_TextArea_TextEntering;
            CodeEditor.TextArea.TextEntered  += TextArea_TextEntered;

            taskFlowManager.TaskResultUpdated += TaskFlowManager_TaskResultUpdated;
            taskFlowManager.TaskResultCleared += TaskFlowManager_TaskResultCleared;

            taskFlowManager.AddTask <DataSource_FromCodeEditor>();
            taskFlowManager.AddTask <Production_Lexer>();
            taskFlowManager.AddTask <Production_Parser>();
            taskFlowManager.AddTask <NFAGenerator_Lexer>();
            taskFlowManager.AddTask <NFAGenerator_Parser>();
            taskFlowManager.AddTask <DFAGenerator>();
            taskFlowManager.AddTask <DFAOptimizer>();
            taskFlowManager.AddTask <DFAPriorityGenerator>();
            taskFlowManager.AddTask <DataSource_FromGrammarComboBoxs>();
            taskFlowManager.AddTask <GrammarCompiler>();

            taskFlowManager.GetTask <DataSource_FromCodeEditor>().BindEditor(CodeEditor);

            CheckBox_NFAGeneratorImageEnable.IsChecked = taskFlowManager.GetTask <NFAGenerator_Parser>().GetImageOutputEnable();
            CheckBox_DFAGeneratorImageEnable.IsChecked = taskFlowManager.GetTask <DFAGenerator>().GetImageOutputEnable();
            CheckBox_DFAOptimizerImageEnable.IsChecked = taskFlowManager.GetTask <DFAOptimizer>().GetImageOutputEnable();

            var grammar_compiler_list = new GrammarCompilerCollection();

            grammar_compiler_list.Add(new GrammarCompiler_LL_1());

            taskFlowManager.GetTask <DataSource_FromGrammarComboBoxs>().Init(grammar_compiler_list, ComboBox_GrammarMajorType, ComboBox_GrammarMinorType);
        }
Пример #2
0
 private void TaskFlowManager_TaskResultCleared(TaskFlowManager Sender, Type TaskType)
 {
     if (TaskType == typeof(Production_Lexer))
     {
         TextBox_Info.Text         = Sender.GetTask <Production_Lexer>().ErrorMsg;
         CodeEditor_Converted.Text = "";
     }
     else if (TaskType == typeof(Production_Parser))
     {
         TextBox_Info.Text        += Sender.GetTask <Production_Parser>().ErrorMsg;
         CodeEditor_Converted.Text = "";
     }
     else if (TaskType == typeof(NFAGenerator_Lexer))
     {
         TextBox_Info.Text += Sender.GetTask <NFAGenerator_Lexer>().ErrorMsg;
     }
     else if (TaskType == typeof(NFAGenerator_Parser))
     {
         TextBox_Info.Text += Sender.GetTask <NFAGenerator_Parser>().ErrorMsg;
         ComboBox_NFA_RegularExpress.Items.Clear();
         Image_NFA_Diagram.Source = null;
     }
     else if (TaskType == typeof(DFAPriorityGenerator))
     {
         TextBox_Info.Text += Sender.GetTask <DFAPriorityGenerator>().ErrorMsg;
         DataGrid_DFAPriorityTableData.Clear();
     }
     else if (TaskType == typeof(GrammarCompiler))
     {
         TextBox_Info.Text += Sender.GetTask <GrammarCompiler>().ErrorText;
     }
 }
Пример #3
0
        private void TaskFlowManager_TaskResultUpdated(TaskFlowManager Sender, Type TaskType)
        {
            if (TaskType == typeof(Production_Lexer))
            {
                TextBox_Info.Text = "[Production_Lexer]:Execute OK!\n";
            }
            else if (TaskType == typeof(Production_Parser))
            {
                TextBox_Info.Text        += "[Production_Parser]:Execute OK!\n";
                CodeEditor_Converted.Text = Sender.GetTask <Production_Parser>().ProductionCode;
            }
            else if (TaskType == typeof(NFAGenerator_Lexer))
            {
                TextBox_Info.Text += "[NFAGenerator_Lexer]:Execute OK!\n";
            }
            else if (TaskType == typeof(NFAGenerator_Parser))
            {
                ComboBox_NFA_RegularExpress.Items.Clear();
                Image_NFA_Diagram.Source = null;
                var nfaparser   = Sender.GetTask <NFAGenerator_Parser>();
                var resultimage = nfaparser.ResultImage;
                var resultdata  = nfaparser.Result;

                foreach (var item in resultdata.Production_ParserResult.tplist)
                {
                    ComboBox_NFA_RegularExpress.Items.Add("<" + item.Name + "> -> \"" + item.RegularExpression + "\"");
                }

                NFAImage = resultimage;

                if (ComboBox_NFA_RegularExpress.Items.Count > 0)
                {
                    ComboBox_NFA_RegularExpress.SelectedIndex = 0;
                }

                TextBox_Info.Text += "[NFAGenerator_Parser]:Execute OK!\n";
            }
            else if (TaskType == typeof(DFAGenerator))
            {
                ComboBox_DFA_RegularExpress.Items.Clear();
                Image_DFA_Diagram.Source = null;
                var dfagenerator = Sender.GetTask <DFAGenerator>();
                var resultimage  = dfagenerator.ResultImage;
                var resultdata   = dfagenerator.Result;

                foreach (var item in resultdata.Production_ParserResult.tplist)
                {
                    ComboBox_DFA_RegularExpress.Items.Add("<" + item.Name + "> -> \"" + item.RegularExpression + "\"");
                }

                DFAImage = resultimage;

                if (ComboBox_DFA_RegularExpress.Items.Count > 0)
                {
                    ComboBox_DFA_RegularExpress.SelectedIndex = 0;
                }

                TextBox_Info.Text += "[DFAGenerator]:Execute OK!\n";
            }
            else if (TaskType == typeof(DFAOptimizer))
            {
                ComboBox_DFAOptimized_RegularExpress.Items.Clear();
                Image_DFAOptimized_Diagram.Source = null;
                var dfaoptimizer = Sender.GetTask <DFAOptimizer>();
                var resultimage  = dfaoptimizer.ResultImage;
                var resultdata   = dfaoptimizer.Result;

                foreach (var item in resultdata.Production_ParserResult.tplist)
                {
                    ComboBox_DFAOptimized_RegularExpress.Items.Add("<" + item.Name + "> -> \"" + item.RegularExpression + "\"");
                }

                DFAOptimizedImage = resultimage;

                if (ComboBox_DFAOptimized_RegularExpress.Items.Count > 0)
                {
                    ComboBox_DFAOptimized_RegularExpress.SelectedIndex = 0;
                }

                TextBox_Info.Text += "[DFAOptimizer]:Execute OK!\n";
            }
            else if (TaskType == typeof(DFAPriorityGenerator))
            {
                TextBox_Info.Text += "[DFAPriorityGenerator]:Execute OK!\n";
                var dfaprioritygenerator = Sender.GetTask <DFAPriorityGenerator>();
                DataGrid_DFAPriorityTableData.Clear();

                foreach (var dfa in dfaprioritygenerator.Result.Item.NoLoopDFAList)
                {
                    var tinfo = dfaprioritygenerator.Result.Production_ParserResult.tplist[dfa.TerminalID];
                    DataGrid_DFAPriorityTableData.Add(new DataGrid_DFAPriorityTable_Item {
                        Name = tinfo.Name, RegularExpression = tinfo.RegularExpression, IsLoop = "否", Priority = "高"
                    });
                }

                foreach (var dfa in dfaprioritygenerator.Result.Item.LoopDFAList)
                {
                    var tinfo = dfaprioritygenerator.Result.Production_ParserResult.tplist[dfa.TerminalID];
                    DataGrid_DFAPriorityTableData.Add(new DataGrid_DFAPriorityTable_Item {
                        Name = tinfo.Name, RegularExpression = tinfo.RegularExpression, IsLoop = "是", Priority = "低"
                    });
                }
            }
            else if (TaskType == typeof(GrammarCompiler))
            {
                TextBox_Info.Text += Sender.GetTask <GrammarCompiler>().ErrorText;
                WebBrowser_GrammarTest_Compile_Result.NavigateToString(Sender.GetTask <GrammarCompiler>().GrammarPreInfo.GrammarCompiler.GetCompileResult());
            }
        }
Пример #4
0
        public bool LexerAnalysis(string Code, out List <LexerWordInfo> WordInfo, out string ErrorText)
        {
            if (Code == "")
            {
                WordInfo  = new List <LexerWordInfo>();
                ErrorText = "";
                return(true);
            }

            WordInfo = new List <LexerWordInfo>();
            var dfapg      = taskFlowManager.GetTask <DFAPriorityGenerator>();
            var nolooplist = dfapg.Result.Item.NoLoopDFAList;
            var looplist   = dfapg.Result.Item.LoopDFAList;
            var tplist     = dfapg.Result.Production_ParserResult.tplist;

            var curindex = 0;

            while (curindex < Code.Length)
            {
                var length  = 0;
                var dfaitem = new DFAItem();
                var ishigh  = true;
                var matched = false;

                foreach (var dfa in nolooplist)
                {
                    if (MatchDFA(Code, curindex, dfa.StartNode, out length))
                    {
                        matched = true;
                        ishigh  = true;
                        dfaitem = dfa;
                        break;
                    }
                }

                if (!matched)
                {
                    foreach (var dfa in looplist)
                    {
                        if (MatchDFA(Code, curindex, dfa.StartNode, out length))
                        {
                            matched = true;
                            ishigh  = false;
                            dfaitem = dfa;
                            break;
                        }
                    }
                }

                if (!matched)
                {
                    ErrorText = "在字符" + curindex + "\"" + Code[curindex] + "\"处分析失败:无匹配DFA!";
                    return(false);
                }
                else
                {
                    if (tplist[dfaitem.TerminalID].Name != "BlankChar")//过滤空字符
                    {
                        WordInfo.Add(new LexerWordInfo {
                            String = Code.Substring(curindex, length), IsHighPriorityDFA = ishigh, DFA = dfaitem
                        });
                    }

                    curindex += length;
                }
            }

            ErrorText = "";
            return(true);
        }