fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // Define a list of keywords
  6. char *keywords[] = {"if", "else", "while", "return"};
  7. int is_keyword(char *str) {
  8. for (int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
  9. if (strcmp(str, keywords[i]) == 0) return 1;
  10. }
  11. return 0;
  12. }
  13. %}
  14.  
  15. %%
  16.  
  17. [0-9]+ { printf("NUMBER: %s\n", yytext); }
  18.  
  19. [a-zA-Z_][a-zA-Z0-9_]* {
  20. if (is_keyword(yytext))
  21. printf("KEYWORD: %s\n", yytext);
  22. else
  23. printf("IDENTIFIER: %s\n", yytext);
  24. }
  25.  
  26. [ \t\n]+ ; // Ignore whitespace
  27.  
  28. . { printf("UNKNOWN: %s\n", yytext); }
  29.  
  30. %%
  31.  
  32. int main() {
  33. yylex();
  34. return 0;
  35. }
  36.  
  37. int yywrap() {
  38. return 1;
  39.  
Success #stdin #stdout #stderr 0.03s 6872KB
stdin
if count 123 return abc12 else
if count 123 return abc12 else
stdout
Standard output is empty
stderr
ERROR: /home/P64TGA/prog:2:1: Syntax error: Operator expected
ERROR: /home/P64TGA/prog:38:13: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit