Cpp syntax detector

Rhohitman Rhohitman is offline Offline Jun 25th, 2009, 2:54 am |
0
i don't know how it happen but the exe formed after compilation was 1.23 MB which has never happen before.
Quick reply to this message  
C++ Syntax
  1. /*
  2.   Title: Detect 5R
  3.   Version: Test Mode
  4.   Started Date : 19 Jan 2009
  5.   Last Update : 30 Jan 2009
  6.   Revised: 4
  7. */
  8.  
  9. #include <iostream>
  10. #include <windows.h>
  11. #include <conio.h>
  12.  
  13. using namespace std;
  14.  
  15. HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  16. FILE *fp;
  17.  
  18. #define TxtAttrib(x) SetConsoleTextAttribute(hConsole, x)
  19. #define DefTxt SetConsoleTextAttribute(hConsole,15)
  20.  
  21. #define Marker(x) {\
  22. TxtAttrib(10);\
  23. cout<<(char)x;\
  24. DefTxt;\
  25. }
  26.  
  27. #define buffer_pp {\
  28. buffer=fgetc(fp);\
  29. if(buffer==EOF) return EOF;\
  30. }
  31.  
  32. /*
  33.   Color Attribute = foreground(%d) + background(%d) * 16
  34.   0 = Black 1 = Blue 2 = Green 3 = Aqua
  35.   4 = Red 5 = Purple 6 = Yellow 7 = Gray
  36.   8 = Dark Gray 9 = Brit Blue A = Brit Green B = Brit Aqua
  37.   C = Brit Red D = Brit Purple E = Brit Yellow F = Brit White
  38. */
  39.  
  40. #define Color_Preprocess 2
  41. #define Color_Comment 6
  42. #define Color_Char 4
  43. #define Color_String 12
  44. #define Color_Numeric 5
  45. #define Color_Datatypes 10
  46. #define Color_Conditional 14
  47. #define Color_Function 11
  48. #define Color_Loop 15+9*16
  49.  
  50. int run_check1=0, run_check2=0, keyword_filter=0, keyword_found=0, function_found=0, function_flag=0;
  51.  
  52. int read_file();
  53. int check1();
  54. int check2(int);
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58. if(argc>1) fp=fopen(argv[1], "r"); else fp=fopen("Detect5R2.cpp","r");
  59. if(!fp){ cout<<"WARNING: A file doesn't exist.\n"; exit(0); }
  60.  
  61. system("color 0f");
  62. read_file();
  63.  
  64. printf("\n\"#\" check1: %4d check2: %4d\
  65. keyword filter: %4d keyword found: %4d\
  66. function found: %4d\
  67. \n", run_check1, run_check2, keyword_filter, keyword_found, function_found);
  68.  
  69. system("pause");
  70. return 0;
  71. }
  72.  
  73. void readtill(int *buffer, int code)
  74. {
  75. cout<<(char)*buffer;
  76. for(*buffer=fgetc(fp); *buffer!=EOF && *buffer!=code; *buffer=fgetc(fp))
  77. cout<<(char)*buffer;
  78. }
  79.  
  80. int i, buffer; char word[20];
  81. int compare(char word[])
  82. {
  83. keyword_filter++;
  84. switch(i)
  85. {
  86. case 2: if(strcmp(word, "if")==0) return 'c';
  87. else if(strcmp(word, "do")==0) return 'l';
  88. break;
  89.  
  90. case 3: if(strcmp(word, "for")==0) return 'l';
  91. else if(strcmp(word, "int")==0) return 'd';
  92. break;
  93.  
  94. case 4: if(strcmp(word, "case")==0) return 'c';
  95. else if(strcmp(word, "char")==0) return 'd';
  96. else if(strcmp(word, "else")==0) return 'c';
  97. else if(strcmp(word, "void")==0) return 'd';
  98. else if(strcmp(word, "goto")==0) return 'k';
  99. break;
  100.  
  101. case 5: if(strcmp(word, "while")==0) return 'l';
  102. else if(strcmp(word, "using")==0) return 'k';
  103. else if(strcmp(word, "break")==0) return 'k';
  104. else if(strcmp(word, "const")==0) return 'k';
  105. break;
  106.  
  107. case 6: if(strcmp(word, "switch")==0) return 'l';
  108. break;
  109.  
  110. case 8: if(strcmp(word, "continue")==0) return 'k';
  111. }
  112. //Marker(175);
  113. return 0;
  114. }
  115.  
  116. int check2(int flag_keyword)
  117. {
  118. run_check2++;
  119. i=0;
  120.  
  121. for(;;)
  122. {
  123. if((buffer>='A' && buffer<='Z') || (buffer>='0' && buffer<='9') || (buffer=='_') ) flag_keyword=0;
  124. else if(!(buffer>='a' && buffer<='z')) break;
  125. word[i++]=buffer;
  126. buffer_pp;
  127. }
  128. word[i]='\0';
  129.  
  130. if(flag_keyword==1) switch(compare(word))
  131. {
  132. case 'l': TxtAttrib(Color_Loop); i=-'l'; break;
  133. case 'c': TxtAttrib(Color_Conditional); i=-'c'; break;
  134. case 'd': TxtAttrib(Color_Datatypes); i=-'d'; break;
  135. case 'k': TxtAttrib(13); i=-'k'; break;
  136. case 0: goto function;
  137. }
  138. else
  139. {
  140. function:
  141. //Marker(173);
  142. if(buffer=='(')
  143. {
  144. function_found++; function_flag+=2;
  145. TxtAttrib(Color_Function);
  146. cout<<word<<'(';
  147. DefTxt;
  148. until:
  149. buffer_pp;
  150. if(check1()==')' && function_flag>0)
  151. {
  152. function_flag--;
  153. TxtAttrib(Color_Function);
  154. cout<<"\b)";
  155. DefTxt;
  156. }
  157. else
  158. {
  159. if(!((buffer>='0' && buffer<='9') ||
  160. buffer==',' ||
  161. buffer=='*' ||
  162. buffer=='[' || buffer==']'
  163. )
  164. ) //Quick Patch
  165. cout<<(char)buffer;
  166. goto until;
  167. }
  168. buffer_pp;
  169. return 1;
  170. }
  171. }
  172.  
  173. cout<<word;
  174.  
  175. //if(i>1) Marker(174);
  176.  
  177. if(i<0) { keyword_found++; DefTxt;} //change colour only when needed
  178. return 0;
  179. }
  180. int stat;
  181. int check1()
  182. {
  183. run_check1++;
  184. if(buffer>='0' && buffer<='9')
  185. {
  186. TxtAttrib(Color_Numeric);
  187. cout<<(char)buffer;
  188. DefTxt;
  189. return buffer;
  190. }
  191.  
  192. if((buffer>='A' && buffer<='Z') || buffer=='_') if(check2(0)==EOF) return EOF;
  193. if(buffer>='a' && buffer<='z') if(stat=check2(1)==EOF) return EOF;
  194.  
  195. switch(buffer)
  196. {
  197. case '/': //what about the line extention using escape sequence
  198. buffer_pp;
  199. if(buffer=='/' || buffer == '*')
  200. {
  201. TxtAttrib(Color_Comment);
  202. cout<<'/';
  203. if(buffer=='/') readtill(&buffer, '\n');
  204. else
  205. {
  206. rep:
  207. readtill(&buffer, '*');
  208. cout<<(char)buffer;
  209. buffer_pp;
  210. if(buffer!='/') goto rep;
  211. }
  212. cout<<(char)buffer;
  213. DefTxt;
  214. }
  215. break;
  216.  
  217. case '#':
  218. TxtAttrib(Color_Preprocess);
  219. cout<<'#';
  220. for(buffer=fgetc(fp); buffer!=EOF && buffer!='\n'; buffer=fgetc(fp))
  221. {
  222. cout<<(char)buffer;
  223. if(buffer=='\\')
  224. {
  225. buffer=fgetc(fp); //bypassing the '\n' as per Dev-C++ 4.9.9.2 IDE
  226. cout<<(char)buffer;
  227. continue;
  228. }
  229. }
  230. cout<<(char)buffer;
  231. DefTxt;
  232. break;
  233.  
  234. case '"':
  235. TxtAttrib(Color_String);
  236. cout<<'"';
  237. for(buffer=fgetc(fp); buffer!=EOF && buffer!='\n' && buffer!='"'; buffer=fgetc(fp))
  238. {
  239. cout<<(char)buffer;
  240. if(buffer=='\\')
  241. {
  242. buffer=fgetc(fp); //bypassing the '\n' as per Dev-C++ 4.9.9.2 IDE
  243. cout<<(char)buffer;
  244. continue;
  245. }
  246. }
  247. cout<<(char)buffer;
  248. DefTxt;
  249. break;
  250.  
  251. case 39: //char '
  252. TxtAttrib(Color_Char);
  253. do{ cout<<(char)buffer; buffer_pp; }while(buffer!=39);
  254. cout<<(char)39;
  255. DefTxt;
  256. break;
  257.  
  258. case ' ':
  259. spacekill:
  260. for(;;) //space kill
  261. {
  262. if(buffer==' ') cout<<' ';//(char)250;
  263. else return check1();
  264. buffer_pp;
  265. }
  266. break;
  267.  
  268. case '\n': //cout<<(char)20;
  269. case ';':
  270. cout<<(char)buffer;
  271. buffer_pp;
  272. goto spacekill;
  273.  
  274. case '(': case ')':
  275. if(stat==0) cout<<(char)buffer;
  276. break;
  277.  
  278. default: cout<<(char)buffer;
  279. }
  280. return buffer;
  281. }
  282.  
  283. int read_file()
  284. {
  285. for(;;)
  286. {
  287. buffer_pp; if(check1()==EOF) {cout<<"EOF"; return EOF;}
  288. }
  289. }
0
William Hemsworth William Hemsworth is offline Offline | Jul 10th, 2009
Nice snippet, works perfectly. The only thing I could complain about (other than the formatting) is the fact that you included conio.h.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC