Lexical Analysis on C++

Joined
Feb 8, 2022
Messages
5
Reaction score
0
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; }
 
Joined
Sep 3, 2023
Messages
36
Reaction score
2
cin >> symbol; I guess stops at the whitespace, so it passes 'if' to Lex. then the next loop thru cin >> number; gets the rest of the line.

Changing that to
Code:
        case 1:
            cout << "enter string: ";
            getline(cin>>ws, symbol);
            cout << "PARSING '" << symbol << "'" << "\n";

            Lex(symbol, symbolTable);
            break;


Also I dont have a strncpy_s, but I think its should be
strncpy_s( P->Symbol, 254-1, symbol.c_str(), symbol.length() );

but since I dont have that this worked
Code:
    int len = symbol.length() > 254-1 ? 254-1 : symbol.length();
    strncpy( P->Symbol, symbol.c_str(), len );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top