CYK algorithm implementation C++

1.9k views Asked by At

I've been trying to implement the CYK algorithm based on the pseudocode given by wikipedia, however I can't seem to get it right and I don't know what is wrong with the code. My best guess is that my indexes are wrong, since the pseudocode does not use array indexing.

This is the grammar I'm testing:
/ --> U1V1 | U2V2 | V1V1 | V2V2 | a | b
S --> U1V1 | U2V2 | V1V1 | V2V2 | a | b
V1 --> a
V2 --> b
U1 --> V1S
U2 --> V2S
This grammar accepts palindromes, so "baab" shoulde be accepted, however it is not being accepted as part of the grammar

Here is the code:

int n = (int)S.size();
        std::vector<std::vector<std::vector<bool>>> P(n, std::vector<std::vector<bool>>(n, std::vector<bool>(startVariables.size(), false)));

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < productions.size(); j++)
            {
                    std::vector<std::string> prods = getProductionsFromVariable(productions[j].variable);
                for(int k = 0; k < prods.size(); k++)
                {
                    if(prods[k][0] == S[i] && !isMayus(prods[k][0]) && !isnumber(prods[k][0]))
                    {
                        P[0][i][j] = true;
                    }
                }
            }
        }

        for(int i = 1; i < n; i++)
        {
            for(int j = 0; j < n-i; j++)
            {
                for(int k = 0; k < i; k++)
                {
                    for(int l = 0; l < startVariables.size(); l++)
                    {
                        std::vector<std::string> prods = getProductionsFromVariable(startVariables[l]);
                        for(int m = 0; m < prods.size(); m++)
                        {
                            if(getNumberOfVars(prods[m]) >= 2)
                            {
                                std::vector<std::string> subProds = getNumberedVariables(prods[m]);
                                int A = getStartVariablePos(startVariables[l]);
                                int B = getStartVariablePos(subProds[0]);
                                int C = getStartVariablePos(subProds[1]);
                                if(P[k][j][B] && P[i-k][j+k][C])
                                    P[i][j][A] = true;
                            }
                        }
                    }
                }
            }
        }

        bool cyk = false;
        for(int i = 0; i < startVariables.size(); i++)
        {
            if(P[n-1][0][i])
                cyk = true;
        }
        return cyk;

The auxiliary functions just return all the given productions of a variable and their index in the vector of variables {/,S,V1,V2,U1,U2}

This grammar (in CNF) comes from the converted grammar S>aSa|bSb|aa|bb|a|b (not in CNF)

Any help would be greatly appreciated

0

There are 0 answers