fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int ntoken=0;
  5. %}
  6. %%
  7. [ \t\n]+ ;
  8. [0-9]+(\.[0-9]+)? { ntoken++; printf("NUMBER(%s) ", yytext); }
  9. [A-Za-z_][A-Za-z0-9_]* { ntoken++; printf("IDENTIFIER(%s) ", yytext); }
  10. "+"|"-"|"*"|"/"|"="|"=="|"!="|"<"|">"|"<="|">=" { ntoken++; printf("OPERATOR(%s) ", yytext); }
  11. "(" { ntoken++; printf("LPAREN "); }
  12. ")" { ntoken++; printf("RPAREN "); }
  13. "{" { ntoken++; printf("LCURLY "); }
  14. "}" { ntoken++; printf("RCURLY "); }
  15. ";" { ntoken++; printf("SEMICOLON "); }
  16. "," { ntoken++; printf("COMMA "); }
  17. . { ntoken++; printf("UNKNOWN(%s) ", yytext); }
  18. %%
  19. int main()
  20. {
  21. char input[1024];
  22. printf("Enter input: ");
  23. fgets(input, sizeof(input), stdin);
  24. YY_BUFFER_STATE buffer = yy_scan_string(input);
  25. yylex();
  26. yy_delete_buffer(buffer);
  27. printf("\nTotal number of tokens = %d\n", ntoken);
  28. return 0;
  29. }
  30.  
  31.  
Success #stdin #stdout #stderr 0.03s 6800KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/oWGN22/prog:2:1: Syntax error: Operator expected
ERROR: /home/oWGN22/prog:30:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit