fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define MAX_SYMBOLS 100
  7. char *symbolTable[MAX_SYMBOLS];
  8. int symbolCount = 0;
  9.  
  10. int yylex(); // Fix: Declare yylex()
  11.  
  12. void addToSymbolTable(char *lexeme) {
  13. for (int i = 0; i < symbolCount; i++) {
  14. if (strcmp(symbolTable[i], lexeme) == 0) return;
  15. }
  16. if (symbolCount < MAX_SYMBOLS) {
  17. symbolTable[symbolCount++] = strdup(lexeme);
  18. }
  19. }
  20. %}
  21.  
  22. %option noyywrap
  23.  
  24. %%
  25.  
  26. [ \t\n]+ /* Ignore whitespace */
  27. \{[^}]*\} /* Ignore comments */
  28.  
  29. "and"|"begin"|"div"|"do"|"else"|"end"|"for"|"function"|"if"|"array"|
  30. "mod"|"not"|"of"|"or"|"procedure"|"program"|"record"|"then"|"to"|
  31. "type"|"var"|"while" { printf("KEYWORD: %s\n", yytext); }
  32.  
  33. [a-zA-Z][a-zA-Z0-9_]* { printf("IDENTIFIER: %s\n", yytext); addToSymbolTable(yytext); }
  34.  
  35. [0-9]+ { printf("INTEGER: %s\n", yytext); }
  36.  
  37. \"[^\"]*\" { printf("STRING: %s\n", yytext); }
  38.  
  39. "+"|"-"|"*"|"="|"<"|"<="|">"|">="|"<>"|"."|","|":"|";"|".."|":="|
  40. "("|")"|"["|"]" { printf("SYMBOL: %s\n", yytext); }
  41.  
  42. . { printf("UNKNOWN TOKEN: %s\n", yytext); }
  43.  
  44. %%
  45.  
  46. int main() {
  47. printf("Enter Pascal-like code:\n");
  48. yylex();
  49.  
  50. printf("\nSymbol Table:\n");
  51. for (int i = 0; i < symbolCount; i++) {
  52. printf("%s\n", symbolTable[i]);
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout #stderr 0.03s 6920KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Zl6pv0/prog:2:1: Syntax error: Operator expected
ERROR: /home/Zl6pv0/prog:56:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit