编译原理词/语法分析
效果:
代码:
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9:
10: namespace 词法分析
11: {
12: public partial class Form1 : Form
13: {
14: public int Count = 0;
15: public bool EndFlag = false;
16: public char MString;
17: public string StartString = "";
18: public string EndString = "";
19: public string FirstString = " ";
20: public string Sym = " ";
21: Dictionary<string, int> Table = new Dictionary<string, int>();
22:
23: public Form1()
24: {
25: InitializeComponent();
26: }
27:
28: private void textBox1_TextChanged(object sender, EventArgs e)
29: {
30: if (textBox1.Text == "")
31: {
32: Count = 0;
33: StartString = "";
34: FirstString = " ";
35: EndString = "";
36: this.pictureBox2.Image = null;
37: this.label2.Text = "";
38: MyDataView.Rows.Clear();
39: }
40: if (textBox1.Text.Length - 1>=0)
41: MString = textBox1.Text.Substring(textBox1.Text.Length - 1, 1)[0];
42: else MString =' ';
43: Table = new Dictionary<string, int>();
44:
45:
46: Table.Add("begin", 1);
47: Table.Add("end", 2);
48: Table.Add("if", 3);
49: Table.Add("then", 4);
50: Table.Add("else", 5);
51: Table.Add("while", 6);
52: Table.Add("do", 7);
53: Table.Add("标识符", 8);
54: Table.Add("整常数", 9);
55: Table.Add("+", 10);
56: Table.Add("-", 11);
57: Table.Add("*", 12);
58: Table.Add("/", 13);
59: Table.Add("<=", 14);
60: Table.Add("<>", 15);
61: Table.Add("<", 16);
62: Table.Add(":", 17);
63: Table.Add(":=", 18);
64: Table.Add(";", 19);
65: Table.Add("(", 18);
66: Table.Add(")", 19);
67: Table.Add("$", 20);
68: Table.Add("id", 21);
69: EndProc();
70: }
71:
72: private void EndProc()
73: {
74: if (char.IsLetter(FirstString[0]))
75: {
76: if (char.IsLetter(MString) || char.IsDigit(MString))
77: {
78: StartString += MString;
79: FirstString = StartString[0].ToString();
80: }
81: else
82: {
83: if (MString != ' ' || MString != '\n')
84: {
85: EndString = StartString;
86: StartString = MString.ToString();
87: FirstString = StartString[0].ToString();
88: }
89:
90: switch (EndString)
91: {
92: case "begin":
93: EndString = "begin";
94: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
95: StartString = "";
96: FirstString = " ";
97: break;
98: case "end":
99: EndString = "end";
100: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
101: StartString = "";
102: FirstString = " ";
103: break;
104: case "if":
105: EndString = "if";
106: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
107: StartString = "";
108: FirstString = " ";
109: break;
110: case "then":
111: EndString = "then";
112: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
113: StartString = "";
114: FirstString = " ";
115: break;
116: case "else":
117: EndString = "else";
118: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
119: StartString = "";
120: FirstString = " ";
121: break;
122: case "while":
123: EndString = "while";
124: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
125: StartString = "";
126: FirstString = " ";
127: break;
128: case "do":
129: EndString = "do";
130: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
131: StartString = "";
132: FirstString = " ";
133: break;
134: case "id":
135: EndString = "id";
136: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
137: StartString = "";
138: FirstString = " ";
139: break;
140: default :
141: EndString = "标识符";
142: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
143: StartString = "";
144: FirstString = " ";
145: break;
146: }
147: EndProc();
148: EndFlag = true;
149: }
150: }
151: else if (char.IsDigit(FirstString[0]))
152: {
153: if (char.IsDigit(MString))
154: {
155: StartString += MString;
156: FirstString = StartString[0].ToString();
157: }
158: else
159: {
160: if (MString != ' ' || MString != '\n')
161: {
162: EndString = StartString;
163: StartString = MString.ToString();
164: FirstString = StartString[0].ToString();
165: }
166: StartString = "";
167: FirstString = " ";
168: EndFlag = true;
169: EndString = "整常数";
170: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
171: EndProc();
172: }
173: }
174: else if (FirstString == "<")
175: {
176: if (MString=='=')
177: {
178: StartString = "";
179: FirstString = " ";
180: EndFlag = true;
181: EndString = "<=";
182: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
183: }
184: else if (MString == '>')
185: {
186: StartString = "";
187: FirstString = " ";
188: EndFlag = true;
189: EndString = "<>";
190: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
191: }
192: else
193: {
194: if (MString != ' ' || MString != '\n')
195: {
196: EndString = StartString;
197: StartString = MString.ToString();
198: FirstString = StartString[0].ToString();
199: }
200: StartString = "";
201: FirstString = " ";
202: EndFlag = true;
203: EndString = "<";
204: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
205: EndProc();
206: }
207: }
208: else if (FirstString == ":")
209: {
210: if (MString == '=')
211: {
212: StartString = "";
213: FirstString = " ";
214: EndFlag = true;
215: EndString = ":=";
216: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
217: }
218: else
219: {
220: if (MString != ' ' || MString != '\n')
221: {
222: EndString = StartString;
223: StartString = MString.ToString();
224: FirstString = StartString[0].ToString();
225: }
226: StartString = "";
227: FirstString = " ";
228: EndFlag = true;
229: EndString = ":";
230: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
231: EndProc();
232: }
233: }
234: else switch (MString)
235: {
236: case '+':
237: StartString = "";
238: FirstString = " ";
239: EndFlag = true;
240: EndString = "+";
241: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
242: break;
243: case '-':
244: StartString = "";
245: FirstString = " ";
246: EndFlag = true;
247: EndString = "-";
248: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
249: break;
250: case '*':
251: StartString = "";
252: FirstString = " ";
253: EndFlag = true;
254: EndString = "*";
255: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
256: break;
257: case '/':
258: StartString = "";
259: FirstString = " ";
260: EndFlag = true;
261: EndString = "/";
262: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
263: break;
264: case '(':
265: StartString = "";
266: FirstString = " ";
267: EndFlag = true;
268: EndString = "(";
269: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
270: break;
271: case ')':
272: StartString = "";
273: FirstString = " ";
274: EndFlag = true;
275: EndString = ")";
276: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
277: break;
278: case '$':
279: StartString = "";
280: FirstString = " ";
281: EndFlag = true;
282: EndString = "$";
283: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
284: break;
285: case ';':
286: StartString = "";
287: FirstString = " ";
288: EndFlag = true;
289: EndString = ";";
290: MyDataView.Rows.Add(++Count, EndString, Table[EndString]);
291: break;
292: case '<':
293: StartString = MString.ToString();
294: FirstString = StartString[0].ToString();
295: break;
296: case ':':
297: StartString = MString.ToString();
298: FirstString = StartString[0].ToString();
299: break;
300: default:
301: if (char.IsLetter(MString))
302: {
303: StartString += MString;
304: FirstString = StartString[0].ToString();
305: }
306: if (char.IsDigit(MString))
307: {
308: StartString += MString;
309: FirstString = StartString[0].ToString();
310: }
311: break;
312: }
313: }
314:
315: private void button1_Click(object sender, EventArgs e)
316: {
317: Count = 0;
318: Scaner();
319: E();
320: if (Sym == "$")
321: {
322: if(this.pictureBox2.Image == null)
323: this.pictureBox2.Image = Image.FromFile("./图片/笑脸.jpg");
324: this.label2.Text = "Excellent work!!";
325: }
326: else
327: Error();
328: }
329:
330: private void Scaner()
331: {
332: try
333: {
334: Sym = MyDataView[1, Count++].Value.ToString();
335: }
336: catch
337: {
338: MessageBox .Show ("请输入终止符$!!");
339: }
340: }
341:
342: private void E()
343: {
344: T();
345: Epie();
346: }
347:
348: private void Epie()
349: {
350: if (Sym == "+")
351: {
352: Scaner();
353: T();
354: Epie();
355: }
356: else if (Sym != ")" && Sym != "$")
357: Error();
358: }
359:
360: private void T()
361: {
362: F();
363: Tpie();
364: }
365:
366: private void Tpie()
367: {
368: if (Sym == "*")
369: {
370: Scaner();
371: F();
372: Tpie();
373: }
374: else if (Sym != ")" && Sym != "$" && Sym != "+")
375: Error();
376: }
377:
378: private void F()
379: {
380: if (Sym == "id")
381: Scaner();
382: else if (Sym == "(")
383: {
384: Scaner();
385: E();
386: if (Sym == ")")
387: Scaner();
388: else Error();
389: }
390: else Error();
391: }
392:
393: private void Error()
394: {
395: this.pictureBox2.Image = Image.FromFile("./图片/哭脸.jpg");
396: this.label2.Text = "Error input!!";
397: }
398: }
399: }