2.c
资源名称:1.rar [点击查看]
上传用户:infocom
上传日期:2021-09-11
资源大小:157k
文件大小:4k
源码类别:

词法分析

开发平台:

C/C++

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define   buf_s 2000 /*缓冲区长度*/  
  4. #define   word_s  50 /*最大单词长度*/
  5. /*单词种别*/
  6. char *symbol[20]={"ERROR","IDN","INT8","INT10","INT16",
  7.                 "ADD","SUB","MUL","DIV","MORE","LESS","EQUAL",
  8. "LEFT","RIGHT","SEM",
  9. "IF","ELSE","THEN","WHILE","DO"};
  10. /*关键字*/
  11. char *key[5]={"if","else","then","while","do"};
  12. /* 运算符、分隔符*/
  13. char op_s[10]="+-*/><=();";
  14. int scan(char*buf,char*word,int*symbol_number);
  15. int main(){
  16. char buffer[buf_s];/*初始化缓冲区*/
  17. char finish_word[word_s]="";/*初始化单词为空*/
  18. int symbol_num=0;/*初始化单词种别值为0(错误)*/
  19. gets(buffer);/*读取程序*/
  20. while(scan(buffer,finish_word,&symbol_num))/*当到达程序结束,scan函数返回0,结束词法分析*/
  21. printf("%stt%sn",symbol[symbol_num],finish_word);/*打印单词种类、属性*/
  22. return 0;
  23. }
  24. /*scan分词函数(缓冲区地址,存放分隔单词地址,单词列别值地址)*/
  25. int scan(char*buf,char*word,int*symbol_number){
  26. static int n=0;/*静态,记录当前缓冲区读取位置*/
  27. char ch;
  28. int word_n=0;/*单词尾部*/
  29. int key=0;/*关键字类别值的临时存放变量*/
  30. int op=0;/*运算符、分隔符类别值的临时存放变量*/
  31. ch=buf[n];/*取第一个字符*/
  32. while(ch==' '||ch=='n'){/*如果是换行符,或者空格,跳过,读取下一个字符*/
  33. n++;
  34. ch=buf[n];
  35. }
  36. if(ch=='0'){/*第一个字符为0,则为十进制0,十六进制,或者八进制*/
  37. word[word_n]=ch;/*ch-->word*/
  38. word_n++;
  39. n++; /*取下一个字符*/
  40. ch=buf[n];
  41. if(ch=='x'||ch=='X'){/*十六进制分支*/
  42. word[word_n]=ch;
  43. word_n++;
  44. n++;/*取下一个字符*/
  45. ch=buf[n];
  46. if(isdigit(ch)||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')){
  47. word[word_n]=ch;
  48. word_n++;
  49. n++;
  50. ch=buf[n];
  51. while(isdigit(ch)||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')){
  52. word[word_n]=ch;
  53. word_n++;
  54. n++;
  55. ch=buf[n];
  56. }
  57. word[word_n]='';
  58. *symbol_number=4;/*单词类别值设为4(十六进制)*/
  59. }
  60. else{/*当为“0x”+“非0-9,a-f,A-F”,出错*/
  61. word[word_n]='';
  62. *symbol_number=0;/*单词类别值设为0(出错)*/
  63. }
  64. }
  65. else if(ch>='0'&&ch<='7'){/*八进制分支*/
  66. word[word_n]=ch;
  67. word_n++;
  68. n++;
  69. ch=buf[n];
  70. while(ch>='0'&&ch<='7'){
  71. word[word_n]=ch;
  72. word_n++;
  73. n++;
  74. ch=buf[n];
  75. }
  76. word[word_n]='';
  77. *symbol_number=2;/*单词类别值设为2(八进制)*/
  78. }
  79. else{/*为十进制0*/
  80. word[word_n]='';
  81. *symbol_number=3;/*单词类别值设为3(十进制)*/
  82. }
  83. }
  84. else if(ch>='1'&&ch<='9'){/*十进制分支*/
  85. word[word_n]=ch;
  86. word_n++;
  87. n++;
  88. ch=buf[n];
  89. while(ch>='0'&&ch<='9'){
  90. word[word_n]=ch;
  91. word_n++;
  92. n++;
  93. ch=buf[n];
  94. }
  95. word[word_n]='';
  96. *symbol_number=3;/*单词类别值设为3(十进制)*/
  97. }
  98. else if(isalpha(ch)){/*标示符(IDN),或者关键字(key)分支*/ 
  99. word[word_n]=ch;
  100. word_n++;
  101. n++;
  102. ch=buf[n];
  103. while(isalpha(ch)||isdigit(ch)){
  104. word[word_n]=ch;
  105. word_n++;
  106. n++;
  107. ch=buf[n];
  108. }
  109. word[word_n]='';
  110. if(key=iskeyword(word))/*是否为关键字*/
  111. *symbol_number=key;/*单词类别值设为key(关键字类别值)*/
  112. else *symbol_number=1;/*单词类别值设为1(标识符)*/
  113. }
  114. else if(op=isop_s(ch)){//运算或分隔符分枝
  115. word[word_n]=ch;
  116. word_n++;
  117. word[word_n]='';
  118. n++;
  119. *symbol_number=op;
  120. }else if(ch==''){/*结束符,退出scan程序*/
  121. return 0;
  122. }else{/*为非法字符*/
  123. word[word_n]=ch;
  124. word_n++;
  125. n++;
  126. word[word_n]='';
  127. *symbol_number=0;/*单词类别值设为0(非法字符)*/}
  128. return 1;
  129. }
  130. int isalpha(ch){//是否为字符
  131. if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
  132. return 1;
  133. else
  134. return 0;}
  135. int isdigit(ch){//是否为数字
  136. if(ch>='0'&&ch<='9')
  137. return 1;
  138. else
  139. return 0;}
  140. int iskeyword(char*w){//关键字
  141. int i;
  142. for(i=0;i<5;i++){
  143. if(strcmp(w,key[i])==0)
  144. return 15+i;/*返回关键字类别值*/
  145. }
  146. return 0;
  147. }
  148. int isop_s(char m){//运算或分隔符
  149. int i;
  150. for(i=0;i<10;i++){
  151. if(m==op_s[i])
  152. return 5+i;/*返回运算或分隔符类别值*/
  153. }
  154. return 0;
  155. }