Please show me how it works in detail.

Description:   Implement the CKY Algorithm as a recognizer to determine if a string is in a language in the languageplatform of your choice.

Program Requirements:

Input a grammar G in CNF. You can use any method to input the grammar. Your program will be tested with grammars of size: 5 – 10 variables, 10 – 20 productions, 2 – 6 terminals.

Input a string w.

Output : True if w ÎL(G), otherwise false.

* Extend your program to a parser that also constructs a parse tree for w.

Algorithm:

Input: start category S and input string

n := length(string)

for j := 1 to n

chart(j − 1, j) := {X | X → wordj ∈ P}

for i := j − 2 down to 0

chart(i, j) := {}

for k := i + 1 to j − 1

for every A → BC ∈ P

if B ∈ chart(i, k) and C ∈ hart(k, j) then

chart(i, j) := chart(i, j) ∪ {A}

Output: if S ∈ chart(0, n) then accept else reject

Program:

#include<iostream>

#include<cstring>

#include<algorithm>

#include<string>

#include<cassert>

#include<iomanip>

using namespace std;

#define MAX 100

#define for(i,a,b) for(i=a;i<b; i++)

string gram[MAX][MAX]; //to store entered grammar

string dpr[MAX];

int p,np;       //np-> number of productions

inline string concat( string a, string b)   //concatenates unique non-terminals

{

int i;

string r=a;

for(i,0,b.length())

if(r.find(b[i]) > r.length())

r+=b[i];

return (r);

}

inline void break_gram(string a)    //seperates right hand side of entered grammar

{

int i;

p=0;

while(a.length())

{

i=a.find(“|”);

if(i>a.length())

{

dpr[p++] = a;

a=””;

}

else

{

dpr[p++] = a.substr(0,i);

a=a.substr(i+1,a.length());

}

}

}

inline int lchomsky(string a)   //checks if LHS of entered grammar is in CNF

{

if(a.length()==1 && a[0]>=’A’ && a[0]<=’Z’)

return 1;

return 0;

}

inline int rchomsky(string a)   //checks if RHS of grammar is in CNF

{

if (a.length() == 1 && a[0]>=’a’ && a[0] <=’z’)

return 1;

if (a.length()==2 && a[0]>=’A’ && a[0]<=’Z’ && a[1]>=’A’ && a[1]<=’Z’ )

return 1;

return 0;

}

inline string search_prod(string p) //returns a concatenated string of variables which can produce string p

{

int j,k;

string r=””;

for(j,0,np)

{

k=1;

while(gram[j][k] != “”)

{

if(gram[j][k] == p)

{

r=concat(r,gram[j][0]);

}

k++;

}

}

return r;

}

inline string gen_comb(string a, string b) //creates every combination of variables from a and b . For eg: BA * AB = {BA, BB, AA, BB}

{

int i,j;

string pri=a,re=””;

for(i,0,a.length())

for(j,0,b.length())

{

pri=””;

pri=pri+a[i]+b[j];

re=re+search_prod(pri);     //searches if the generated productions can be created or not

}

return re;

}

int main()

{

int i,pt,j,l,k;

string a,str,r,pr,start;

cout<<“nEnter the start Variable “;

cin >> start;

cout<<“nNumber of productions “;

cin >> np;

for(i,0,np)

{

cin >> a;

pt=a.find(“->”);

gram[i][0] = a.substr(0,pt);

if (lchomsky(gram[i][0]) == 0)

{

cout<<“nGrammar not in Chomsky Form”;

abort();

}

a = a.substr(pt+2, a.length());

break_gram(a);

for(j,0,p)

{

gram[i][j+1]=dpr[j];

if (rchomsky(dpr[j]) == 0)

{

cout<<“nGrammar not in Chomsky Form”;

abort();

}

}

}

string matrix[MAX][MAX],st;

cout<<“nEnter string to be checked : “;

cin >> str;

for(i,0,str.length())       //Assigns values to principal diagonal of matrix

{

r=””;

st = “”;

st+=str[i];

for(j,0,np)

{

k=1;

while(gram[j][k] != “”)

{

if(gram[j][k] == st)

{

r=concat(r,gram[j][0]);

}

k++;

}

}

matrix[i][i]=r;

}

int ii,kk;

for(k,1,str.length())       //Assigns values to upper half of the matrix

{

for(j,k,str.length())

{

r=””;

for(l,j-k,j)

{

pr = gen_comb(matrix[j-k][l],matrix[l+1][j]);

r = concat(r,pr);

}

matrix[j-k][j] = r;

}

}

for(i,0,str.length())   //Prints the matrix

{

k=0;

l=str.length()-i-1;

for(j,l,str.length())

{

cout<<setw(5)<<matrix[k++][j]<<” “;

}

cout<<endl;

}

int f=0;

for(i,0,start.length())

if(matrix[0][str.length()-1].find(start[i]) <= matrix[0][str.length()-1].length())   //Checks if last element of first row contains a Start variable

{

cout<<“String can be generatedn”;

return 0;

}

cout<<“String cannot be generatedn”;

return 0;

}

Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.