fork download
  1. %{
  2. #include <stdio.h>
  3. int num_tokens = 0;
  4. %}
  5.  
  6. DIGIT [0-9]
  7. LETTER [a-zA-Z]
  8. ID {LETTER}({LETTER}|{DIGIT})*
  9. NUMBER {DIGIT}+(\.{DIGIT}+)?
  10. WS [ \t\n]+
  11.  
  12. %%
  13.  
  14. "if"|"else"|"while"|"int"|"float"|"return" {
  15. printf("TOKEN: KEYWORD -> %s\n", yytext);
  16. num_tokens++;
  17. }
  18.  
  19. {ID} {
  20. printf("TOKEN: IDENTIFIER -> %s\n", yytext);
  21. num_tokens++;
  22. }
  23.  
  24. {NUMBER} {
  25. printf("TOKEN: NUMBER -> %s\n", yytext);
  26. num_tokens++;
  27. }
  28.  
  29. "+"|"-"|"*"|"/"|"="|"=="|"!="|"<"|">" {
  30. printf("TOKEN: OPERATOR -> %s\n", yytext);
  31. num_tokens++;
  32. }
  33.  
  34. "{"|"}"|"("|")"|";" {
  35. printf("TOKEN: PUNCTUATION -> %s\n", yytext);
  36. num_tokens++;
  37. }
  38.  
  39. {WS} { /* skip whitespace */ }
  40.  
  41. . {
  42. printf("TOKEN: UNRECOGNIZED -> %s\n", yytext);
  43. }
  44.  
  45. %%
  46.  
  47. int main() {
  48. printf("Enter source code (Ctrl+D to end):\n");
  49. yylex();
  50. printf("\nTotal tokens found: %d\n", num_tokens);
  51. return 0;
  52. }
  53.  
  54. int yywrap() {
  55. return 1;
  56. }
  57.  
Success #stdin #stdout #stderr 0.03s 6956KB
stdin
int main() {
    if (x > 5) return 3.14;
}
stdout
Standard output is empty
stderr
ERROR: /home/EvTbLP/prog:2:1: Syntax error: Operator expected
ERROR: /home/EvTbLP/prog:56:1: 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