|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.BufferedWriter; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.FileWriter; |
| 6 | + |
| 7 | +public class slr { |
| 8 | + static String[][] ir=new String[15][10]; |
| 9 | + static String[][] il=new String[15][10]; |
| 10 | + static String gl[]=new String[10],tempr[]=new String[15]; |
| 11 | + static int in[]=new int[15]; |
| 12 | + static String dfa[][]=new String[15][15]; |
| 13 | + static String gr[]=new String[10],read[]=new String[15]; |
| 14 | + static int n=0,a,l=0,i=0,rr=0,j=0,ns=0,p1,k,p,tn=0; |
| 15 | + static String temp="\0",t1=null,temp2,templ,ptr; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + public static void main(String[] args) throws Exception { |
| 20 | + File inputFile = new File("C:\\Users\\Harshad\\Desktop\\t1.txt"); |
| 21 | + File tempFile = new File("C:\\Users\\Harshad\\Desktop\\t2.txt"); |
| 22 | + if (!tempFile.exists()) { |
| 23 | + tempFile.createNewFile(); |
| 24 | + } |
| 25 | + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); |
| 26 | + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); |
| 27 | + |
| 28 | + String currentLine; |
| 29 | + |
| 30 | + while((currentLine = reader.readLine()) != null) |
| 31 | + { |
| 32 | + String trimmedLine = currentLine.replace("->"," "); |
| 33 | + writer.write(trimmedLine); |
| 34 | + writer.newLine(); |
| 35 | + } |
| 36 | + reader.close(); |
| 37 | + writer.close(); |
| 38 | + inputFile.delete(); |
| 39 | + tempFile.renameTo(inputFile); |
| 40 | + |
| 41 | + |
| 42 | + BufferedReader bufferedReader = new BufferedReader(new FileReader(inputFile)); |
| 43 | + String line; |
| 44 | + while((line = bufferedReader.readLine()) != null) { |
| 45 | + //System.out.println(line); |
| 46 | + try{ |
| 47 | + gl[n]=line.substring(0,1); |
| 48 | + a=line.length(); |
| 49 | + gr[n]=line.substring(2,a); |
| 50 | + n++;} |
| 51 | + catch(Exception e){ |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + } |
| 55 | + // Always close files. |
| 56 | + bufferedReader.close(); |
| 57 | + |
| 58 | + System.out.println("THE GRAMMER IS AS FOLLOWS"); |
| 59 | + for(int i=0;i<n;i++){ |
| 60 | + System.out.print(gl[i]+"->"); |
| 61 | + System.out.println(gr[i]); |
| 62 | + } |
| 63 | + il[0][0]="Z"; |
| 64 | + ir[0][0]=".S"; |
| 65 | + in[0]++; |
| 66 | + l = 0; |
| 67 | + for(i=0;i<n;i++){ |
| 68 | + //System.out.println("in[0]:"+in[0]+" "+"ir[]:"+ir[0][l].substring(1,2)+" i:"+i); |
| 69 | + temp=ir[0][l].substring(1,2); |
| 70 | + l++; |
| 71 | + for(j=0;j<rr;j++) |
| 72 | + {if(temp.equals(read[j].substring(0,1))) |
| 73 | + { //System.out.println("temp:"+temp+"read[j]:"+read[j]+"l:"+l); |
| 74 | + break;}} |
| 75 | + if(j==rr){//System.out.println("read[rr]:"+read[rr]+"j:"+j); |
| 76 | + read[rr]=temp; |
| 77 | + //System.out.println("inside j=rr:rr:"+rr+"temp:"+temp+"read[rr]:"+read[rr]); |
| 78 | + rr++;} |
| 79 | + else |
| 80 | + {//System.out.println("inside else"); |
| 81 | + continue;} |
| 82 | + |
| 83 | + for(j=0;j<n;j++){ |
| 84 | + //System.out.println("inside 2 for:gl:"+gl[j].trim()); |
| 85 | + if(temp.equals(gl[j].trim())){ |
| 86 | + //System.out.println("inside gl[j]:"+gl[l]); |
| 87 | + ir[0][in[0]]="."; |
| 88 | + ir[0][in[0]]=ir[0][in[0]].concat(gr[j]); |
| 89 | + il[0][in[0]]=gl[j]; |
| 90 | + in[0]++; |
| 91 | + //System.out.println("inside ir[j]:"+ir[0][in[0]]+"il[j]"+il[0][in[0]]); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +ns++; |
| 96 | +System.out.println("\nI"+(ns-1)+":"); |
| 97 | +for(int i=0;i<in[0];i++) |
| 98 | + System.out.println("\t"+(il[0][i])+"->"+(ir[0][i])); |
| 99 | + |
| 100 | +for(l=0;l<ns;l++){ |
| 101 | + |
| 102 | + //System.out.println("Going to canonical"); |
| 103 | + canonical(l);} |
| 104 | + |
| 105 | +System.out.println("\n\t\tDFA TABLE IS AS FOLLOWS\n\n\n"); |
| 106 | +for(i=0;i<ns;i++){ |
| 107 | + |
| 108 | + System.out.print("I"+i+": "); |
| 109 | + for(j=0;j<ns;j++){ |
| 110 | + if(dfa[i][j]!=(null)){ |
| 111 | + System.out.println("'"+dfa[i][j]+"'->I"+j); |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + System.out.println("\n"); |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + @SuppressWarnings("null") |
| 126 | + private static void canonical(int l) { |
| 127 | + int t1=0,rr1=0,b; |
| 128 | + String read1[] = new String[15],ptr1; |
| 129 | + |
| 130 | + for(int i=0;i<in[l];i++){//System.out.println("l:"+l); |
| 131 | + temp2="."; |
| 132 | + ir[l][i].trim(); |
| 133 | + b=ir[l][i].indexOf('.'); |
| 134 | + //System.out.println("b:"+b); |
| 135 | + ptr1=ir[l][i].substring(b,ir[l][i].length()); |
| 136 | + // System.out.println("ptr1:"+ptr1); |
| 137 | + t1=ir[l][i].length()-ptr1.length(); |
| 138 | + //System.out.println("ir:"+ir[l][i]); |
| 139 | + //System.out.println("t1:"+t1+"prt1:"+ptr1.length()+"ir.lenght"+ir[l][i].length()); |
| 140 | + |
| 141 | + if(t1+1==ir[l][i].length()) |
| 142 | + { //System.out.println("inside for t1+1"); |
| 143 | + continue;} |
| 144 | + |
| 145 | + temp2=temp2+ir[l][i].charAt(t1+1); |
| 146 | + //System.out.println("temp2:"+temp2); |
| 147 | + for(j=0;j<rr1;j=j+1){ |
| 148 | + if(temp2.equals(read1[j])){ |
| 149 | + // System.out.println("inside for temp2 equals:"+temp2+"read[j]"+read1[j]+"j:"+j); |
| 150 | + break;}} |
| 151 | + |
| 152 | + //System.out.println("inside j:"+read1[rr1]+temp2+"rr1:"+rr1+"and j:"+j); |
| 153 | + if(j==rr1){ |
| 154 | + //System.out.println("inside j:"+read1[rr1]+temp2+j); |
| 155 | + read1[rr1]=temp2; |
| 156 | + //System.out.println("inside j: after read1:"+read1[rr1]+temp2); |
| 157 | + rr1++; |
| 158 | + } |
| 159 | + else |
| 160 | + {//System.out.println("inside else"); |
| 161 | + continue;} |
| 162 | + for(j=0;j<in[0];j++){ |
| 163 | + //System.out.println("inside for temp2;"+temp2); |
| 164 | + if(ir[l][j]==null) |
| 165 | + { p1=-1; |
| 166 | + ptr=null;} |
| 167 | + else |
| 168 | + p1=ir[l][j].indexOf(temp2); |
| 169 | + //System.out.println("inside for p1;"+p1); |
| 170 | + if(p1==-1) |
| 171 | + ptr=null; |
| 172 | + else |
| 173 | + ptr=ir[l][j].substring(p1, temp2.length()); |
| 174 | + if(ptr != null){ |
| 175 | + //System.out.println("inside ptr!=null tn"+tn); |
| 176 | + if(templ==null) |
| 177 | + templ=il[l][j].substring(0,1); |
| 178 | + else |
| 179 | + templ=templ+il[l][j].charAt(0); |
| 180 | + //System.out.println("inside ptr!=null templ:"+templ); |
| 181 | + tempr[tn]=ir[l][j]; |
| 182 | + //System.out.println("inside ptr!=null tempr:"+tempr[tn]+" l:"+l+" j:"+j); |
| 183 | + tn++; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + for(j=0;j<tn;j++){ |
| 188 | + //System.out.println("inside for of tn:"+tn); |
| 189 | + b=tempr[j].indexOf('.'); |
| 190 | + //System.out.println("inside for b;"+b); |
| 191 | + ptr=tempr[j].substring(b); |
| 192 | + //System.out.println("inside for ptr:"+ptr); |
| 193 | + p=tempr[j].length()-ptr.length(); |
| 194 | + //System.out.println("p:"+p); |
| 195 | + //System.out.println("tempr[j]:"+tempr[j]); |
| 196 | + StringBuffer str1=new StringBuffer(tempr[j]); |
| 197 | + str1.setCharAt(p,tempr[j].charAt(p+1)); |
| 198 | + tempr[j]=str1.toString(); |
| 199 | + //System.out.println("tempr[j]:"+tempr[j]); |
| 200 | + str1.setCharAt(p+1,'.'); |
| 201 | + tempr[j]=str1.toString(); |
| 202 | + //System.out.println("tempr[j]:"+tempr[j]+" "+tempr[j].indexOf('.')); |
| 203 | + |
| 204 | + if(il[ns][in[ns]]==null) |
| 205 | + il[ns][in[ns]]=templ.substring(j,j+1); |
| 206 | + else |
| 207 | + il[ns][in[ns]]=templ.substring(j,j+1); |
| 208 | + //System.out.println("tempr[j]:"+tempr[j]+" "+tempr[j].indexOf('.')); |
| 209 | + ir[ns][in[ns]]=tempr[j]; |
| 210 | + //System.out.println("ir[ns][in[ns]]:"+ir[ns][in[ns]]); |
| 211 | + |
| 212 | + in[ns]++; |
| 213 | + } |
| 214 | + |
| 215 | + moreprod(); |
| 216 | + |
| 217 | + for(j=0;j<ns;j++) |
| 218 | + { int g=compstruct(ns,j); |
| 219 | + if(g==1){ |
| 220 | + //System.out.println("i m inside"); |
| 221 | + il[ns][0]= null; |
| 222 | + StringBuffer str3,str4; |
| 223 | + for(k=0;k<in[ns];k++){ |
| 224 | + str3=new StringBuffer(ir[ns][k]); |
| 225 | + str3.setCharAt(0, '\0'); |
| 226 | + ir[ns][k]=str3.toString();} |
| 227 | + in[ns]=0; |
| 228 | + |
| 229 | + dfa[l][j]=temp2.substring(1,2); |
| 230 | + break; |
| 231 | + } |
| 232 | + } |
| 233 | + StringBuffer str5,str6; |
| 234 | + if(j<ns) |
| 235 | + { |
| 236 | + tn=0; |
| 237 | + for(j=0;j<15;j++) |
| 238 | + { templ=null; |
| 239 | + tempr[j]=null; |
| 240 | + } |
| 241 | + continue; |
| 242 | + } |
| 243 | + dfa[l][j]=temp2.substring(1,2); |
| 244 | + System.out.println("\n\nI"+ns+":"); |
| 245 | + for(j=0;j<in[ns];j++) |
| 246 | + System.out.println("\n\t"+il[ns][j]+"->"+ir[ns][j]); |
| 247 | + |
| 248 | + ns++; |
| 249 | + tn=0; |
| 250 | + |
| 251 | + for(j=0;j<15;j++){ |
| 252 | + templ=null; |
| 253 | + tempr[j]=null; |
| 254 | + } |
| 255 | + |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + private static int compstruct(int y,int u) { |
| 262 | + |
| 263 | + int t; |
| 264 | + if(in[y]!=in[u]) |
| 265 | + {return 0;} |
| 266 | + |
| 267 | + if(!il[y][0].equals(il[u][0])) |
| 268 | + return 0; |
| 269 | + for(t=0;t<in[y];t++) |
| 270 | + { |
| 271 | + if(!ir[y][t].equals(ir[u][t])) |
| 272 | + return 0; |
| 273 | + } |
| 274 | + // TODO Auto-generated method stub |
| 275 | + return 1; |
| 276 | + } |
| 277 | + |
| 278 | + |
| 279 | + |
| 280 | + @SuppressWarnings("unused") |
| 281 | + private static void moreprod() { |
| 282 | + //System.out.println("inside moreprod()"); |
| 283 | + int t=0,r,s,l1=0,rr1=0,b; |
| 284 | + String read1[] = new String[15],ptr1; |
| 285 | + |
| 286 | + for(r=0;r<in[ns];r++) |
| 287 | + { |
| 288 | + b=ir[ns][l1].indexOf('.'); |
| 289 | + //System.out.println("b:"+b); |
| 290 | + |
| 291 | + ptr1=ir[ns][l1].substring(b,ir[ns][l1].length()); |
| 292 | + //System.out.println("ptr1:"+ptr1); |
| 293 | + //System.out.println("b:"+b+"ptr1:"+ptr1+"ir[ns][l1]:"+ir[ns][l1]); |
| 294 | + t=ir[ns][l1].length()-ptr1.length(); |
| 295 | + //System.out.println("ns:"+ns+"l1:"+l1+"t:"+t); |
| 296 | + if(t+1==ir[ns][l1].length()) |
| 297 | + {//System.out.println("inside for t1+1"); |
| 298 | + l1++; |
| 299 | + continue; |
| 300 | + } |
| 301 | + //System.out.println("ns:"+ns+"l1:"+l1); |
| 302 | + //System.out.println("temp"+temp); |
| 303 | + temp=ir[ns][l1].substring(t+1,t+2); |
| 304 | + //System.out.println("temp"+temp+" "+ir[ns][l1].charAt(t+1)); |
| 305 | + l1++; |
| 306 | + //System.out.println("temp"+temp+"l1:"+l1); |
| 307 | + for(s=0;s<rr1;s++){ |
| 308 | + if(temp.equals(read1[s])){ |
| 309 | + //System.out.println("inside for tempequals:"+temp+"re:"+read1[s]); |
| 310 | + break;} |
| 311 | + } |
| 312 | + if(s==rr1){ |
| 313 | + //System.out.println("inside s:"+read1[rr1]+temp+s); |
| 314 | + read1[rr1]=temp; |
| 315 | + rr1++; |
| 316 | + //System.out.println("inside j:"+read1[rr1-1]+temp); |
| 317 | + } |
| 318 | + else |
| 319 | + {//System.out.println("inside else"); |
| 320 | + continue;} |
| 321 | + for(s=0;s<n;s++){ |
| 322 | + //System.out.println("inside for "); |
| 323 | + //System.out.println("inside for gl[s]:"+gl[s]+" and il[ns][in[ns]:"+il[ns][in[ns]-1] +"and ns:"+ns); |
| 324 | + //System.out.println("inside for "+temp); |
| 325 | + if(gl[s].equals(temp)){ |
| 326 | + //System.out.println("inside gl[s]=temp ns"+ns); |
| 327 | + |
| 328 | + ir[ns][in[ns]]="."; |
| 329 | + ir[ns][in[ns]]=ir[ns][in[ns]].concat(gr[s]); |
| 330 | + |
| 331 | + //System.out.println("inside ir[ns][in[ns]]"+ir[ns][in[ns]]); |
| 332 | + il[ns][in[ns]]=gl[s]; |
| 333 | + //System.out.println("inside il[ns][in[ns]]"+il[ns][in[ns]] ); |
| 334 | + in[ns]++; |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + // TODO Auto-generated method stub |
| 339 | + } |
| 340 | +} |
0 commit comments