fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int line_num = 1;
  7. int col_num = 1;
  8. int token_count = 0;
  9. int keyword_count = 0;
  10. int identifier_count = 0;
  11. int number_count = 0;
  12. int relop_count = 0;
  13. int unknown_count = 0;
  14.  
  15. typedef struct {
  16. char token_type[20];
  17. char value[100];
  18. int line;
  19. int column;
  20. } TokenInfo;
  21.  
  22. TokenInfo tokens[1000];
  23.  
  24. void add_token(const char* type, const char* value, int line, int col) {
  25. strcpy(tokens[token_count].token_type, type);
  26. strcpy(tokens[token_count].value, value);
  27. tokens[token_count].line = line;
  28. tokens[token_count].column = col;
  29. token_count++;
  30. }
  31.  
  32. void print_statistics() {
  33. printf("\n=================== STATISTICS ===================\n");
  34. printf("Total Tokens: %d\n", token_count);
  35. printf("Keywords: %d\n", keyword_count);
  36. printf("Identifiers: %d\n", identifier_count);
  37. printf("Numbers: %d\n", number_count);
  38. printf("Relational Operators: %d\n", relop_count);
  39. printf("Unknown Tokens: %d\n", unknown_count);
  40. printf("Total Lines: %d\n", line_num);
  41. printf("==================================================\n");
  42. }
  43.  
  44. void print_token_table() {
  45. printf("\n=================== TOKEN TABLE ===================\n");
  46. printf("%-4s %-15s %-15s %-6s %-6s\n", "No.", "Type", "Value", "Line", "Col");
  47. printf("----------------------------------------------------\n");
  48. for(int i = 0; i < token_count; i++) {
  49. printf("%-4d %-15s %-15s %-6d %-6d\n",
  50. i+1, tokens[i].token_type, tokens[i].value,
  51. tokens[i].line, tokens[i].column);
  52. }
  53. printf("====================================================\n");
  54. }
  55.  
  56. void save_to_file() {
  57. FILE *fp = fopen("token_analysis.txt", "w");
  58. if(!fp) {
  59. printf("Error creating output file!\n");
  60. return;
  61. }
  62.  
  63. fprintf(fp, "LEXICAL ANALYSIS REPORT\n");
  64. fprintf(fp, "=======================\n\n");
  65.  
  66. fprintf(fp, "TOKEN TABLE:\n");
  67. fprintf(fp, "%-4s %-15s %-15s %-6s %-6s\n", "No.", "Type", "Value", "Line", "Col");
  68. fprintf(fp, "----------------------------------------------------\n");
  69.  
  70. for(int i = 0; i < token_count; i++) {
  71. fprintf(fp, "%-4d %-15s %-15s %-6d %-6d\n",
  72. i+1, tokens[i].token_type, tokens[i].value,
  73. tokens[i].line, tokens[i].column);
  74. }
  75.  
  76. fprintf(fp, "\nSTATISTICS:\n");
  77. fprintf(fp, "Total Tokens: %d\n", token_count);
  78. fprintf(fp, "Keywords: %d\n", keyword_count);
  79. fprintf(fp, "Identifiers: %d\n", identifier_count);
  80. fprintf(fp, "Numbers: %d\n", number_count);
  81. fprintf(fp, "Relational Operators: %d\n", relop_count);
  82. fprintf(fp, "Unknown Tokens: %d\n", unknown_count);
  83. fprintf(fp, "Total Lines: %d\n", line_num);
  84.  
  85. fclose(fp);
  86. printf("Analysis saved to: token_analysis.txt\n");
  87. }
  88.  
  89. %}
  90.  
  91. DIGIT [0-9]
  92. LETTER [a-zA-Z]
  93. IDENTIFIER {LETTER}({LETTER}|{DIGIT}|_)*
  94. INTEGER {DIGIT}+
  95. FLOAT {DIGIT}*\.{DIGIT}+
  96. WHITESPACE [ \t]+
  97.  
  98. %%
  99.  
  100. "int"|"float"|"char"|"double"|"void"|"if"|"else"|"while"|"for"|"do"|"break"|"continue"|"return"|"switch"|"case"|"default"|"struct"|"union"|"typedef"|"static"|"extern"|"auto"|"register"|"const"|"volatile"|"sizeof"|"long"|"short"|"signed"|"unsigned"|"enum"|"goto" {
  101. printf("Token %-3d: %-15s Value: %-12s Line: %-3d Col: %-3d\n",
  102. token_count + 1, "KEYWORD", yytext, line_num, col_num);
  103. add_token("KEYWORD", yytext, line_num, col_num);
  104. keyword_count++;
  105. col_num += yyleng;
  106. }
  107.  
  108. {IDENTIFIER} {
  109. printf("Token %-3d: %-15s Value: %-12s Line: %-3d Col: %-3d\n",
  110. token_count + 1, "IDENTIFIER", yytext, line_num, col_num);
  111. add_token("IDENTIFIER", yytext, line_num, col_num);
  112. identifier_count++;
  113. col_num += yyleng;
  114. }
  115.  
  116. {NUMBER} {
  117. printf("Token %-3d: %-15s Value: %-12s Line: %-3d Col: %-3d\n",
  118. token_count + 1, "NUMBER", yytext, line_num, col_num);
  119. add_token("NUMBER", yytext, line_num, col_num);
  120. number_count++;
  121. col_num += yyleng;
  122. }
  123.  
  124. "<="|">="|"=="|"!="|"<"|">" {
  125. printf("Token %-3d: %-15s Value: %-12s Line: %-3d Col: %-3d\n",
  126. token_count + 1, "RELATIONAL_OP", yytext, line_num, col_num);
  127. add_token("RELATIONAL_OP", yytext, line_num, col_num);
  128. relop_count++;
  129. col_num += yyleng;
  130. }
  131.  
  132. {WHITESPACE} {
  133. col_num += yyleng;
  134. }
  135.  
  136. \n {
  137. line_num++;
  138. col_num = 1;
  139. }
  140.  
  141. "//".*$ {
  142. col_num += yyleng;
  143. }
  144.  
  145. "/*"([^*]|\*+[^*/])*\*+"/" {
  146. for(int i = 0; i < yyleng; i++) {
  147. if(yytext[i] == '\n') {
  148. line_num++;
  149. col_num = 1;
  150. } else {
  151. col_num++;
  152. }
  153. }
  154. }
  155.  
  156. . {
  157. printf("Token %-3d: %-15s Value: %-12s Line: %-3d Col: %-3d\n",
  158. token_count + 1, "UNKNOWN", yytext, line_num, col_num);
  159. add_token("UNKNOWN", yytext, line_num, col_num);
  160. unknown_count++;
  161. col_num += yyleng;
  162. }
  163.  
  164. %%
  165.  
  166. int yywrap() {
  167. return 1;
  168. }
  169.  
  170. void print_menu() {
  171. printf("\n=============== MENU OPTIONS ===============\n");
  172. printf("1. Show Token Table\n");
  173. printf("2. Show Statistics\n");
  174. printf("3. Save Analysis to File\n");
  175. printf("4. Exit\n");
  176. printf("===========================================\n");
  177. printf("Enter your choice: ");
  178. }
  179.  
  180. int main(int argc, char *argv[]) {
  181. printf("======================================\n");
  182. printf(" ADVANCED LEX TOKENIZER v2.0\n");
  183. printf("======================================\n");
  184. printf("Features: Keywords, Identifiers, Numbers,\n");
  185. printf(" Relational Operators, Comments\n");
  186. printf("======================================\n\n");
  187.  
  188. if(argc > 1) {
  189. FILE *input_file = fopen(argv[1], "r");
  190. if(!input_file) {
  191. printf("Error: Cannot open file '%s'\n", argv[1]);
  192. printf("Usage: %s [input_file.c]\n", argv[0]);
  193. return 1;
  194. }
  195. yyin = input_file;
  196. printf("Analyzing file: %s\n", argv[1]);
  197. printf("======================================\n");
  198. } else {
  199. printf("Enter source code (Ctrl+D to finish):\n");
  200. printf("======================================\n");
  201. }
  202.  
  203. yylex();
  204.  
  205. if(argc > 1) {
  206. fclose(yyin);
  207. }
  208.  
  209. printf("\n======================================\n");
  210. printf(" LEXICAL ANALYSIS COMPLETE\n");
  211. printf("======================================\n");
  212.  
  213. int choice;
  214. do {
  215. print_menu();
  216. scanf("%d", &choice);
  217.  
  218. switch(choice) {
  219. case 1:
  220. print_token_table();
  221. break;
  222. case 2:
  223. print_statistics();
  224. break;
  225. case 3:
  226. save_to_file();
  227. break;
  228. case 4:
  229. printf("Thank you for using Advanced LEX Tokenizer!\n");
  230. break;
  231. default:
  232. printf("Invalid choice! Please try again.\n");
  233. }
  234. } while(choice != 4);
  235.  
  236. return 0;
  237. }
  238.  
Success #stdin #stdout #stderr 0.03s 6936KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/j7jCvt/prog:2:1: Syntax error: Operator expected
ERROR: /home/j7jCvt/prog:238:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit