📄 complier.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Specialized;
namespace ComplierForm
{
public partial class Complier : Form
{
private string[,] StringCodeWord;//存放保留字的二维数组
private bool beginString = true; //判断是否为首字符
private string stringPath = null; //存放打开的源文件的路径
private static string stringAdd = ""; //对读入的每个字符进行拼接
private static string[] stringOut=new string[2]; //用于输出模块存放保留字和码值
private StreamWriter sw1 = null; //用于把非法字符存入Error.txt文件
private static int countErrorLine = 0; //用于给出非法字符行的行号
private static bool ErrorString = false;
private static int countChar = 0; //用于给出非法字符的位置
public Complier()
{
InitializeComponent();
}
private void buttonLoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt Files(*.txt)|*.txt|All Files(*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
if ((openFileDialog1.ShowDialog() == DialogResult.OK) && (openFileDialog1.FileName.Length > 0))
{
if ((File.Exists(openFileDialog1.FileName))==true) //如果文件存在
{
buttonComplie.Enabled = true;
stringPath = openFileDialog1.FileName;//为后边建立文件流给出文件路径
richTextBoxSourceFile.Clear();
richTextBoxSourceFile.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
sw1 = File.CreateText(Directory.GetCurrentDirectory()+"\\Error.txt");//进行错误处理
}
}
}
private void Complier_Load(object sender, EventArgs e)
{
#region 把关键字从文件中倒入二维数组中
try
{
buttonComplie.Enabled = false;
listViewCode.GridLines = true; //设置listview的属性
listViewCode.FullRowSelect = true;
listViewCode.AllowColumnReorder = true;
string CodeWordPath = Directory.GetCurrentDirectory() + "//CodeTable.txt";
StreamReader sr = new StreamReader(CodeWordPath);
StringCollection stringcollection = new StringCollection();//无需给出初始值的大小
string oneLine;
while ((oneLine = sr.ReadLine()) != null) //读到文件末尾将返回null
{
stringcollection.Add(oneLine.Trim());
}
sr.Close();
int length = stringcollection.Count;
string[] stringArray=new string[length];
stringcollection.CopyTo(stringArray, 0);
StringCodeWord=new string[length,2]; //此例中,length=35
//为数组赋值
for (int i = 0; i < length; i++)
{
string[] splitString = stringArray[i].Split(' '); //把每行的数据分开存在二维数组中
StringCodeWord[i,0]=splitString[0].Trim();
StringCodeWord[i,1]=splitString[1].Trim();
}
}
catch
{
MessageBox.Show("关键字表文件不存在!","警告!",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
#endregion
}
//拼读一个单词模块scan
private string Scan(char stringChar)
{
// MessageBox.Show(stringAdd);
if (stringChar == '\n')
{
Look("\\n");
if ((stringAdd.Trim().Length != 0) && (stringAdd.Trim() != ";") && (stringAdd.Trim() != ".") && (stringAdd.Trim() != ","))
{
Look(stringAdd);
}
countErrorLine += 1; //用于判断是哪一行出错了,执行到出错行的时候停止编译,给出提示信息
beginString = true;
stringAdd = "";
return stringAdd;
}
//if (((int)stringChar) == 10)
//{
// if ((stringAdd.Trim().Length != 0) && (stringAdd.Trim() != ";") && (stringAdd.Trim() != ".") && (stringAdd.Trim() != ","))
// {
// Look(stringAdd);
// }
// //MessageBox.Show(((int)(stringChar)).ToString());
// countErrorLine += 1; //用于判断是哪一行出错了,执行到出错行的时候停止编译,给出提示信息
// beginString = true;
// stringAdd = "";
// // MessageBox.Show(countErrorLine.ToString());
// return stringAdd;
//}
if(stringChar==' ') //如果是空格的话,把标志为置为true,因为空格后边读入的肯定是个新的字符串
{
if (stringAdd.Trim().Length >0)
{
Look(stringAdd);
}
//清空拼接字符串,以便接受一个新的字符
stringAdd = "";
beginString=true;
return stringAdd;
}
if (beginString ==true) //如果此字符是字符串的第一个字符,分为数字和字母两种情况
{
if(stringChar!=' ')
{
beginString = false;
stringAdd += stringChar; //不管首字符是数字还是字符,都赋给拼接字符串 stringAdd
//MessageBox.Show(stringAdd);
}
}
else if (beginString == false) //如果此字符不是字符串的第一个字符,根据字符决定如何拼接
{
switch(stringChar)
{
case ',': //设置一个标志为来判断此时传来的是否为一个字符串的开始
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look(","); //如果是“,”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case ';':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look(";"); //如果是“;”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case '.':
if (stringAdd.Trim() != "")
{
// MessageBox.Show(stringChar.ToString());
Look(stringAdd);
stringAdd = "";
}
Look("."); //如果是“.”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case '+':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look("+"); //如果是“+”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case '-':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look("-"); //如果是“-”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case '*':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look("*"); //如果是“*”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case '/':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look("/"); //如果是“/”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case '=':
if (stringAdd.Trim() != "")
{
if (stringAdd.Trim().EndsWith(":", true,null)) //":="
{
stringAdd += stringChar;
Look(stringAdd);
stringAdd = "";
}
else if (stringAdd.Trim().EndsWith(">", true, null)) // ">="
{
stringAdd += stringChar;
Look(stringAdd);
stringAdd = "";
}
else if (stringAdd.Trim().EndsWith("<", true, null)) // "<="
{
stringAdd += stringChar;
Look(stringAdd);
stringAdd = "";
}
}
else
{
Look("="); //如果是“=”,直接查表
stringAdd = "";
}
stringAdd = "";
beginString = true;
return stringAdd;
case '>':
if (stringAdd.Trim() != "")
{
if (stringAdd.EndsWith("<", true, null))
{
stringAdd += stringChar;
Look(stringAdd);
stringAdd = "";
beginString = true;
}
else if (stringAdd.EndsWith("<", true, null) == false) //如果上个符号不是“<”,就要查找上个字符串
{
Look(stringAdd);
stringAdd = stringChar.ToString().Trim();
beginString = false;
}
}
else
{
stringAdd = stringChar.ToString().Trim(); //把“>”放入字符串一边检查是否是符号 “>=”
beginString = false; //这个地方不可以查“>”表,因为又可能是符号“>=”
}
return stringAdd;
case '<':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = stringChar.ToString(); //不可以对“<”查表,因为可能是符号“<=”和“<”
beginString = false; //现在就对“<”查表,会把符号“<=”解析成两个符号
} //"<" 和 "<="
else
{
stringAdd = stringChar.ToString();
beginString = false;
}
return stringAdd;
case '(':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look("("); //如果是“(”,直接查表
stringAdd = "";
beginString = true;
return stringAdd;
case ')':
if (stringAdd.Trim() != "")
{
Look(stringAdd);
stringAdd = "";
}
Look(")"); //如果是“+”,直接查表
stringAdd = "";
beginString = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -