Hello! I'm trying to build basic compiler on C++. When I call function Lex it's call function Recognise_Identifier_Keyword twice and then the program end. I don't know why the function Lex doesn't call the other two functions. I'm testing the code with string if (x == 5) { y = 10 } else { y <= 20; } here is the code:
C++:
#include <iostream>
#include <string>
using namespace std;
struct Table
{
int Position{ 0 };
char Symbol[254]{ "" };
int Code{ 0 };
Table* next;
};
typedef Table* point;
//typedef Table* pointer();
//point symbolTable;
point symbolTable = NULL;
point P, Check, Prev;
point Last = NULL;
//point Del = NULL;
int Create(point& symbolTable, string symbol, int kod) {
P = new Table;
strncpy_s(P->Symbol, symbol.c_str(), sizeof(P->Symbol - 1));
P->Code = kod;
// P->Symbol = str;
Check = symbolTable;
P->next = NULL;
while (Check != NULL) {
if (strcmp(P->Symbol, Check->Symbol) == 0) {
return Check->Position;
} //if
Check = Check->next;
P->Position++;
} //while
if (symbolTable == NULL) { symbolTable = P; }
else { Last->next = P; }
Last = P;
P->Position++;
return P->Position;
} // Create()
void show_Lex_Error(const string& programText) {
cout << "LEX error" << programText;
}
int token = 100;
bool Recognise_Identifier_Keyword(const string& programText, point& symbolTable) {
cout << "test indentifier \n";
int count = 0;
// string buffer = "";
for (int i = 0; i < programText.size(); i++) {
if (isalnum(programText[i]) && isalpha(programText[0])) {
count++;
}
}
if (programText == "if" || programText == "else" || programText == "while" || programText == "break") {
token++;
Create(symbolTable, programText, token);
return true;
}
if (programText.size() == count) {
Create(symbolTable, programText, 1);
return true;
}
else { return false; }
}
int konst = 200;
bool Recognise_Number(const string& programText, point& symbolTable) {
cout << "test number \n";
int count = 0, dotCounter = 0;
// string buffer = "";
for (int i = 0; i < programText.size(); i++) {
if (isdigit(programText[i]) || dotCounter < 1) {
if (programText[i] == '.') {
dotCounter++;
}
count++;
}
}
if (programText.size() == count) {
konst++;
Create(symbolTable, programText, konst);
return true;
}
else { return false; }
}
int op = 300;
bool Recognise_Operator(const string& programText, point& symbolTable) {
cout << "test operator \n";
if (programText == "=>" || programText == "<=" || programText == "==" || programText == "!=" || programText == "=" || programText == "+" || programText == "-" || programText == "*" || programText == "/" || programText == "(" || programText == ")" || programText == "[" || programText == "]" || programText == "{" || programText == "}")
{
op++;
Create(symbolTable, programText, op);
return true;
}
else { return false; }
}
void Lex(const string& programText, point& symbolTable) {
string buffer ="";
char symbol = ' ';
for (int i = 0; i < programText.size(); i++) {
if (programText[i] == ' ') {
Recognise_Identifier_Keyword(buffer, symbolTable);
Recognise_Number(buffer, symbolTable);
Recognise_Operator(buffer, symbolTable);
}
if (!Recognise_Identifier_Keyword(buffer, symbolTable) &&
!Recognise_Number(buffer, symbolTable) &&
!Recognise_Operator(buffer, symbolTable)) {
show_Lex_Error(buffer);
// return; // Exit the function when an error is detected
}
/* if (programText[i] == ' ') {
if (!Recognise_Identifier_Keyword(buffer, symbolTable) &&
!Recognise_Number(buffer, symbolTable) &&
!Recognise_Operator(bu ffer, symbolTable)) {
show_Lex_Error(buffer);
return; // Exit the function when an error is detected
}
}
*/
symbol = programText[i];
buffer += symbol;
if (programText[i] == ' ') {
buffer = "";
}
}
}
void printirane(point P) {
cout << "position" << "\t" << "symbol" << "\t" << "token" << "\n";
while (P != NULL) // или while (P)
{
cout << P->Position << "\t " << P->Symbol << "\t" << P->Code << "\n";
P = P->next;
} //while
} //printirane
void main() {
system("chcp 1251");
string symbol;
int token;
int number;
do {
cout << "1) fill the symbolTable. \n";
cout << "2) print the symbolTable. \n";
cout << "0) exit. \n";
cout << "enter a number: ";
cin >> number;
cout << "\n";
switch (number)
{
case 1:
cout << "enter string: ";
// getline(cin, symbol);
cin >> symbol;
Lex(symbol, symbolTable);
break;
case 2:
printirane(symbolTable);
cout << "\n";
break;
}
} while (number != 0);
}
// if (x == 5) { y = 10 } else { y <= 20; }