context free grammar - Java Implementation of an algorithm for CFG in Chomsky form -
the goal convert pseudo code actual working code. thought had figured out there's not quite right.
the rule i'm using is
s -> rt|empty t -> tr|a r => tr|b
here's pseudo code:
d == "on input w == wi . ..wn : 1. if w == e , s -> e rule, accept. [handle w == empty case] 2. == 1 n: [examine each substring of length 1] 3. each variable a: 4. test whether -> b rule, b == wi. 5. if so,place in table(i,i). 6. l == 2 n: 7. == 1 n -l + 1: 8. let j == + l - 1, 9. k == j -1: 10. each rule \037 bc: 11. if table(i,k)contains b , table(k + 1,j)contains c,put in table(i,j). 12.if s in table(l,n), accept.otherwise, reject.
here's actual code:
public class algorithm { public static void main(string[] args){ string w = " abab"; string table[][] = new string[w.length()][w.length()]; if (w.isempty()){ system.out.println("accept\n"); } for(int i=1; i<w.length();i++){ if(w.charat(i)=='a') table[i][i]="r"; else if(w.charat(i)=='b') table[i][i]="t"; } for(int l=2; l<=w.length();l++){ for(int i=1; i<w.length()-l+1;i++){ int j = i+l-1; for(int k=i; k<=j-1;k++){ if(table[i][k].contains("t") && table[k+1][j].contains("r")) table[i][j]="t,r"; //not sure how part? else if(table[i][k].contains("r") && table[k+1][j].contains("t")) table[i][j]="s"; } } } system.out.println("finished"); system.out.println(w); for(int i=1;i<w.length();i++){ for(int j=1;j<w.length();j++){ system.out.print(table[i][j]+"\t");} system.out.println(""); } //system.out.println((table[1][w.length()-1])); //if (table[1][w.length()-1].equals("s")) // system.out.println("accept"); } }
i get:
abab r s s null null t t,r s null null r s null null null t
however can tell, baba can derived grammar.
figured out did wrong. first off had <=
rather <
secondly didn't initialize array entirely null references @ points.
Comments
Post a Comment