Hello guys,
I've wrote a symbol table creator that uses hash function to determine the position of the symbol in the table and a linked list approach for the storing process in the table.
And to the point. I've known myself for doing easy problems the hard way(and my teachers tell me that too) and I am bad with pointers, so what I want to ask - is there a better way to do some of the things I did in my code and with a short description why it is so.
What I am after here is to learn better ways to write my code, because I like C++ and I am still learning my ways around it.

Thank you in advance.

Here is my code:

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

// CODE/S OF SYMBOL
struct data{

    int typeSymb = -1;

};

// SYMBOL AND CORESPONDING ATTRIBUTES
struct bucket{

    string   Name = "/0";
    data     Attr;
    bucket*  next = NULL;

};

// SYMBOL TABLE DECLARATION
const int symbolTableSize = 20;
bucket* SymbolTable[ symbolTableSize ];


int hashFunc( string Symbol ){

    int buff = (Symbol.at(0) * Symbol.at( Symbol.size()-1 ))*Symbol.size();
//    int buff = 1;
//    for( int i = 0; i < Symbol.size(); i++ ){
//        buff = buff * Symbol.at(i);
//    }
    return fmod(buff, symbolTableSize );

}

bucket* findLast( bucket* part ){
    if( part->next != NULL )
        findLast( part->next );
    else
        return part;
}

bucket* searchEqual( bucket* part, string Symbol ){

    //IF NOT SYMBOL AND THERE IS NEXT
    if( part->Name.compare( Symbol ) && part->next != NULL )
        return searchEqual( part->next, Symbol );

/* I DO NOT NEED IT BECAUSE THIS COMBINATION IS COVERED IN findTab!*/
//    //IF NOT SYMBOL AND THERE IS NO NEXT
//    else if( part->Name.compare( Symbol ) && part->next == NULL )
//        return NULL;

    //IF FOUND EQUAL
    else
        return part;

}

bucket *findTab( string Symbol, data Attr ){

    int hFunc = hashFunc( Symbol );

    if( !SymbolTable[hFunc] ){

        bucket *temp = new bucket;
        temp->Name.assign(Symbol);
        temp->Attr.typeSymb = Attr.typeSymb;
        temp->next = new bucket;
        delete temp->next;
        temp->next = NULL;

        SymbolTable[hFunc] = temp;
        return SymbolTable[hFunc];

    }

    // IF TRUE TRUE TRUE
    else if( SymbolTable[hFunc] != NULL && SymbolTable[hFunc]->Name.compare(Symbol) && SymbolTable[hFunc]->next == NULL ){

        bucket *temp = new bucket;
        temp->Name.assign(Symbol);
        temp->Attr.typeSymb = Attr.typeSymb;
        temp->next = new bucket;
        delete temp->next;
        temp->next = NULL;

        SymbolTable[hFunc]->next = temp;
        return SymbolTable[hFunc]->next;

    }
    else{
        bucket* eqSymb = searchEqual( SymbolTable[hFunc], Symbol );
        if( eqSymb != NULL ){
            return eqSymb;

        }
        else{

            bucket* temp = findLast( SymbolTable[hFunc]->next )->next;
            temp = new bucket;
            temp->Name.assign(Symbol);
            temp->Attr.typeSymb = Attr.typeSymb;
            temp->next = new bucket;
            delete temp->next;
            temp->next = NULL;

            bucket* temp2 = findLast( SymbolTable[hFunc]->next )->next;
            temp2 = temp;

            return temp2;
        }

    }
}

void INPUT( string Symbol, data Attr ){

    bucket *k = findTab( Symbol, Attr );

    cout << "Symbol: " << k->Name << endl;
    cout << "Code: " << k->Attr.typeSymb << endl;
}

int main()
{
    data b;

    while(1){
        string a;
        int c;
        cin >> a;

        cin >> c;
        b.typeSymb = c;

        INPUT( a, b );
    }


}

BMutev

The purpose is to make a compiler in the end, but for now I only did the symbol table. Now I am working my way around the lexical analyzer. But while I am doing it I though about asking if I could have done the symbol table in a bit better way.

This article has been dead for over six months. Start a new discussion instead.