词法分析器设计实验报告

发布时间 : 星期六 文章词法分析器设计实验报告更新完毕开始阅读

dataGridView2.Rows.Add(dgr); } }

catch (Exception) {

; } }

private void addData2(String s1,String s2,String s3,String s4,String s5) { DataGridViewRow dgr = new DataGridViewRow();

DataGridViewTextBoxCell dgt1 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dgt2 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dgt3 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dgt4 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dgt5 = new DataGridViewTextBoxCell(); dgt1.Value = s1; dgt2.Value = s2; dgt3.Value = s3; dgt4.Value = s4; dgt5.Value = s5; dgr.Cells.Add(dgt1); dgr.Cells.Add(dgt2); dgr.Cells.Add(dgt3); dgr.Cells.Add(dgt4); dgr.Cells.Add(dgt5);

dataGridView1.Rows.Add(dgr); }

private Char getStackBottom(String s) { if (s == \ return '\\0';

else if ((s[0] > 'a') && s[0] <= 'z') return 'i'; else

return s[0]; }

private void Error() {

MessageBox.Show(\出现错误\ }

private PLACE SearchSheet(String[,] sheet,Char c,Char a){ PLACE place = new PLACE();

for (int i = 0; i < Terminator.Length; i++) { if (Terminator[i] == a)

place.y = i; }

for (int i = 0; i < nonterminal.Length; i++) {

if (nonterminal[i] == c) place.x = i; }

if (sheet[place.x, place.y] != \ place.flag = true; else

place.flag = false; return place; }

private String Reverse(String s) { String str = \

for (int i = 0; i < s.Length; i++) {

str = str + s[s.Length - i - 1]; }

return str; }

private void mainControlProgram(String[,] sheet,String s) {

bool FLAG = true; Char c=' ';//符号栈顶 Char a=' ';//输入串栈底 String temStack = \ String temS = \

String action = \初始化\ int k = 0;

STACK = \ a=getStackBottom(s); while (FLAG) {

c = STACK[STACK.Length - 1]; if (!(temStack == STACK)) {

PLACE place = new PLACE(); place = SearchSheet(sheet, c, a);

addData2(k.ToString(), STACK, s, temS,action); action = \

if (sheet[place.x, place.y] != null place.y].Contains('@'))

temS = sheet[place.x, place.y]; else

&& sheet[place.x,

temS = \ k++; }

STACK = STACK.Remove(STACK.Length-1); action = action + \ if (terminator.Contains(c)) {

if (c == a){

s = Reverse(s).Remove(s.Length-1); action = \ s = Reverse(s);

a = getStackBottom(s); } else

Error(); }

else if (c == '#') { if (c == a)

FLAG = false; else

Error(); }

else if(SearchSheet(sheet,c,a).flag){ PLACE place = new PLACE(); place=SearchSheet(sheet, c, a); String str =Reverse(sheet[place.x, place.y]+\ str=str.Remove(str.Length-3); if (!(str == \

STACK = STACK + str;

action = action + \)\ temStack = STACK;

addData2(k.ToString(), STACK, s, sheet[place.x, place.y], action); action = \ k++; } } } }

private void button1_Click(object sender, EventArgs e) {

String first_s=\

String follow_s=\

terminator = getTerminator((richTextBox1.Text+\ nonterminal = getNonterminal((richTextBox1.Text + \ production = getProductions((richTextBox1.Text + \ FIRST[] first = new FIRST[20];

FOLLOW[]follow=new FOLLOW[20]; for (int i = 0; i < nonterminal.Length; i++) {

first[i] = getFirst(nonterminal[i]);

first_s = first_s + first[i].noter + \的First集为:\ }

richTextBox2.Text = first_s;

for (int i = 0; i < nonterminal.Length; i++) {

follow[i] = getFollow(nonterminal[i]);

follow_s = follow_s + follow[i].noter + \的Follow集为:\+ '\\n';

}

richTextBox4.Text = follow_s; String [,]sheet=new String[20,20]; sheet=getAnalysis_Sheet(first, follow); addData(sheet);

mainControlProgram(sheet,richTextBox3.Text);//进入主控程序 } } }

七、实验总结

本次实验经过多个文法进行测试,能够正确的构造FIRST集和FOLLOW集以及 LL(1)分析表。能够做到完全自动化。通过上次实验的C#的编程学习,为这次编写LL(1)分析器做好了充分的铺垫。能够熟练的掌握C#的编程技巧。为下次实验打下基础。

联系合同范文客服:xxxxx#qq.com(#替换为@)