Created
April 27, 2015 21:49
-
-
Save AdmiinX/4f6dc348dcf9098e5ab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include < iostream > | |
using namespace ; | |
void main ( ) { | |
int result ; | |
result = 321 + 123 ; | |
50 + 20 + 874 + 123h32 ; | |
int result2 = 764 + 259 + void ; | |
786das | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================ | |
// Task : Compiler | |
// Description : Lexical Analyzer | |
// Name : Mahmoud Mohamed Fathy | |
// ID : 2012030135 | |
//============================================================================ | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
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<sizeof(reservedWords)/sizeof(reservedWords[i]);i++) { | |
if (word == reservedWords[i]) | |
return true; | |
} | |
return false; | |
} | |
bool isOperator(string word) { | |
for (int i = 0; i < sizeof(operators); i++) { | |
if (word.at(0) == operators[i]) | |
return true; | |
} | |
return false; | |
} | |
bool isSpecialSymbols(string word) { | |
int counter = 0; | |
for (int j = 0; j < sizeof(specialSymbols); j++) { | |
if (word[0] == specialSymbols[j]) | |
return true; | |
} | |
return false; | |
} | |
int main() { | |
ifstream myfile; | |
string word; | |
string previousWord; | |
myfile.open("code.txt"); | |
while (!myfile.eof()) { | |
myfile >> 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<<word<<"\tError in position "<<myfile.tellg()<< " not integer after operator" << endl; | |
goto startOfElse; | |
} | |
} | |
else { | |
startOfElse: if (isInteger(word)) { | |
cout << word << "\tis Integer\n"; | |
previousWord = "integer"; | |
} else if (isReservedWord(word)) { | |
cout << word << "\tis ReservedWord\n"; | |
previousWord = "reservedWord"; | |
} else if (isIdentifier(word)) { | |
cout << word << "\tis Identifier\n"; | |
previousWord = "Identifier"; | |
} else if (isSpecialSymbols(word)) { | |
cout << word << "\tis SpecialSymbols\n"; | |
previousWord = "SpecialSymbols"; | |
} else if (isOperator(word) && previousWord != "integer") { | |
cout<<word<<"\tError in position "<<myfile.tellg()<<" not integer before operator"; | |
cout << word << "\tis arithmetic operator\n"; | |
previousWord = "operator"; | |
} else { | |
cout<<word<<"\tError in position "<<myfile.tellg()<<" could not be resolved"<<endl; | |
previousWord = ""; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment