NAMA : VASKO EDO GULTOM NIM : 071402009 Tugas Praktikum Kompiler 1


NAMA : VASKO EDO GULTOM
NIM : 071402009

Tugas Praktikum Kompiler 1

#include
#include
using namespace std;

string keyword[] = {“PROGRAM”, “MULAI”, “AKHIR”};
string delimiter[] = {“;”, “.”};
string aritmatik[] = {“+”, “-“, “*”, “/”};

string getNextLexeme(string s,int *pos) {
string lex = “”;
int i;
for (i = (*pos); i =’a’&&c=’A’&&c=’0’&&c<='9')
return true;
return false;
}

bool isIdentifier(string lexeme) {
if(isAlphabet(lexeme[0])) {
for(int i=1;i<lexeme.length();i++) {
if(!(isAlphabet(lexeme[i]) || isDigit(lexeme[i]) || lexeme[i] == '_') )
return false;
}
return true;
}
return false;
}

bool isNumber(string lexeme) {
for(int i = 0; i < lexeme.length(); i++) {
if(!isDigit(lexeme[i]))
return false;
}
return true;
}

bool isKeyword(string lexeme) {
for(int i = 0; i < sizeof(keyword)/sizeof(string); i++) {
if(keyword[i] == lexeme)
return true;
}
return false;
}

bool isDelimiter(string lexeme) {
for(int i = 0; i < sizeof(delimiter)/sizeof(string); i++)
if(lexeme == delimiter[i])
return true;
return false;
}

bool isAritmatik(string lexeme) {
for(int i = 0; i < sizeof(aritmatik)/sizeof(string); i++)
if(lexeme == aritmatik[i])
return true;
return false;
}

int main() {
string input = "", lexeme="";
ifstream file("input.txt");
string token[100];
int counter = 0;
bool leksikal = true;

// Analisis Leksikal
if(file.is_open()) {
while(!file.eof()) {
getline(file,input);
int pos = 0;
int j = 0;

while((lexeme = getNextLexeme(input,&pos))!= "") {
token[counter++] = lexeme;
cout << lexeme ;
if(isNumber(lexeme))
cout < number” << endl;
else if(isKeyword(lexeme))
cout < kata kunci” << endl;
else if (isIdentifier(lexeme))
cout < nama” << endl;
else if(isDelimiter(lexeme))
cout < delimiter” << endl;
else if(lexeme == "=")
cout < operator penugasan” << endl;
else if(isAritmatik(lexeme))
cout < operator aritmatik” << endl;
else {
cout < invalid” << endl;
leksikal = false;
break;
}
}
}
}

cout << endl;

// Analisis Sintaksis
int ind = 0;
bool sintaksis = false;
if(leksikal){
cout << "————————–" << endl;
cout << " ANALISIS LEKSIKAL OK " << endl;
cout << "————————–" << endl;

//aturan pendeklarasian program
if(token[0] == "PROGRAM")
if(isIdentifier(token[1]))
if(isDelimiter(token[2]))
sintaksis = true;
} else {
cout << "————————–" << endl;
cout << " ANALISIS LEKSIKAL SALAH " << endl;
cout << "————————–" << endl;
}

if(sintaksis){
cout << "————————–" << endl;
cout << " ANALISIS SINTAKSIS OK " << endl;
cout << "————————–" << endl;
} else {
cout << "————————–" << endl;
cout << "ANALISIS SINTAKSIS SALAH " << endl;
cout << "————————–" << endl;
}

cin.get();
return 0;
}

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s