public string clsHandle(string exp) { string result = "exp"; char[] compair = { '+', '-', '*', '/' }; string[] exptmp = exp.Split(','); List <string> rescom = new List <string>(); foreach (var item in exptmp) { #region 进入四则运算判断 if (item.IndexOfAny(compair, 0) > -1) { try { GrammerAnalyzer ga = new GrammerAnalyzer(item); ga.Analyze(); Token[] toks = ga.TokenList; SyntaxAnalyzer sa = new SyntaxAnalyzer(toks); sa.Analyze(); Calculator calc = new Calculator(sa.SyntaxTree); double value = calc.Calc(); rescom.Add(value.ToString());//加入到list中 } catch { } } else { rescom.Add(item.ToString()); } #endregion } result = string.Join(",", rescom); return(result); }
/// <summary> /// 开始执行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void executeToolStripMenuItem_Click(object sender, EventArgs e) { ClearText(); //输入内容不为空 if (!inputRichTextBox.Text.Equals("")) { AddInformation("开始进行词法分析..."); //先进行词法分析 lexicalRichTextBox.Clear(); errorListView.Items.Clear(); string content = inputRichTextBox.Text; TokenBuilder tb = new TokenBuilder(content); while (tb.hasMoreLines()) { lexicalRichTextBox.AppendText(tb.getCurrenLineNumber() + ": " + tb.getCurrentLine() + "\n"); foreach (Token t in tb.nextTokens()) { if (t.GetTokenType() != TokenType.ANNOTATION) { lexicalRichTextBox.AppendText("\t" + t.ToString() + "\n"); } } } AddInformation("词法分析结束..."); //存在词法分析错误 if (tb.hasLexicalError()) { AddInformation("存在词法错误等待修改..."); MessageBox.Show("程序存在错误, 请先检查", "Interpreter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); List <Error> errors = tb.Errors; for (int i = 0; i < errors.Count; i++) { Error error = errors[i]; ListViewItem item = new ListViewItem(new String[] { error.Dec, error.LineNo + "", i + 1 + "" }); errorListView.Items.Add(item); } } //词法分析不存在错误进行语法分析 else { AddInformation("开始进行语法分析..."); //grammarRichTextBox.Clear(); GrammerAnalyzer ga = new GrammerAnalyzer(content); Node tree = ga.Analyze(); PrintNode(tree, ""); AddInformation("语法分析结束..."); if (ga.HasGrammarError()) { AddInformation("存在语法错误等待修改..."); MessageBox.Show("程序存在错误, 请先检查", "Interpreter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); List <Error> errors = ga.Errors; for (int i = 0; i < errors.Count; i++) { Error error = errors[i]; ListViewItem item = new ListViewItem(new String[] { error.Dec, error.LineNo + "", i + 1 + "" }); errorListView.Items.Add(item); } } //当语法分析不存在错误时进行解释执行 else { AddInformation("开始解释执行..."); intermediateTabPage.Focus(); intermediateTabPage.Show(); String result = ""; intermediateRichTextBox.Clear(); AddInformation("中间代码生成中..."); List <TempCode> TreeNode = new MiddleCode(content).getMiddleCode(); AddInformation("执行..."); new Execute(content).Run(); for (int i = 0; i < TreeNode.Count; ++i) { result += "(" + TreeNode[i].fc.op + "," + TreeNode[i].fc.arg1 + "," + TreeNode[i].fc.arg2 + "," + TreeNode[i].fc.result + ")" + "\n"; } //List<Table> list = new MiddleCode(content).getTable(); //for (int i = 0; i < list.Count; ++i) //{ // result += list[i].str + "," + list[i].value + "," + list[i].type + "\n"; //} intermediateRichTextBox.Text = result; } AddInformation("执行完毕..."); } } //输入程序为空 else { MessageBox.Show("程序存在错误, 请先检查", "Interpreter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } inputRichTextBox.Focus(); }
/// <summary> /// 先进行词法分析,若词法分析无误则进行语法语义分析 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void grammarToolStripMenuItem_Click(object sender, EventArgs e) { ClearText(); //输入内容不为空 AddInformation("开始进行词法分析..."); if (!inputRichTextBox.Text.Equals("")) { lexicalRichTextBox.Clear(); errorListView.Items.Clear(); string content = inputRichTextBox.Text; TokenBuilder tb = new TokenBuilder(content); while (tb.hasMoreLines()) { lexicalRichTextBox.AppendText(tb.getCurrenLineNumber() + ": " + tb.getCurrentLine() + "\n"); foreach (Token t in tb.nextTokens()) { if (t.GetTokenType() != TokenType.ANNOTATION) { lexicalRichTextBox.AppendText("\t" + t.ToString() + "\n"); } } } AddInformation("词法分析结束..."); //存在词法分析错误 if (tb.hasLexicalError()) { AddInformation("词法分析错误..."); MessageBox.Show("程序存在错误, 请先检查", "Interpreter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); List <Error> errors = tb.Errors; for (int i = 0; i < errors.Count; i++) { Error error = errors[i]; ListViewItem item = new ListViewItem(new String[] { error.Dec, error.LineNo + "", i + 1 + "" }); errorListView.Items.Add(item); } } //词法分析不存在错误进行语法分析 else { AddInformation("开始进行语法分析..."); //grammarRichTextBox.Clear(); GrammerAnalyzer ga = new GrammerAnalyzer(content); Node tree = ga.Analyze(); PrintNode(tree, ""); //grammarTabPage.Focus(); //grammarTabPage.Show(); if (ga.HasGrammarError()) { AddInformation("存在语法错误等待修改..."); List <Error> errors = ga.Errors; for (int i = 0; i < errors.Count; i++) { Error error = errors[i]; ListViewItem item = new ListViewItem(new String[] { error.Dec, error.LineNo + "", i + 1 + "" }); errorListView.Items.Add(item); } } } } //输入程序为空 else { MessageBox.Show("程序存在错误, 请先检查", "Interpreter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } inputRichTextBox.Focus(); }