📄 form1.cs
字号:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Antlr.Runtime.Collections;
using Antlr.Runtime.Debug;
using Antlr.Runtime.Tree;
using Antlr.Runtime.Misc;
using Antlr.StringTemplate;
using Antlr.Runtime;
using Antlr.Utility;
using regularExpression = System.Text.RegularExpressions;
using System.Collections;
namespace syn
{
public partial class Form1 : Form
{
//static int ds = 0;
public Form1()
{
InitializeComponent();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
//{
// StreamReader st=new StreamReader(openFileDialog1.FileName);
// richTextBoxReadin.Text = st.ReadToEnd();
//}
}
private void buttonRead_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader st = new StreamReader(openFileDialog1.FileName);
richTextBoxReadin.Text = st.ReadToEnd();
}
}
//string analyse
public static ArrayList subStringCollection;
public void Lexer()//词法分析
{//FontDialog fg=new FontDialog();fg.Color=Color.Green;
Boolean isComment = true;
//Boolean isComment1 = true;
string str = "";
subStringCollection = new ArrayList();
string text = richTextBoxReadin.Text;
string pat = @"([!][=])|([&][&])|([|][|])" + @"|([|!#%&])|([:;,.'])" + @"|([/][/].*)" + @"|[/][*]|[*][/]" + @"|([(){}])" + @"|([[])|([]])" + @"|([+][+])|([-][-])|([=][=])|([>][=])|([<][=])|([+-])|([1-9]*[0-9]+[.][0-9]+)|[0]|([1-9][0-9]*)|([*/><=])|([a-zA-Z_]+[a-zA-Z0-9_]*)|" + @"([""][\s\S]*[""])" + @"|([\\][\\])" + @"|([\\])";
richTextBoxAnaliseOut.Text = "";
regularExpression.Regex re = new regularExpression.Regex(pat, regularExpression.RegexOptions.Compiled);
regularExpression.Match mat = re.Match(text);
while (mat.Success)
{
if (mat.Value.Contains(@"/*"))
{
isComment = false;
//isComment1 = false;
}
str=mat.Value;
if (str.Contains(@"*/"))
{
if (isComment == false)
str = "";
isComment = true;
}
if (isComment && !str.Equals("")&& !str.Contains(@"//") )
{
if (CategoryCode(str) < 12 ||str.Equals(@"\#"))
{
richTextBoxReadin.Select(mat.Index, str.Length);
richTextBoxReadin.SelectionColor = Color.Blue;
}
subStringCollection.Add(str);
if(!str.Equals(@"*/"))
richTextBoxAnaliseOut.Text += CategoryCode(str) + "\t" + str + "\n";
}
mat = mat.NextMatch();
}
}
private void buttonAnalyse_Click(object sender, EventArgs e)
{
Lexer();
}
//add CategoryCodes
public static int CategoryCode(String charCollection)
{
switch (charCollection)
{
case "else": return 0;
case "void": return -1;
case "include": return -2;
case "main": return 1;
case "int": return 2;
case "float": return 3;
case "printf": return 4;
case "if": return 5;
case "for": return 6;
case "while": return 7;
case "do": return 8;
case "return": return 9;
case "break": return 10;
case "continue": return 11;
case "+": return 12;
case "-": return 13;
case "*": return 14;
case "/": return 15;
case ">": return 16;
case "<": return 17;
case ">=": return 18;
case "<=": return 19;
case "!=": return 20;
case "==": return 21;
case "++": return 22;
case "--": return 23;
case "=": return 24;
case "(": return 25;
case ")": return 26;
case "{": return 27;
case "}": return 28;
case "[": return 29;
case "]": return 30;
case "'": return 31;
case ",": return 32;
case ".": return 33;
case ";": return 34;
case ":": return 35;
case "//": return 36;
case @"\": return 37;
case @"\\": return 38;
case "&": return 39;
case "&&": return 40;
case "|": return 41;
case "||": return 42;
case "!": return 43;
case @"%": return 44;
case "#": return 45;
default: return 46;
}
}
int isSuccessParser = 0;
private void buttonParser_Click(object sender, EventArgs e)
{
Lexer();
if (subStringCollection.Count > 12)
{
syntaxAnalyser parser = new syntaxAnalyser(richTextBoxStatus);
parser.AnalyseCode();
isSuccessParser = parser.isSuccess;
}
else
richTextBoxStatus.Text+="the code is not complete!\n";
}
private void button3_Click(object sender, EventArgs e)
{
if (isSuccessParser == 0)
MessageBox.Show("parsing is not passed!");
else
{
semAnalysis sem = new semAnalysis();
sem.IsVarible();
richTextBoxStatus.Text += "\n已定义变量:" + sem.VariableStack;
richTextBoxStatus.Text += "\n重定义变量:" + sem.reDefine;
richTextBoxStatus.Text += "\n未定义变量:" + sem.unDefine;
}
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show("have not yet to implement!");
}
private void richTextBoxStatus_TextChanged(object sender, EventArgs e)
{
richTextBoxStatus.Text = "";
}
private void richTextBoxReadin_TextChanged(object sender, EventArgs e)
{
richTextBoxReadin.Text = "";
}
private void richTextBoxAnaliseOut_TextChanged(object sender, EventArgs e)
{
richTextBoxAnaliseOut.Text = "";
}
private void button5_Click(object sender, EventArgs e)
{
richTextBoxStatus.Text = "";
}
private void button7_Click(object sender, EventArgs e)
{
Form form1 = new Form();
RichTextBox rich = new RichTextBox();
rich.Text = richTextBoxStatus.Text;
rich.Dock = System.Windows.Forms.DockStyle.Fill;
form1.Controls.Add(rich);
form1.Show();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -