Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <iostream>
- using namespace std;
- %}
- int num_chars = 0, num_numbers = 0, num_strings = 0, num_symbols = 0, num_comments = 0;
- NUMBERS [-+]?(0|[1-9][0-9]*)
- FLOAT {NUMBERS}("."[0-9]+)
- STRINGS \"(\\.|[^\n"\\]*)\"
- SYMBOLS [a-zA-Z][^\,\.\"\(\) \n\t]*
- COMMENTS ";"[^\n]*
- %%
- [ \t\n]+ ;
- {FLOAT} {
- ++num_numbers;
- num_chars += yyleng;
- cout << "Float: -->" << yytext << "<--" << endl;
- //cout << "Float: -->"; ECHO; cout << "<--" << endl;
- }
- {NUMBERS} {
- ++num_numbers;
- num_chars += yyleng;
- cout << "Number: -->" << yytext << "<--" << endl;
- //cout << "Number: -->"; ECHO; cout << "<--" << endl;
- }
- {SYMBOLS} {
- ++num_symbols;
- num_chars += yyleng;
- cout << "Symbol: -->" << yytext << "<--" << endl;
- //cout << "Symbol: -->"; ECHO; cout << "<--" << endl;
- }
- {STRINGS} {
- ++num_strings;
- num_chars += yyleng;
- cout << "String: -->" << yytext << "<--" << endl;
- //cout << "String: -->"; ECHO; cout << "<--" << endl;
- }
- {COMMENTS} {
- ++num_comments;
- num_chars += yyleng;
- cout << "Comment: -->" << yytext << "<--" << endl;
- //cout << "Comment: -->"; ECHO; cout << "<--" << endl;
- }
- . ;
- %%
- int main()
- {
- FILE *myfile = fopen("data.txt", "r");
- if (!myfile) {
- cout << "Error opening \"data.txt\"!" << endl;
- return -1;
- }
- yyin = myfile;
- yylex();
- printf("\n# of numbers = %d | # of strings = %d | # of symbols = %d | # of comments = %d;\n", num_numbers, num_strings, num_symbols, num_comments);
- printf("# of chars = %d;\n", num_chars);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement