C语言词法分析器

  • 格式:doc
  • 大小:79.00 KB
  • 文档页数:8

实验任务6:编写词法分析程序 1.实验目的:体会词法分析程序的工作原理和功能 2.实验内容:编写一个词法分析程序,实现的功能是:输入一个C语言程序,经该词法分析程序处理后,输出单词记号序列。 3.实验要求:提交程序清单(包含注释),提交输入一个C语言程序用词法分析程序处理后的运行结果截图以及运行环境配置说明。 4.验收时间:第12周

(1) main主程序图

是 (2) Scan()函数流程图

Run=1? 初始化数组 Program[],words[]

提示输入程序字符串 输入并读取程序字符

初始化 处理每个字符 开始

Scan()函数,分析并返回每个单词的属性

判断并输出每个单词类型属性。

是否结束 结束 初始化数组,标志符 判断并标志是否为关键字

判断并标志为运算符或标点符号

判断并标志为其它类型

Return返回sign数值 五,原程序代码 #include #include int i,j,k,sign,number,flag,run,sum,autuor; /*flag which is use to judge the string is keywords or not!*/ char ch; char words[10] = {" "}; char program[500]; int Scan(char program[]) { char * keywords[39] = { "auto","default","do", "extern","goto", "int","short","static","struct","switch", "typedef","void","break","case","char", "const","continue","double","else", "enum", "float","for","if","long", "register", "return","signed","zhouhong","sizeof","union", "unsigned","volatile","while","main","printf", "scanf","include","define","string" }; number = 0; flag = 0; j = 0; ch = program[i++]; /* To handle the lettle space ands tab*/ while ((ch == ' ') || (ch == '\t') || (ch == '\r')||(ch=='\n')) ch=program[i++]; /* 跳过不理睬 */ /*handle letters*/ if ((ch >= 'a') && (ch <= 'z' )) { while ((ch >= 'a') && (ch <= 'z' )) { words[j++]=ch; ch=program[i++]; } i--; words[j++] = '\0'; for (k = 0; k < 39; k++) if (strcmp (words,keywords[k]) == 0) {sign=k+1;flag=1; if(k==27) autuor=1; break; } if (flag == 0) { sign = 100; /*为关键字*/ } }/* if 是关键字*/ /*handle digits*/ else if ((ch >= '0') && (ch <= '9')) { number = 0; while ((ch >= '0' ) && (ch <= '9' )) { number = number*10+(ch-'0'); ch = program[i++]; } sign = 200; /*为数字*/ i--; } /*opereation and edge handle*/ else switch (ch) { case '=':{ if (ch == '=') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') {words[j++] = ch; words[j] = '\0'; sign= 401; /* = = */ } else { i--; /*退回一个字符*/ sign = 402; /* = */ } break; } case'>':{ if (ch == '>') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') {words[j++] = ch; words[j] = '\0'; sign = 403; /* >= */ } else if(ch=='>') { words[j++] =ch; words[j] ='\0'; sign =419; /* >> */ } else { i--; sign = 404; /* > */ } break; } case'<': {if (ch == '<') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; sign = 405; } else if(ch=='<') { words[j++] = ch; words[j] = '\0'; sign = 420; } else { i--; sign = 406; }break; } case'!': {if (ch == '!') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') {words[j++] = ch; words[j] = '\0'; sign= 407; } else {i--; sign= 408; } break; } case'+':{ if (ch == '+') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; sign= 409; } else if (ch == '+') { words[j++] = ch; words[j] = '\0'; sign= 410; } else {i--; sign = 411; } break; } case'-': {if (ch == '-') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; sign= 412; } else if( ch == '-') { words[j++] = ch; words[j] = '\0'; sign= 413; } /* 负数的情况 */ else if((ch >= '0') && (ch <= '9')) {while( (ch >= '0') && (ch <= '9') ) {words[j++] = ch; ch = program[i++]; } words[j] = '\0'; sign = 1000; /* 标志为1000 */ /* 负数 */ i--; break; } else { i--; sign = 414; }break; } case'*':{ if (ch == '*') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') {words[j++] = ch; words[j] = '\0'; sign = 415; } else { i--; sign = 416; } break; } case'/':{ if (ch == '/') words[j++] = ch; words[j] = '\0'; ch = program[i++]; if (ch == '=') { words[j++] = ch; words[j] = '\0'; sign = 417; } else { i--; sign = 418; } break; } case'^':{ if(ch=='^') words[j++] = ch; words[j] = '\0'; sign =421; break; } case';':{ words[j] = ch; words[j+1] = '\0'; sign = 501; break; } case'(':{ words[j] = ch; words[j+1] = '\0'; sign = 502; break; } case')':{ words[j] = ch; words[j+1] = '\0'; sign = 503; break; } case'[':{ words[j] = ch; words[j+1] = '\0'; sign = 504; break; } case']':{ words[j] = ch; words[j+1] = '\0'; sign = 505; break; } case'{':{ words[j] = ch; words[j+1] = '\0'; sign = 506; break; } case'}':{ words[j] = ch; words[j+1] = '\0'; sign = 507; break; } case':':{ words[j] = ch; words[j+1] = '\0'; sign = 508; break; } case'"':{ words[j] = ch; words[j+1] = '\0'; sign = 509;