//============================================================================ // Task : Compiler // Description : Lexical Analyzer // Name : Mahmoud Mohamed Fathy // ID : 2012030135 //============================================================================ #include #include #include using namespace std; const string reservedWords[] = { "include", "iostream", "namespace", "using", "std", "void", "main", "int", "float", "char", "string" }; const char alphabets[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; const char numbers[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; const char operators[] = { '+', '-', '*', '/', '%' }; const char specialSymbols[] ={';','{','}','(',')','=','\'','\"','<','>','#',',','!','.'}; bool isIdentifier(string word) { int counter = 0; for (int i = 0; i < word.size(); i++) { if (i == 0) { for (int j = 0; j < sizeof(alphabets); j++) { if (word.at(i) == alphabets[j]) { counter++; break; } } } else { for (int j = 0; j < sizeof(alphabets); j++) { if (word.at(i) == alphabets[j]) { counter++; goto end_else; } } for (int k = 0; k < sizeof(numbers); k++) { if (word.at(i) == numbers[k]) { counter++; } } end_else: ; } } if (counter == word.size()) return true; return false; } bool isInteger(string word) { int counter = 0; for (int i = 0; i < word.size(); i++) { for (int j = 0; j < sizeof(numbers); j++) { if (word.at(i) == numbers[j]) { counter++; break; } } } if (counter == word.size()) return true; return false; } bool isReservedWord(string word) { for (int i=0;i> word; if (previousWord == "integer") { if (isOperator(word)) { cout << word << "\tis arithmetic operator\n"; previousWord = "operator"; } else { goto startOfElse; } } else if (previousWord == "operator") { if (isInteger(word)) { cout << word << "\tis Integer\n"; previousWord = "integer"; } else { cout<