fork download
  1. /* Define tokens */
  2. %{
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. %}
  7.  
  8. %%
  9.  
  10. /* Define patterns and corresponding actions */
  11. "STORE" { printf("STORE\n"); }
  12. "LOAD" { printf("LOAD\n"); }
  13. "MACRO" { printf("MACRO\n"); }
  14. "MEND" { printf("MEND\n"); }
  15. "ADD" { printf("ADD\n"); }
  16. "DIV" { printf("DIV\n"); }
  17. "END" { printf("END\n"); }
  18. [a-zA-Z_][a-zA-Z0-9_]* { printf("ID: %s\n", yytext); }
  19. [0-9]+ { printf("NUM: %s\n", yytext); }
  20. [ \t\n] { /* Ignore whitespace and newline characters */ }
  21.  
  22. %%
  23.  
  24. int main(int argc, char* argv[]) {
  25. /* Open input file */
  26. FILE* fp = fopen(argv[1], "r");
  27. if (fp == NULL) {
  28. perror("Error opening file");
  29. exit(EXIT_FAILURE);
  30. }
  31.  
  32. /* Set input file */
  33. yyin = fp;
  34.  
  35. /* Call the lexer */
  36. yylex();
  37.  
  38. /* Close input file */
  39. fclose(fp);
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout #stderr 0.03s 6956KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/EyPd0u/prog:42:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit