📄 form1.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 Microsoft.VisualBasic.Devices;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
const short ERROR = -1;
const short BEGIN = 1;
const short END = 2;
const short IF = 3;
const short THEN = 4;
const short ELSE = 5;
const short ID = 6;
const short INT = 7;
const short LT = 8;
const short LE = 9;
const short EQ = 10;
const short NE = 11;
const short GT = 12;
const short GE = 13;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
KeywordTable = new Keyword[5];
KeywordTable[0] = new Keyword("BEGIN", BEGIN);
KeywordTable[1] = new Keyword("END", END);
KeywordTable[2] = new Keyword("IF", IF);
KeywordTable[3] = new Keyword("THEN", THEN);
KeywordTable[4] = new Keyword("ELSE", ELSE);
textBox1.Clear();
textBox4.Clear();
try
{
string FILENAME = string.Empty;
FILENAME = textBox2.Text;
Computer MyComputer = new Computer();
System.IO.StreamReader MyStreamReader;
MyStreamReader = MyComputer.FileSystem.OpenTextFileReader(FILENAME, Encoding.Default);
textBox4.Text = MyComputer.FileSystem.ReadAllText(FILENAME, Encoding.Default);
/////////////////////////////////////////////////////////////////////////////
int c;
char ch;
string stemp = string.Empty;
while (MyStreamReader.Peek()!=-1)
{
stemp = string.Empty;
while(IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
{
MyStreamReader.Read();
}
if (MyStreamReader.Peek() == -1)
continue;
ch = (char)MyStreamReader.Peek();
if (isalpha(ch))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
while (isalnum(ch))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
}
ch = (char)MyStreamReader.Peek();
if (MyStreamReader.Peek() == -1 | IsListSeparatorOrBlank(ch) | isrelationsign(ch))
{
c = lookup(stemp);
if (c == 0) Out(ID, stemp);
else Out(c, "");
}
else
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
}
Out(ERROR, stemp);
}
}
else if (isdigit(ch))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
while (isdigit(ch))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
}
ch = (char)MyStreamReader.Peek();
if (MyStreamReader.Peek() == -1 | IsListSeparatorOrBlank(ch) | isrelationsign(ch))
{
Out(INT, stemp);
}
else
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
}
Out(ERROR, stemp);
}
}
else switch (ch)
{
case '<':
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
if (ch == '=')
{ stemp += ch; Out(LE, ""); MyStreamReader.Read(); }
else if (ch == '>')
{ stemp += ch; Out(NE, ""); MyStreamReader.Read(); }
else Out(LT, "");
break;
case '=':
stemp += ch; Out(EQ, "");
MyStreamReader.Read();
break;
case '>':
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
if (ch == '=')
{ stemp += ch; Out(LE, ""); MyStreamReader.Read(); }
else Out(GT, "");
break;
default :
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
{
stemp += ch;
MyStreamReader.Read();
ch = (char)MyStreamReader.Peek();
}
Out(ERROR, stemp);
break;
}
}
MyStreamReader.Close();
////////////////////////////////////////////////////////////////////////////
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private bool isalpha(int c)
{
if ((c <= 'z' && c >= 'a') | (c <= 'Z' && c >= 'A'))
return true;
else return false;
} //判断是否为大小写字母
private bool isalnum(int c)
{
if ((c <= 'z' && c >= 'a') | (c <= 'Z' && c >= 'A') | (c <= '9' && c >= '0'))
return true;
else return false;
} //判断是否为大小写字母或数字
private bool isdigit(int c)
{
if (c <= '9' && c >= '0')
return true;
else return false;
} //判断是否为数字
private bool isrelationsign(int c) //判断是否为关系符
{
if (c == '>' | c == '<' | c == '=')
return true;
else return false;
}
class Keyword
{
public string key;
public int value;
public Keyword(string s, int v)
{
this.key = s;
this.value = v;
}
} //关键字类定义
Keyword[] KeywordTable;
private int lookup(string s)
{
foreach (Keyword t in KeywordTable)
{
if (s == t.key)
return t.value;
}
return 0;
} //判断输入串是否在关键字表中
private void Out(int c, string s)
{
textBox1.Text += "(" + c + "," + s + ") "+Environment.NewLine;
} //输出信息
private bool IsBlank(char c)
{
if (c == ' ' | c == '\t' | c=='\n')
return true;
else return false;
} //是空白符吗
private bool IsListSeparatorOrBlank(char c)
{
if (c == ' ' | c == '\t' | c=='\n'|c==','|c==';'|c=='\r')
return true;
else return false;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text += (@"D:\T1.txt");
textBox5.Text += (@"D:\T2.txt");
textBox3.Text += "分析结果是形如(CLASS,VALUE)的二元表达式,CLASS为类型码,VALUE为其值。若当前分析的串为关键字,则VALUE为空。另外,我们约定CLASS为-1时表示此串为非法串(查错功能)";
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string myText;
myText = "*****原文件内容为*****" + textBox4.Text + Environment.NewLine + "**********************" + Environment.NewLine + Environment.NewLine + "****词法分析结果为****" + Environment.NewLine + textBox1.Text + "**********************" + Environment.NewLine + "建立文件时间:" + System.DateTime.Now.ToString() + Environment.NewLine;
File.WriteAllText(textBox5.Text, myText, Encoding.Default);
MessageBox.Show("成功写入文件","操作成功",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -