Advertisement
Guest User

flex

a guest
May 9th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. %{
  2. #include <iostream>
  3. using namespace std;
  4. %}
  5.  
  6.     int num_chars = 0, num_numbers = 0, num_strings = 0, num_symbols = 0, num_comments = 0;
  7.  
  8. NUMBERS     [-+]?(0|[1-9][0-9]*)
  9. FLOAT       {NUMBERS}("."[0-9]+)
  10. STRINGS     \"(\\.|[^\n"\\]*)\"
  11. SYMBOLS     [a-zA-Z][^\,\.\"\(\) \n\t]*
  12. COMMENTS    ";"[^\n]*
  13.  
  14. %%
  15.  
  16. [ \t\n]+    ;
  17.  
  18. {FLOAT}     {
  19.                 ++num_numbers;
  20.                 num_chars += yyleng;
  21.  
  22.                 cout << "Float: -->" << yytext << "<--" << endl;
  23.                 //cout << "Float: -->"; ECHO; cout << "<--" << endl;
  24. }
  25.  
  26. {NUMBERS}   {
  27.                 ++num_numbers;
  28.                 num_chars += yyleng;
  29.  
  30.                 cout << "Number: -->" << yytext << "<--" << endl;
  31.                 //cout << "Number: -->"; ECHO; cout << "<--" << endl;
  32. }
  33.  
  34. {SYMBOLS}   {
  35.                 ++num_symbols;
  36.                 num_chars += yyleng;
  37.  
  38.                 cout << "Symbol: -->" << yytext << "<--" << endl;
  39.                 //cout << "Symbol: -->"; ECHO; cout << "<--" << endl;
  40. }
  41.  
  42. {STRINGS}   {
  43.                 ++num_strings;
  44.                 num_chars += yyleng;
  45.  
  46.                 cout << "String: -->" << yytext << "<--" << endl;
  47.                 //cout << "String: -->"; ECHO; cout << "<--" << endl;
  48. }
  49.  
  50. {COMMENTS}  {
  51.                 ++num_comments;
  52.                 num_chars += yyleng;
  53.  
  54.                 cout << "Comment: -->" << yytext << "<--" << endl;
  55.                 //cout << "Comment: -->"; ECHO; cout << "<--" << endl;
  56. }
  57.  
  58. .   ;
  59.  
  60. %%
  61.  
  62. int main()
  63. {
  64.     FILE *myfile = fopen("data.txt", "r");
  65.     if (!myfile) {
  66.         cout << "Error opening \"data.txt\"!" << endl;
  67.        
  68.         return -1;
  69.     }
  70.     yyin = myfile;
  71.    
  72.     yylex();
  73.    
  74.     printf("\n# of numbers = %d | # of strings = %d | # of symbols = %d | # of comments = %d;\n", num_numbers, num_strings, num_symbols, num_comments);
  75.     printf("# of chars = %d;\n", num_chars);
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement