2.cpp
上传用户:jiashang
上传日期:2013-07-26
资源大小:12k
文件大小:7k
源码类别:

文本生成

开发平台:

Visual C++

  1. #include "prediction.h"
  2. #include "func.h"
  3. const char *Operator[]={
  4. "," , "?" , ":" ,
  5. "||" , "&&" , "|" , "^" , "&" , "!" , "<" , ">" , "~" ,
  6. "+" , "-" , "*" , "/" , "=" , "%" ,
  7. "[" , "]" , "(" , ")"
  8. };
  9. int isstatic(str)
  10. {
  11. const char *StrToken[]=
  12. {"auto","break","case","char","const","continue","default","do",
  13. "double","else","enum","extern","float","for","goto","if",
  14. "int","long","register","return","short","signed","sizeof","static",
  15. "struct","switch","typedef","union","unsigned","void","volatile","while"
  16. };
  17. int i;
  18. for(i=0;i<32;i++)
  19. if(strcmp(str,StrToken[i])==0) return (i+1);
  20. return(0);
  21. }
  22. void SaveResult(char str[],char message[])
  23. {
  24. if(fputs(str,fpw)!=0) exit(0);
  25. if(fputc('<',fpw)==EOF) exit(0);
  26. for(j=0;j<5;j++)
  27. if(fputc('-',fpw)==EOF) exit(0);
  28. if(fputc('>',fpw)==EOF) exit(0);
  29. if(fputs(message,fpw)!=0) exit(0);
  30. if(fputc("t",fpw)==EOF) exit(0);
  31. }
  32. //////////////////////////////////////////////////////////////////////////////////
  33. void GetMessage()
  34. {
  35. char nowch;
  36. char str[100],message[20];
  37. int i,j;
  38. while(fgetc(fpr)!=EOF)
  39. {
  40. i=0;
  41. nowch=fgetc(fpr);
  42. str[i++]=nowch;
  43. switch(nowch)
  44. { //跳过空格、换行等
  45. if(isspace(nowch)==1) continue;
  46. //头文件符号#
  47. case '#' : str[i++]='';
  48. strcpy(message,"#号");
  49. break;
  50. //字符串
  51. case '"' : while((nowch=fgetc(fpr))!='"')
  52. str[i++]=nowch;
  53. str[i++]='"';str[i++]=''
  54. strcpy(message,"字符串");
  55. break;
  56. //注释,/,/=
  57. case '/' : nowch=fgetc(fpr);
  58. if(nowch=='*') 
  59. { str[i++]='*';
  60. strcpy(message,"左解释符");
  61. printf("%s<----->%st",str,message);
  62. SaveResult(str,message);
  63. //继续分析至注释结束
  64. while(1)
  65. {
  66. nowch=fgetc(fpr);
  67. if((nowch=fgetc(fpr))=='*')
  68. if((nowch=fgetc(fpr))=='/')
  69. {
  70. strcpy(str,"*/");
  71. strcpy(message,"右解释符");
  72. break;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. while(isspace(nowch)==1) nowch=fgetc(fpr);
  79. if(nowch=='=')
  80. { str[i++]=nowch;str[i++]='';
  81. strcpy(message,"组合运算符=");
  82. }
  83. else
  84. {
  85. str[i++]='';
  86. fseek(fpr,-sizeof(char),1);
  87. strcpy(message,"除法运算符");
  88. }
  89. }
  90. break;
  91. //*,*=
  92. case '*' : nowch=fgetc(fpr);
  93. while(isspace(nowch)==1) nowch=fgetc(fpr);
  94. if(nowch=='=')
  95. { str[i++]=nowch;str[i++]='';
  96. strcpy(message,"组合运算符*=");
  97. }
  98. else
  99. {
  100. str[i++]='';
  101. fseek(fpr,-sizeof(char),1);
  102. strcpy(message,"乘法运算符");
  103. }
  104. break;
  105. //+=,++,+
  106. case '+' :
  107. case '-' :
  108. case '<' :
  109. case '>' :
  110. case '=' :
  111. }
  112. //打印分析结果并保存
  113. printf("%s<----->%st",str,message);
  114. SaveResult(str,message);
  115. if((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))//字符是字母
  116. }
  117. }
  118. /*
  119. i=0;
  120. while(isspace(nowch)==1)
  121. nowch=fgetc(fpr);
  122. while(islower(nowch)||isupper(nowch))
  123. {
  124. str[i++]=nowch;
  125. nowch=fgetc(fpr);
  126. }
  127. str[i++]='';
  128. if(isstatic(str)) strcpy(message,"关键字");
  129. else
  130. printf("%s<----->%st",str,message);
  131. SaveResult(str,message);
  132. //头文件名
  133. i=0;
  134. fseek(fpr,-sizeof(char),1);
  135. nowch=fgetc(fpr);
  136. while(isspace(nowch)==1)
  137. nowch=fgetc(fpr);
  138. if(nowch=='<')
  139. {
  140. }
  141.    case '^':
  142. cch=GetC();
  143. if(cch=='=')cout<<"29"<<"^=";
  144. else
  145. {
  146. Pushback(cch);cout<<"30"<<"^";
  147. };break;
  148. case '|':
  149.     cch=GetC();
  150. switch(cch)
  151. {
  152. case'|':cout<<"31"<<"||";break;
  153. case'=':cout<<"32"<<"|=";break;
  154. default:Pushback(cch);cout<<"33"<<"|";
  155. };break;
  156. case '(':cout<<"34"<<"(";break;
  157. case ')':cout<<"35"<<")";break;
  158. case '[':cout<<"36"<<"[";break;
  159.             case ']':cout<<"37"<<"]";break;
  160.             case '{':cout<<"38"<<"{";break;
  161. case '}':cout<<"39"<<"}";break;
  162. case '~':cout<<"40"<<"~";break;
  163. case '.':cout<<"41"<<".";break;
  164.             case ';':cout<<"42"<<";";break;
  165.             case ',':cout<<"43"<<",";break;
  166. //case '#':cout<<"#";break;
  167.             case '?':
  168.        case '+':
  169. {
  170. cch=GetC();
  171.         switch(cch)
  172. {
  173.             case '+':cout<<"1"<<"++";break;
  174.         case '=':cout<<"2"<<"+=";break;
  175.         default:Pushback(cch);cout<<"3"<<"+";break;
  176. }
  177. };break;
  178.   
  179. case '-':
  180. {
  181. cch=GetC();
  182.     switch(cch)
  183. {
  184.     case '-':cout<<"4"<<"--";break;
  185.     case '=':cout<<"5"<<"-=";break;
  186.                         case '>':cout<<"6"<<"->";break;
  187.     default :Pushback(cch);
  188.  cout<<"7"<<"-";
  189.  break;
  190. }
  191. };break;
  192. case '*':
  193. {
  194. cch=GetC();
  195. if(cch=='=') cout<<"8"<<"*=";
  196. else 
  197. {
  198. Pushback(cch);
  199. cout<<"9(注释):"<<"*";
  200. }
  201. };break;
  202.  
  203. case '/':
  204. {
  205. cch=GetC();
  206.     if(cch=='=') cout<<"10"<<"/=";
  207. else
  208. {
  209. Pushback(cch);
  210. cout<<"11(字符是):"<<"/";
  211. }
  212. };break;
  213.  
  214. case '<':
  215. {
  216. cch=GetC();
  217. switch(cch)
  218. {
  219. case'=':cout<<"12"<<"<=";break;
  220. case'<':cch=GetC();
  221.   if(cch=='=')cout<<"13"<<"<<=";
  222.   else 
  223.   {
  224.   Pushback(cch);
  225.       cout<<"14"<<"<<";
  226.   };
  227. default:Pushback(cch);cout<<"15"<<"<";
  228.  
  229. }
  230. };break;
  231.  
  232. case '>':
  233. {
  234. cch=GetC();
  235.     switch(cch)
  236. {
  237. case'=':cout<<"16"<<">=";break;
  238. case'>':cch=GetC();
  239.   if(cch=='=')cout<<"17"<<">>=";
  240.   else 
  241.   {
  242.   Pushback(cch);
  243.   cout<<"18"<<">>";break;}
  244. default:Pushback(cch);cout<<"19"<<">";
  245. }
  246. };break;
  247. ////////////////////////////////////////////////////////////////////////
  248. case '=':
  249. {
  250. cch=GetC();
  251. if(cch=='=') cout<<"20"<<"==";
  252. else 
  253. {
  254. Pushback(cch);
  255. cout<<"21"<<"=";
  256. }
  257. };break;
  258. case '%':
  259. {
  260. cch=GetC();
  261. if(cch=='=') cout<<"22"<<"%=";
  262. else
  263. {
  264. Pushback(cch);
  265. cout<<"23"<<"%";
  266. }
  267. };break;
  268. case '!':
  269. {
  270. cch=GetC();
  271. if(cch=='=') cout<<"24"<<"!=";
  272. else 
  273. {
  274. Pushback(cch);
  275. cout<<"25"<<"!";
  276. }
  277. };break;
  278. case '&':
  279. {
  280. cch=GetC();
  281.     switch(cch)
  282. {
  283.     case '&':cout<<"26"<<"&&";break;
  284. case '=':cout<<"27"<<"&=";break;
  285. default:Pushback(cch);cout<<"28"<<"&";
  286. }
  287. };break;