fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. // List of C keywords
  5. char *keywords[] = {
  6. "auto", "break", "case", "char", "const", "continue", "default", "do",
  7. "double", "else", "enum", "extern", "float", "for", "goto", "if",
  8. "int", "long", "register", "return", "short", "signed", "sizeof",
  9. "static", "struct", "switch", "typedef", "union", "unsigned",
  10. "void", "volatile", "while"
  11. };
  12. int is_keyword(char *word) {
  13. int i;
  14. for (i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
  15. if (strcmp(word, keywords[i]) == 0)
  16. return 1;
  17. }
  18. return 0;
  19. }
  20. %}
  21. %%
  22. [a-zA-Z_][a-zA-Z0-9_]* {
  23. if (is_keyword(yytext))
  24. printf("Keyword: %s\n", yytext);
  25. else
  26. printf("Identifier: %s\n", yytext);
  27. }
  28. [0-9]+\.[0-9]+ {
  29. printf("Constant (float): %s\n", yytext);
  30. }
  31. [0-9]+ {
  32. printf("Constant: %s\n", yytext);
  33. }
  34.  
  35. "=="|"!="|"<="|">="|"&&"|"||"|"++"|"--" {
  36. printf("Operator: %s\n", yytext);
  37. }
  38. [+\-*/%=<>&|^~!] {
  39. printf("Operator: %s\n", yytext);
  40. }
  41.  
  42. [(){}\[\],;:.?] {
  43. printf("Special Character: %s\n", yytext);
  44. }
  45. [ \t\n]+ ; // Ignore whitespace
  46. . {
  47. printf("Unknown: %s\n", yytext);
  48. }
  49. %%
  50. int main(int argc, char **argv) {
  51. yylex();
  52. return 0;
  53. }
  54. int yywrap() {
  55. return 1;
  56. }
  57.  
Success #stdin #stdout #stderr 0.02s 3352KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/run-20049/prog:2:1: Syntax error: Operator expected
ERROR: /home/run-20049/prog:56:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit