fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. FILE *yyin;
  7. %}
  8.  
  9. digit [0-9]
  10. id [A-Za-z][A-Za-z0-9]*
  11. li\"[^\"]*\" // Define a string literal pattern
  12.  
  13. %%
  14.  
  15. void|main|int|printf { printf("\nA keyword: %s\n", yytext); }
  16. {digit}+ { printf("\nAn integer: %s (%d)\n", yytext, atoi(yytext)); }
  17. {digit}+"."{digit}* { printf("\nA float: %s (%g)\n", yytext, atof(yytext)); }
  18. {id} { printf("\nAn identifier: %s\n", yytext); }
  19. "+"|"-"|"*"|"/"|"=" { printf("\nAn operator: %s\n", yytext); }
  20. ";"|"("|")"|":"|","|"}"|"{" { printf("\n%s is a punctuation\n", yytext); }
  21. #{id}"<"{id}"."{id}">" { printf("\n%s is a preprocessor directive\n", yytext); }
  22. {li}"%"{id}+{li} { printf("\n%s is a literal string\n", yytext); }
  23. {li}{id}+{li} { printf("\n%s is a literal string\n", yytext); }
  24.  
  25. %%
  26.  
  27. int main(int argc, char **argv) {
  28. if (argc > 1) {
  29. yyin = fopen(argv[1], "r");
  30. if (yyin == NULL) {
  31. printf("Cannot open file %s\n", argv[1]);
  32. return 1;
  33. }
  34. } else {
  35. yyin = stdin;
  36. }
  37.  
  38. yylex(); // Call the lexer to process the input file or stdin
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout #stderr 0.03s 6932KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/a8KvKE/prog:41:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit