词法分析器设计实验报告 联系客服

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

六、实验代码(C#)

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms;

namespace 词法分析器

{ /*单词符号 种别码 单词符号 种别码 * if 1 标识符 40 * else 2 常量 41 * then 3 * 42

* while 4 / 43 * do 5 + 44 * begin 6 - 45 * end 7 > 46 * printf 8 < 47 * main 9 <> 48 * scanf 10 <= 49 * return 11 >= 50 * = 51 * ; * ( * ) * { * } * ++ * -- * 出错符 * # * */

public partial class Form1 : Form {

/*

*成员变量 */

String text = \

Char[] text_is;//输入流

int type_code=10;//单词种别码 int number;//整形常量 int line=1;//行号 int column = 1;//列号 char ch; int p = 0; String []keyword=new String[]{\\\ public Form1() {

InitializeComponent(); } /*

* 对输入流进行操作 */

private void readIo() {

52 53 54 55 56 57 58 -1 0 \\

text = \ try {

ch = text_is[p++]; }

catch (Exception) { ; }

/*预处理*/ while (ch == ' ') {

ch = text_is[p]; p++; }

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) //标识符或变量名检测 {

while ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

{

text = text + ch; ch = text_is[p++]; } p--;

type_code = 40;

for (int i = 0; i< keyword.Length; i++) //保留字检测 if (text==keyword[i]) {

type_code = i + 1; break; } }

else if ((ch >= '0' && ch <= '9')) //数字检测 {

if ((text_is[p] >= 'a' && text_is[p] <= 'z') || (text_is[p] >= 'A' && text_is[p] <= 'Z'))

{

while (ch!=' '&&ch!=';'&&ch!='#') {

text = text + ch;