fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. char key[100][100], head[100][100], dig[100][100], op[100][100], id[100][100];
  6. int i = 0, j = 0, k = 0, a = 0, b = 0, c = 0, d = 0, m = 0, n = 0;
  7. %}
  8.  
  9. /* Define Regular Expressions */
  10. KW "int"|"while"|"if"|"else"|"for"|"char"|"float"|"case"|"switch"
  11. HF "#include<".*">"
  12. OP "+"|"-"|"*"|"/"|"="
  13. DIG [0-9]*|[0-9]*"."[0-9]+
  14. ID [a-zA-Z][a-zA-Z0-9]*
  15.  
  16. %%
  17.  
  18. /* Pattern Matching Rules */
  19. {KW} { strcpy(key[i], yytext); i++; }
  20. {HF} { strcpy(head[j], yytext); j++; }
  21. {DIG} { strcpy(dig[k], yytext); k++; }
  22. {OP} { strcpy(op[m], yytext); m++; }
  23. {ID} { strcpy(id[n], yytext); n++; }
  24. . { /* Ignore other characters */ }
  25.  
  26. %%
  27.  
  28. /* Main Function */
  29. int main() {
  30. yyin = fopen("input.c", "r+");
  31. yylex();
  32.  
  33. printf("\nThe keywords are:");
  34. for (a = 0; a < i; a++) {
  35. printf("\n%s", key[a]);
  36. }
  37.  
  38. printf("\nThe header files are:");
  39. for (b = 0; b < j; b++) {
  40. printf("\n%s", head[b]);
  41. }
  42.  
  43. printf("\nThe digits are:");
  44. for (c = 0; c < k; c++) {
  45. printf("\n%s", dig[c]);
  46. }
  47.  
  48. printf("\nThe operators are:");
  49. for (d = 0; d < m; d++) {
  50. printf("\n%s", op[d]);
  51. }
  52.  
  53. printf("\nThe identifiers are:");
  54. for (d = 0; d < n; d++) {
  55. printf("\n%s", id[d]);
  56. }
  57.  
  58. return 0;
  59. }
  60.  
  61. /* Function to Handle End of File */
  62. int yywrap() {
  63. return 1;
  64. }
  65.  
Success #stdin #stdout #stderr 0.02s 7048KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/SBGHLO/prog:2:1: Syntax error: Operator expected
ERROR: /home/SBGHLO/prog:64:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit