fork download
  1. %option c++
  2. %option noyywrap
  3.  
  4. %{
  5. #include <iostream>
  6. #include <cstdlib> // for exit()
  7. using namespace std;
  8.  
  9. // Required declaration for C++ flex
  10. int yylex();
  11. %}
  12.  
  13. DIGIT [0-9]
  14. ID [a-zA-Z_][a-zA-Z0-9_]*
  15. KEYWORD int|float|if|else|while|return
  16.  
  17. %%
  18.  
  19. {KEYWORD} { cout << "Keyword: " << yytext << endl; }
  20. {ID} { cout << "Identifier: " << yytext << endl; }
  21. {DIGIT}+ { cout << "Integer: " << yytext << endl; }
  22. {DIGIT}+"."{DIGIT}+ { cout << "Float: " << yytext << endl; }
  23. [+\-*/=] { cout << "Operator: " << yytext << endl; }
  24. [;,(){}] { cout << "Punctuation: " << yytext << endl; }
  25. [ \t\n]+ { /* ignore whitespace */ }
  26. . { cerr << "Error: Unrecognized character '" << yytext << "'" << endl; }
  27.  
  28. %%
  29.  
  30. int main(int argc, char** argv) {
  31. if (argc > 1) {
  32. yyin = fopen(argv[1], "r");
  33. if (!yyin) {
  34. cerr << "Error: Cannot open file " << argv[1] << endl;
  35. return 1;
  36. }
  37. }
  38.  
  39. yylex();
  40.  
  41. if (argc > 1) {
  42. fclose(yyin);
  43. }
  44.  
  45. return 0;
  46. }
Success #stdin #stdout #stderr 0.03s 6948KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/ieVqy8/prog:5:1: Syntax error: Operator expected
ERROR: /home/ieVqy8/prog:46:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit