Perbaikan Ujian Praktikum Kompiler


NAMA : VASKO EDO GULTOM
NIM : 071402009

Perbaikan Ujian Praktikum Kompiler

#include
#include
using namespace std;

string keyword[] = {“PROGRAM”,”MULAI”,”AKHIR”};
string delimiter[] ={“;”,”.”,”=”};

string getNextLexeme(string s,int *pos){
string lexeme=””;
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 isDelimiter(string lexeme)
{
for (int i=0;i<sizeof(delimiter)/sizeof(string);i++)

if(lexeme == delimiter[i])
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= 1)
{
ok &= (tmp[0] >= ‘0’ && tmp[0] <= '9');
for (int i = 1, n = tmp.size(); i = ‘0’ && tmp[i] <= '9');
}
else
ok = false;

return ok;
}

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 (is KeyWord(lexeme) */
if(isNumber(lexeme))
cout<<"number"<<endl;
else if(isKeyword(lexeme))
cout<<"keyword"<<endl;
else if(isIdentifier(lexeme))
cout<<"nama"<<endl;
else if (isDelimiter(lexeme))
cout<<" delimiter"<<endl;
else
{
cout<<"invalid"<<endl;
leksikal = false;
break;
}
}

}
}//analisis leksikal

int ind = 0;
bool sintaksis = false;

if(leksikal)
{
cout<<"————————" <<endl;
cout <<" Analisis Leksikal : OK" <<endl;
cout<<"————————" <<endl;
//aturan pendeklarasian program
if(isKeyword(token[0]) && token[0] == "PROGRAM")
{
if(isIdentifier(token[1]))
{
if(isDelimiter(token[2]))
{
if (token [3] == "MULAI")
{
ind =4;
bool ok =false;
while (token[ind] != "AKHIR")
{
ok = false;
if (isIdentifier(token[ind]))
{
if (token[++ind] == "=")
{
if (isNumber (token[++ind]))
{
if(isDelimiter(token[++ind]))
ok = true;
}
}

}
if (!ok)
break;
ind++;
}
if(ok)
{
if(token [ind] =="AKHIR")
if (token[++ind] == ".")
sintaksis =true;
}
}
}
// sintaksis = true;
}
}
}
if (sintaksis)
{
cout<<"————————" <<endl;
cout<<" Analisis Sintaksis :OK" <<endl;
cout<<"————————" <<endl;
}
else
{
cout<<"————————" <<endl;
cout<<" Analisis Sintaksis :ERROR" <<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