fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. enum TokenType {
  6. OPERATOR,
  7. PARENTHESIS,
  8. EQUAL,
  9. UNKNOWN
  10. };
  11.  
  12. const char *TokenTypeStrings[] = {
  13. "INTEGER",
  14. "OPERATOR",
  15. "PARENTHESIS",
  16. "EQUAL",
  17. "UNKNOWN"
  18. };
  19.  
  20. struct Token {
  21. enum TokenType type;
  22. char *value;
  23. };
  24. %}
  25.  
  26. digit [0-9]
  27. integer {digit}+
  28.  
  29. %%
  30.  
  31. printf("INTEGER: %s\n", yytext);
  32. }
  33.  
  34. [\+\-\*\/] {
  35. printf("OPERATOR: %s\n", yytext);
  36. }
  37.  
  38. [\(\)] {
  39. printf("PARENTHESIS: %s\n", yytext);
  40. }
  41.  
  42. \= {
  43. printf("EQUAL: %s\n", yytext);
  44. }
  45.  
  46. . {
  47. printf("UNKNOWN: %s\n", yytext);
  48. exit(1);
  49. }
  50.  
  51. %%
  52.  
  53. int main(int argc, char *argv[]) {
  54. yylex();
  55. return 0;
  56. }
  57.  
Success #stdin #stdout #stderr 0.03s 6864KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/AM9cxS/prog:2:1: Syntax error: Operator expected
ERROR: /home/AM9cxS/prog:58:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit