fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAX_SYMBOLS 200
  7.  
  8. struct Symbol {
  9. char name[100];
  10. char type[30];
  11. };
  12.  
  13. struct Symbol symbolTable[MAX_SYMBOLS];
  14. int symbolCount = 0;
  15.  
  16. void addSymbol(const char *name, const char *type) {
  17. for (int i = 0; i < symbolCount; ++i) {
  18. if (strcmp(symbolTable[i].name, name) == 0)
  19. return;
  20. }
  21. strcpy(symbolTable[symbolCount].name, name);
  22. strcpy(symbolTable[symbolCount].type, type);
  23. symbolCount++;
  24. }
  25.  
  26. void tokenize(const char *input) {
  27. int i = 0;
  28. char token[100];
  29.  
  30. while (input[i] != '\0') {
  31.  
  32. if (isspace((unsigned char)input[i])) {
  33. i++;
  34. continue;
  35. }
  36.  
  37. if (input[i] == '(' || input[i] == ')') {
  38. token[0] = input[i];
  39. token[1] = '\0';
  40. addSymbol(token, "PARENTHESIS");
  41. i++;
  42. continue;
  43. }
  44.  
  45. if (strchr("|*+.-", input[i])) {
  46. token[0] = input[i];
  47. token[1] = '\0';
  48. addSymbol(token, "OPERATOR");
  49. i++;
  50. continue;
  51. }
  52.  
  53. if (input[i] == '\\') {
  54. if (input[i + 1] == 'n' || input[i + 1] == 't' || input[i + 1] == '\\') {
  55. token[0] = '\\';
  56. token[1] = input[i + 1];
  57. token[2] = '\0';
  58. addSymbol(token, "ESCAPE_SEQUENCE");
  59. i += 2;
  60. continue;
  61. }
  62. }
  63.  
  64. if (input[i] == '\'' && input[i + 2] == '\'') {
  65. token[0] = input[i];
  66. token[1] = input[i + 1];
  67. token[2] = input[i + 2];
  68. token[3] = '\0';
  69. addSymbol(token, "LITERAL");
  70. i += 3;
  71. continue;
  72. }
  73.  
  74. if (strncmp(&input[i], "lambda", 6) == 0) {
  75. addSymbol("lambda", "EMPTY_STRING");
  76. i += 6;
  77. continue;
  78. } else if (strncmp(&input[i], "null", 4) == 0) {
  79. addSymbol("null", "EMPTY_STRING");
  80. i += 4;
  81. continue;
  82. } else if ((unsigned char)input[i] == 0xCE && (unsigned char)input[i + 1] == 0xB5) {
  83. // UTF-8 for ε
  84. strcpy(token, "ε");
  85. addSymbol(token, "EMPTY_STRING");
  86. i += 2;
  87. continue;
  88. } else if (input[i] == 'ε') {
  89. token[0] = 'ε';
  90. token[1] = '\0';
  91. addSymbol(token, "EMPTY_STRING");
  92. i++;
  93. continue;
  94. }
  95.  
  96. if (isalpha((unsigned char)input[i]) || input[i] == '_') {
  97. int j = 0;
  98. while (isalnum((unsigned char)input[i]) || input[i] == '_') {
  99. token[j++] = input[i++];
  100. }
  101. token[j] = '\0';
  102. addSymbol(token, "IDENTIFIER");
  103. continue;
  104. }
  105.  
  106. if (isdigit((unsigned char)input[i])) {
  107. int j = 0;
  108. while (isdigit((unsigned char)input[i])) {
  109. token[j++] = input[i++];
  110. }
  111. token[j] = '\0';
  112. addSymbol(token, "NUMBER");
  113. continue;
  114. }
  115. token[0] = input[i];
  116. token[1] = '\0';
  117. printf("Unknown token: %s\n", token);
  118. i++;
  119. }
  120. }
  121.  
  122. int main() {
  123. char input[1024];
  124.  
  125. printf("Enter a regular expression:\n");
  126. fflush(stdout);
  127.  
  128. if (fgets(input, sizeof(input), stdin) == NULL) {
  129. fprintf(stderr, "Error reading input.\n");
  130. return 1;
  131. }
  132.  
  133. tokenize(input);
  134. +
  135. printf("\nSymbol Table:\n");
  136. printf("-------------------------------------------------------------------\n");
  137. printf("%-20s %-20s %-20s\n", "Token", "Type", "Address");
  138. printf("-------------------------------------------------------------------\n");
  139. for (int i = 0; i < symbolCount; ++i) {
  140. printf("%-20s %-20s %p\n", symbolTable[i].name, symbolTable[i].type, (void*)&symbolTable[i]);
  141. }
  142. printf("-------------------------------------------------------------------\n");
  143.  
  144. return 0;
  145. }
  146.  
Success #stdin #stdout #stderr 0.03s 6936KB
stdin
(a+b)
stdout
Standard output is empty
stderr
ERROR: /home/yA14kD/prog:145:1: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit