text44.Text = \; text4E.Text = \; text4T.Text = \;
text4F.Text = \;//状态4 的对应各种非终结符的动作和转移.
text52.Text = \; text53.Text = \; text56.Text = \;
text55.Text = \;//状态5 的对应各种非终结符的动作和转移.
text61.Text = \; text64.Text = \; text6T.Text = \;
text6F.Text = \;//状态6 的对应各种非终结符的动作和转移.
text71.Text = \; text74.Text = \;
text7F.Text = \;//状态7 的对应各种非终结符的动作和转移.
text82.Text = \;
text85.Text = \;//状态8 的对应各种非终结符的动作和转移.
text92.Text = \; text93.Text = \; text95.Text = \;
text96.Text = \;//状态9 的对应各种非终结符的动作和转移.
text102.Text = \; text103.Text = \; text105.Text = \;
text106.Text = \;//状态A 的对应各种非终结符的动作和转移.
text112.Text = \; text113.Text = \; text115.Text = \;
text116.Text = \;//状态B 的对应各种非终结符的动作和转移.
b.开始分析按钮代码(初始化代码)
listzhan.Items.Clear();//状态栈的初始化 清空
listin.Items.Clear();//输入栈的初始化 清空 listfuhao.Items.Clear();//符号栈的初始化 清空
tempfuhao=\;//tempfuhao 符号栈的临时串,功能:程序对它做修改 再传入符号栈 tempzhan=\;//tempzhan 状态栈的临时串 功能:程序对它作修改 再传入状态栈 shurust = textshuru.Text+\;
//shurust 输入栈的临时串 功能:程序对它做修改,再传入输入栈
6
if (textshuru.Text == \) {
DialogResult r1 = MessageBox.Show(\未输入语句\, \错误!!\, MessageBoxButtons.OK,
MessageBoxIcon.Error);
}// 判断输入串是否为空 如果为空 弹出提示窗口 else {
for (int i = 0; i < ccsBox.Items.Count; i++) {
jiedian[i, 0] = ccsBox.Items[i].ToString(); jiedian[i,2]=\;
jiedian[i,1]=jiedian[i,0][0].ToString(); for(int j=2;j jiedian[i, 2] += jiedian[i, 0][j]; } } //读入各个产生式 并将每个产生式的开始符号传入 jiedian[I,1] //将产生式的 右半部分传入 jiedian[I,2] listzhan.Items.Add(tempzhan); listin.Items.Add(shurust); listfuhao.Items.Add(tempfuhao); //将tempzhan压入状态栈,shurust压入输入栈 tempfuhao压入符号栈 } c.[单步跟踪]按钮代码(分布处理代码) text01.ForeColor = Color.Black; text04.ForeColor = Color.Black; text0E.ForeColor = Color.Black; …………….(略去部分代码) ……………. // 将分析表中文本的字体颜色初始化为黑色 if (textshuru.Text == \) { DialogResult r1 = MessageBox.Show(\未输入语句\, \错误!!\, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { string temp1 = \; string temp2 = \; string cmp1, cmp2; cmp1 = tempzhan; cmp2 = tempfuhao; int llll = 1; 7 // llll 局部变量 用来标示是否已经执行过一次,1为未执行 0为已执行 if (tempzhan[tempzhan.Length - 1] == '0' && llll == 1) //状态栈最后1个状态为0时的响应函数 { if (shurust[shurucount] == 'd') //输入栈第一个字符为d是响应函数 { tempzhan += \; tempfuhao += \; for (int i = 1; i < shurust.Length; i++) { temp1 += shurust[i].ToString(); } shurust = temp1; llll = 0; text01.ForeColor = Color.Red; //将0状态下遇到d的动作文本 字体颜色变成红色显示 } else { if (shurust[shurucount] == '(') { tempzhan += \; tempfuhao += \; for (int i = 1; i < shurust.Length; i++) { temp1 += shurust[i].ToString(); } shurust = temp1; llll = 0; text04.ForeColor = Color.Red; //将0状态下遇到 ( 的动作文本 字体颜色变成红色显示 } } } if (tempzhan[tempzhan.Length - 1] == '1' && llll == 1) //状态栈最后1个状态为1时的响应函数 与0响应函数类似 { if (shurust[shurucount] == '+') { tempzhan += \; tempfuhao += \; 8 for (int i = 1; i < shurust.Length; i++) { temp1 += shurust[i].ToString(); } shurust = temp1; llll = 0; text12.ForeColor = Color.Red; } else { if (shurust[shurucount] == '$') { llll = 0; text16.ForeColor = Color.Red; DialogResult r1 = MessageBox.Show(\该语句属于该文法\, \分析完毕!!\, MessageBoxButtons.OK, MessageBoxIcon.Information); } } } if (tempzhan[tempzhan.Length - 1] == '2' && llll == 1) { if (shurust[shurucount] == '*') { tempzhan += \; tempfuhao += \; for (int i = 1; i < shurust.Length; i++) { temp1 += shurust[i].ToString(); } shurust = temp1; llll = 0; text23.ForeColor = Color.Red; } else { if (shurust[shurucount] == '+' || shurust[shurucount] == ')' || shurust[shurucount] == '$') // 响应规约时的 各个栈 变化函数 { temp2 = \; for (int i = 0; i < tempfuhao.Length - 1; i++) { 9 temp2 += tempfuhao[i]; } tempfuhao = temp2; tempfuhao += jiedian[1, 1]; temp2 = \; for (int i = 0; i < tempzhan.Length - 1; i++) { temp2 += tempzhan[i]; } tempzhan = temp2; if (tempzhan[tempzhan.Length - 1] == '0') { tempzhan += '1'; text0E.ForeColor = Color.Red; } else { if (tempzhan[tempzhan.Length - 1] == '4') { tempzhan += '8'; text4E.ForeColor = Color.Red; } } llll = 0; if(shurust[shurucount]=='+') text22.ForeColor = Color.Red; if (shurust[shurucount] == ')') text25.ForeColor = Color.Red; if (shurust[shurucount] == '$') text26.ForeColor = Color.Red; } } } ………… ………….(略去部分代码) if (cmp2 == tempfuhao && cmp1 == tempzhan ) //判断符号栈与状态栈是否有新的变换 无变化则分析停止 否则将各个栈的状态更新 { if(llll==1) MessageBox.Show(\该语句不属于该文法\, \分析完毕!!\, MessageBoxButtons.OK, MessageBoxIcon.Stop); } 10 else { listfuhao.Items.Add(tempfuhao); listin.Items.Add(shurust); listzhan.Items.Add(tempzhan); } 5、演示分析 例如:对句子d+d+d*d的分析: 四、总结 我认为做好一个可视化的程序首先要做的工作是设计它界面,应为一个好的界面设计好了,那么在设计界面的过程中可能会激发起编程的思路。做好一个项目的最主要的是要有恒心,虽然在做分析器的过程中遇到了很多的问题,刚开始做的时候没有什么头绪,经过和同学讨论和在网上搜集相关问题的答案,终于基本的问题都能够解决。 经过这次课程设计,对语法分析有了更深入的了解,巩固了上课期间所学的知识。对编译原理的基本原理也有了一定的了解。 五、参考书目 【1】编译原理…………………………..陈意云 张昱 ……………高等教育出版社 11