mercredi 23 mars 2022

In symbol table how to mark variable out of scope?

I am writing a toy compiler, which compile a c/c++ like language to c++. I am using bison, but in this structure is hard to handle when variable became out of scope.

In the source lanugage in the whole main function there can be only one variable with the same name, it is good, but there is a problem what I cannot solve.

I cannot make c++ code like this, because c++ compiler throw semantical error: 'var' was not declared in this scope.

    int main()
    {
        if (true)
        {
            int var = 4;
        }
        if (true)
        {
            var = 5;
        }
    }

The source language has while, if and if/else statements. I have to throw semantical error if a declared variable is assinged in out of scope. For example:

This should be semantical error, so I cannot generetad this code:

int main()
{
    while(0)
    {
        int var = 1;
    }

    if (1)
    {
        var = 2;
    }
}

This also have to be semantical error:

int main()
{
    if (0)
    {
        int var = 1;
    }
    else
    {
        if (1)
        {
            var = 5;
        }
    }
}

And this is allowed, I can generate this code:

int main()
{
    if (0)
    {

    }
    else
    {
        int var = 1;
        if (1)
        {
            while (0)
            {
                while (0)
                {
                    var = 2;
                }
            }
        }
    }
}

I tried lot of things, but I cannot solve when there is nested if, if/else or while.

I read tons of tutorials about symbol table, but none of them can explain properly how to manage a variable if it is become out of scope.

If you familiar with this topic and with bison, please do not just give me hints, like "use stack, and mark a variable if it become out of scope". I found lot of article about it. Instead of please give me pseudocode or concrate implementation sketch.

I think it cannot be so much difficult, because in the whole main function there can be one variable with the same name as I wrote.

Symbol table:

struct SymbolType
{
    int lineNumber;
    std::string identifier;
    int identifierValue;
    Type type;
    int functionArgumentNumber;
    Type functionReturnType;
    Type markType;
    bool outOfScope;
};

    class Symbol
    {
        public:
            void AddVariable(int _lineNumber, std::string _identifier, int _identifierValue, Type _type, int _functionArgumentNumber, Type _functionReturnType, Type _markType, bool _outOfScope);
            void AddMarker(int _lineNumber, std::string _scopeName, Type _markType);
            bool FindVariable(std::string _identifier);
            int FindVariableValue(std::string _identifier);
            void Remove();
            void Print();
            std::vector<SymbolType> symbolTable;

        private:
            int lineNumber;
            std::string identifier;
            int identifierValue;
            Type type;
            int functionArgumentNumber;
            Type functionReturnType;
            Type markType;
            bool outOfScope;
    };

Aucun commentaire:

Enregistrer un commentaire