//词法分析 private void button2_Click(object sender, EventArgs e) { listView1.GridLines = true; //添加表头(列) listView1.Clear(); listView1.Columns.Add("NO", 50, HorizontalAlignment.Center); listView1.Columns.Add("KEY", 100, HorizontalAlignment.Center); listView1.Columns.Add("ID", 100, HorizontalAlignment.Center); int i = 1; foreach (String line in richTextBox1.Lines) { String output = String.Empty; output += ma.analysis(line); while (ma.token.Count > 0) { myanalysis.Data d = ma.token.Dequeue(); ListViewItem item = new ListViewItem(); item.SubItems.Clear(); item.SubItems[0].Text = i.ToString(); item.SubItems.Add(d.value); string str = d.name.Remove(0, 3); item.SubItems.Add(str); listView1.Items.Add(item); i++; } } }
//代码高亮 private void button3_Click(object sender, EventArgs e) { richTextBox3.Text = String.Empty; foreach (String line in richTextBox1.Lines) { String output = String.Empty; output += ma.analysis(line); if (line == String.Empty) { richTextBox3.AppendText("\n"); continue; } for (int i = 0; i < line.Length; i++) { if (line[i] == '\t' || line[i] == '\r' || line[i] == '\n' || line[i] == 32) { richTextBox3.AppendText(line[i].ToString()); } else { break; } } while (ma.token.Count > 0) { myanalysis.Data d = ma.token.Dequeue(); if (d.value == "//" || d.value == "/*" || d.value == "*/") { continue; } int start = richTextBox3.TextLength; richTextBox3.AppendText(d.value + " "); richTextBox3.Select(start, start + d.value.Length); if ((int)d.code <= 4) { richTextBox3.SelectionColor = Color.Blue; } else if ((int)d.code <= 41) { richTextBox3.SelectionColor = Color.Red; } else { richTextBox3.SelectionColor = Color.Green; } } richTextBox3.AppendText("\n"); } }