Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stdlib.h>
- #include <algorithm>
- #include <vector>
- #include "lex.h"
- using namespace std;
- int main(int argc, char *argv[]) {
- bool print_all_token = false;
- bool print_all_constants = false;
- bool print_all_ids = false;
- bool rigged_filename_pos = false;
- string filename = "";
- filename.clear();
- for (int i = 1; i < argc; i++) {
- if (rigged_filename_pos && argv[i][0] == '-') {
- cout << "UNRECOGNIZED FLAG " << filename << endl;
- return 1;
- }
- if (i == argc - 1) {
- if (argv[i][0] != '-') {
- if (!rigged_filename_pos) {
- rigged_filename_pos = true;
- filename = string(argv[i]);
- continue;
- } else {
- cout << "ONLY ONE FILE NAME ALLOWED" << endl;
- return 0;
- }
- }
- } else {
- if (argv[i][0] != '-') {
- if (!rigged_filename_pos) {
- rigged_filename_pos = true;
- filename = string(argv[i]);
- continue;
- } else {
- cout << "ONLY ONE FILE NAME ALLOWED" << endl;
- return 0;
- }
- }
- }
- if (string(argv[i]).compare("-v") == 0)
- print_all_token = true;
- else if (string(argv[i]).compare("-consts") == 0)
- print_all_constants = true;
- else if (string(argv[i]).compare("-ids") == 0)
- print_all_ids = true;
- else {
- cout << "UNRECOGNIZED FLAG " << (argv[i]) << endl;
- return 1;
- }
- }
- istream *istreamp = &(cin);
- fstream fs;
- if (rigged_filename_pos) {
- fs.open(filename, fstream::in);
- if (!fs) {
- cout << "CANNOT OPEN " << filename << endl;
- return 1;
- }
- istreamp = &fs;
- }
- vector<string> ids;
- vector<string> strings;
- vector<int> ints;
- vector<Lex> totals;
- Lex nextLex;
- int linenum = 0;
- do {
- nextLex = getNextToken(*istreamp, linenum);
- totals.push_back(nextLex);
- if (nextLex.GetToken() == STR) {
- strings.push_back(nextLex.GetLexeme());
- } else if (nextLex.GetToken() == INT) {
- ints.push_back(atoi(nextLex.GetLexeme().c_str()));
- } else if (nextLex.GetToken() == ID) {
- ids.push_back(nextLex.GetLexeme());
- }
- if (print_all_token && nextLex.GetToken() != ERR && nextLex.GetToken() != DONE)
- cout << nextLex << endl;
- else if (nextLex.GetToken() == ERR) {
- cout << nextLex << endl;
- return 1;
- }
- } while (nextLex.GetToken() != ERR && nextLex.GetToken() != DONE);
- sort(strings.begin(), strings.end());
- strings.erase(unique(strings.begin(), strings.end()), strings.end());
- sort(ints.begin(), ints.end());
- ints.erase(unique(ints.begin(), ints.end()), ints.end());
- sort(ids.begin(), ids.end());
- ids.erase(unique(ids.begin(), ids.end()), ids.end());
- if (!filename.empty())
- fs.close();
- if (print_all_constants) {
- if (strings.size() != 0) {
- cout << "STRINGS:" << endl;
- for (int i = 0; i < strings.size(); i++) {
- cout << strings[i] << endl;
- }
- }
- if (ints.size() != 0) {
- cout << "INTERGERS:"<< endl;
- for (int i = 0; i < ints.size(); i++) {
- cout << ints[i] << endl;
- }
- }
- }
- if (print_all_ids) {
- if (ids.size() != 0) {
- cout << "IDENTIFIERS: ";
- for (int i = 0; i < ids.size() - 1; i++) {
- cout << ids[i] << ", ";
- }
- cout << ids[ids.size() - 1] << endl;
- }
- }
- cout << "Lines: " << to_string(linenum) << endl;
- if (linenum != 0) {
- cout << "Tokens: " << to_string(totals.size() - 1) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement