编译原理经典算法的可视化实现 - 图文 联系客服

发布时间 : 星期二 文章编译原理经典算法的可视化实现 - 图文更新完毕开始阅读60e4aab5eefdc8d377ee3247

编译原理经典算法的可视化实现 析,并将分析出的结果也存储到另一个txt文件。将Richtextbox中内容保存成文件是在fileSave()函数中实现的,在这里我们是将其保存在a.txt中,如下所示: private void fileSave() {

if (File.Exists(\)) File.Delete(\);

FileStream fs = new FileStream(\, FileMode.OpenOrCreate,

FileAccess.Write);

StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.Flush();

m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin); m_streamWriter.Write(rtSource.Text); m_streamWriter.Flush(); m_streamWriter.Close(); }

而将a.txt中的内容进行词法分析是在函数cmdExcute()中执行的。这些动作都是在程序的后台执行,创建一个进程执行cmd程序,然后在cmd中输入将文件进行词法分析的命令,执行完后,我们将进程关闭,而这种过程我们是不能见到其执行过程。cmdExcute()函数代码如下所示: private void cmdExcute() {

System.Diagnostics.Process lexProcess = new System.Diagnostics.Process(); lexProcess.StartInfo.FileName = \; lexProcess.StartInfo.UseShellExecute = false; lexProcess.StartInfo.RedirectStandardInput = true; lexProcess.StartInfo.RedirectStandardOutput = true; lexProcess.StartInfo.RedirectStandardError = true; lexProcess.StartInfo.CreateNoWindow = true; lexProcess.Start();

13

编译原理经典算法的可视化实现 lexProcess.StandardInput.WriteLine(\); lexProcess.StandardInput.WriteLine(\); lexProcess.Close(); }

在词法分析的动态演示过程中,我们需要在richtextbox中移动的光标来指示程序分析到何处,而这个光标是在changcolor()函数中实现的,代码如下所示: private void changecolor() {

rtSource.SelectionStart = i-1; rtSource.SelectionLength = 1; rtSource.SelectionColor = Color.Red; i++; }

当richtextbox中的光标移动时,此时光标每移动一个字母或符号时,那么程序中的DFA也要有相关的动作,而这些动作是在drawDirectly()函数中用GDI+实现的。在每次光标移动时,我们就把将要画线的起始坐标和终点坐标传给这给函数。其中我们可以通过speed这个变量设置画线的播放速度。 void drawDirectly(Point start, Point end) {

Graphics g = this.CreateGraphics(); int length = end.X - start.X; int ave = length / 5; int len = start.X;

Pen pline = new Pen(Color.Red , 5);

pline.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; while (len < end.X) {

g.DrawLine(pline, start.X ,start.Y , len + ave-5, end.Y); System.Threading.Thread.Sleep(speed);//参数以毫秒为单位

14

编译原理经典算法的可视化实现 len += ave; }

Pen pline1 = new Pen(Color.Orange, 5);

pline1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; pline1.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; g.DrawLine(pline1, start, end);

}

3.2.2 词法分析器动态演示的暂停事件

当词法分析器正在对richtextbox中的内容进行词法分析,这时如果我们要求程序暂停,我们就可点击程序界面上的暂停按钮,为了方便实现暂停事件,我们将执行按钮的事件定义为一个线程,并声明一个AutoResetEvent变量和一个time变量,

AutoResetEvent允许线程通过发信号互相通信,通常,此通信涉及线程需要独占访问的资源,这此程序中,通信资源就是time,。 首先我们声明一个时间间隔引发事件的计时器,

private System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer(); AutoResetEvent autoEvent = new AutoResetEvent(false);

我们在执行事件中设置autoevent的waitone方法,然后我们每隔一秒就引发事件

tm.Interval = 1000;

tm.Tick += new EventHandler(tm_Tick);

接下来我们如果单击暂停按钮,我们就将计时器停止,则此时autoevent事件就不会开启,执行事件的这个线程也就暂停了,然后再单击继续按钮时,计时器继续计时,此时执行事件的这个线程又能开始运行了。

private void toolStop_Click(object sender, EventArgs e) {

if (flag) {

tm.Stop(); flag = false;

this.toolStop.Text = \继 续\;

15

编译原理经典算法的可视化实现 } else {

tm.Start(); flag = true;

this.toolStop.Text = \暂 停\; } }

3.2 程序界面的实现

在本程序中在左边我们提供一个输入原文件的文本框,需要用到GDI+来画出词法分析器的DFA,整个程序的界面顶端都是按钮,我们用listview来给出词法分析的的结果,即二元组,整体的界面如下图所示:

图3.2 程序的界面

16