1 8 9 package net.sourceforge.chaperon.process; 10 11 import java.io.Serializable ; 12 13 import java.util.Arrays ; 14 15 22 public class ParserAutomaton implements Serializable 23 { 24 private String [] tsymbols; 26 27 private String [] ntsymbols; 29 30 private int[] productionsymbols; 32 33 private int[] productionlengths; 35 36 private String [] errors; 38 39 private int[][] actions; 42 private int[] eofactions; 43 44 45 private static final int ERROR = 0; 46 47 48 private static final int SHIFT = 1; 49 50 51 private static final int REDUCE = 2; 52 53 54 private static final int ACCEPT = 3; 55 56 private int[][] transitions; 59 private static final long serialVersionUID = 8045275977315821215L; 60 61 70 public ParserAutomaton(int tsymbolcount, int ntsymbolcount, int productioncount, int errorcount, 71 int statecount) 72 { 73 tsymbols = new String [tsymbolcount]; 74 Arrays.fill(tsymbols, "noname"); 75 76 ntsymbols = new String [ntsymbolcount]; 77 Arrays.fill(ntsymbols, "noname"); 78 79 productionsymbols = new int[productioncount]; 80 Arrays.fill(productionsymbols, 0); 81 productionlengths = new int[productioncount]; 82 Arrays.fill(productionlengths, 0); 83 84 errors = new String [errorcount]; 85 Arrays.fill(errors, "Unexpected token."); 86 87 actions = new int[statecount][tsymbolcount]; 88 for (int i = 0; i<statecount; i++) 89 Arrays.fill(actions[i], ERROR); 90 91 eofactions = new int[statecount]; 92 Arrays.fill(eofactions, ERROR); 93 94 transitions = new int[statecount][ntsymbolcount]; 95 for (int i = 0; i<statecount; i++) 96 Arrays.fill(transitions[i], 0); 97 } 98 99 105 public void setTerminal(int index, String symbol) 106 { 107 if ((index<0) || (index>=tsymbols.length)) 108 throw new IndexOutOfBoundsException (); 109 110 tsymbols[index] = symbol; 111 } 112 113 120 public String getTerminal(int index) 121 { 122 if ((index<0) || (index>=tsymbols.length)) 123 throw new IndexOutOfBoundsException (); 124 125 return tsymbols[index]; 126 } 127 128 133 public int getTerminalCount() 134 { 135 return tsymbols.length; 136 } 137 138 144 public void setNonterminal(int index, String symbol) 145 { 146 if ((index<0) || (index>=ntsymbols.length)) 147 throw new IndexOutOfBoundsException (); 148 149 ntsymbols[index] = symbol; 150 } 151 152 159 public String getNonterminal(int index) 160 { 161 if ((index<0) || (index>=ntsymbols.length)) 162 throw new IndexOutOfBoundsException (); 163 164 return ntsymbols[index]; 165 } 166 167 172 public int getNonterminalCount() 173 { 174 return ntsymbols.length; 175 } 176 177 183 public void setProductionSymbol(int index, int symbol) 184 { 185 if ((symbol<0) || (symbol>=ntsymbols.length)) 186 throw new IndexOutOfBoundsException (); 187 188 productionsymbols[index] = symbol; 189 } 190 191 198 public int getProductionSymbol(int index) 199 { 200 return productionsymbols[index]; 201 } 202 203 209 public void setProductionLength(int index, int length) 210 { 211 if (length<0) 212 throw new IllegalArgumentException (); 213 214 productionlengths[index] = length; 215 } 216 217 224 public int getProductionLength(int index) 225 { 226 return productionlengths[index]; 227 } 228 229 234 public int getProductionCount() 235 { 236 return productionsymbols.length; 237 } 238 239 245 public void setErrorMessage(int index, String message) 246 { 247 if (index>0) 248 errors[index-1] = message; 249 } 250 251 258 public String getErrorMessage(int index) 259 { 260 if (index==0) 261 return "Unexpected token."; 262 263 return errors[index-1]; 264 } 265 266 271 public int getErrorCount() 272 { 273 return errors.length; 274 } 275 276 283 public void setErrorAction(int state, int tsymbol, int nextstate) 284 { 285 if ((nextstate<0) && (nextstate>=actions.length)) 286 throw new IndexOutOfBoundsException (); 287 288 actions[state][tsymbol] = (ERROR|(nextstate<<2)); 289 } 290 291 297 public void setErrorAction(int state, int nextstate) 298 { 299 if ((nextstate<0) && (nextstate>=actions.length)) 300 throw new IndexOutOfBoundsException (); 301 302 eofactions[state] = (ERROR|(nextstate<<2)); 303 } 304 305 312 public void setShiftAction(int state, int tsymbol, int nextstate) 313 { 314 if ((nextstate<0) && (nextstate>=actions.length)) 315 throw new IndexOutOfBoundsException (); 316 317 actions[state][tsymbol] = (SHIFT|(nextstate<<2)); 318 } 319 320 327 public void setReduceAction(int state, int tsymbol, int production) 328 { 329 if ((production<0) && (production>=productionsymbols.length)) 330 throw new IndexOutOfBoundsException (); 331 332 actions[state][tsymbol] = (REDUCE|(production<<2)); 333 } 334 335 341 public void setReduceAction(int state, int production) 342 { 343 if ((production<0) && (production>=productionsymbols.length)) 344 throw new IndexOutOfBoundsException (); 345 346 eofactions[state] = (REDUCE|(production<<2)); 347 } 348 349 355 public void setAcceptAction(int state, int production) 356 { 357 if ((production<0) && (production>=productionsymbols.length)) 358 throw new IndexOutOfBoundsException (); 359 360 eofactions[state] = (ACCEPT|(production<<2)); 361 } 362 363 371 public boolean isErrorAction(int state, int tsymbol) 372 { 373 return (actions[state][tsymbol]&3)==ERROR; 374 } 375 376 383 public boolean isErrorAction(int state) 384 { 385 return (eofactions[state]&3)==ERROR; 386 } 387 388 396 public boolean isShiftAction(int state, int tsymbol) 397 { 398 return (actions[state][tsymbol]&3)==SHIFT; 399 } 400 401 409 public boolean isReduceAction(int state, int tsymbol) 410 { 411 return (actions[state][tsymbol]&3)==REDUCE; 412 } 413 414 421 public boolean isReduceAction(int state) 422 { 423 return (eofactions[state]&3)==REDUCE; 424 } 425 426 433 public boolean isAcceptAction(int state) 434 { 435 return (eofactions[state]&3)==ACCEPT; 436 } 437 438 446 public int getShiftTransition(int state, int tsymbol) 447 { 448 if ((actions[state][tsymbol]&3)!=SHIFT) 449 throw new IllegalArgumentException ("Action is not a 'shift' action"); 450 451 return actions[state][tsymbol]>>2; 452 } 453 454 462 public int getReduceProduction(int state, int tsymbol) 463 { 464 if ((actions[state][tsymbol]&3)!=REDUCE) 465 throw new IllegalArgumentException ("Action is not a 'reduce' action"); 466 467 return actions[state][tsymbol]>>2; 468 } 469 470 477 public int getReduceProduction(int state) 478 { 479 if (((eofactions[state]&3)!=REDUCE) && ((eofactions[state]&3)!=ACCEPT)) 480 throw new IllegalArgumentException ("Action is not a 'reduce' action"); 481 482 return eofactions[state]>>2; 483 } 484 485 493 public int getErrorTransition(int state, int tsymbol) 494 { 495 if ((actions[state][tsymbol]&3)!=ERROR) 496 throw new IllegalArgumentException ("Action is not a 'error' action"); 497 498 return actions[state][tsymbol]>>2; 499 } 500 501 508 public int getErrorTransition(int state) 509 { 510 if ((eofactions[state]&3)!=ERROR) 511 throw new IllegalArgumentException ("Action is not a 'error' action"); 512 513 return eofactions[state]>>2; 514 } 515 516 523 public void setTransition(int state, int ntsymbol, int nextstate) 524 { 525 transitions[state][ntsymbol] = (nextstate); 526 } 527 528 536 public int getTransition(int state, int ntsymbol) 537 { 538 return transitions[state][ntsymbol]; 539 } 540 541 546 public int getStateCount() 547 { 548 return actions.length; 549 } 550 551 556 public String toString() 557 { 558 int i; 559 int j; 560 java.text.DecimalFormat format = new java.text.DecimalFormat ("00"); 561 StringBuffer buffer = new StringBuffer (); 562 563 buffer.append("Terminal symbols:"); 564 for (i = 0; i<tsymbols.length; i++) 565 { 566 buffer.append(tsymbols[i]); 567 buffer.append("("); 568 buffer.append(String.valueOf(i)); 569 buffer.append(") "); 570 } 571 572 buffer.append("\n"); 573 574 buffer.append("Non terminal symbols:"); 575 for (i = 0; i<ntsymbols.length; i++) 576 { 577 buffer.append(ntsymbols[i]); 578 buffer.append("("); 579 buffer.append(String.valueOf(i)); 580 buffer.append(") "); 581 } 582 583 buffer.append("\n"); 584 585 int max = 0; 586 587 for (i = 0; i<tsymbols.length; i++) 588 max = Math.max(max, tsymbols[i].length()); 589 590 for (i = 0; i<ntsymbols.length; i++) 591 max = Math.max(max, ntsymbols[i].length()); 592 593 max = Math.max(max, 3); 595 for (i = 0; i<max; i++) 596 { 597 buffer.append(" "); 598 for (j = 0; j<tsymbols.length; j++) 599 if ((max-i-1)<tsymbols[j].length()) 600 { 601 buffer.append(tsymbols[j].charAt(max-i-1)); 602 buffer.append(" "); 603 } 604 else 605 buffer.append(" "); 606 607 if ((max-i-1)<"EOF".length()) 608 { 609 buffer.append("EOF".charAt(max-i-1)); 610 buffer.append(" "); 611 } 612 else 613 buffer.append(" "); 614 615 buffer.append(" "); 616 617 for (j = 0; j<ntsymbols.length; j++) 618 if ((max-i-1)<ntsymbols[j].length()) 619 { 620 buffer.append(ntsymbols[j].charAt(max-i-1)); 621 buffer.append(" "); 622 } 623 else 624 buffer.append(" "); 625 626 buffer.append("\n"); 627 } 628 629 for (i = 0; i<actions.length; i++) 630 { 631 buffer.append(format.format(i)+" "); 632 for (j = 0; j<actions[i].length; j++) 633 { 634 if ((actions[i][j]&3)==SHIFT) 635 { 636 buffer.append("s"); 637 buffer.append(format.format(actions[i][j]>>2)); 638 buffer.append(" "); 639 } 640 else if ((actions[i][j]&3)==REDUCE) 641 { 642 buffer.append("r"); 643 buffer.append(format.format(actions[i][j]>>2)); 644 buffer.append(" "); 645 } 646 else if ((actions[i][j]&3)==ERROR) 647 { 648 if ((actions[i][j]>>2)!=0) 649 { 650 buffer.append("e"); 651 buffer.append(format.format(actions[i][j]>>2)); 652 buffer.append(" "); 653 } 654 else 655 buffer.append("--- "); 656 } 657 else if ((actions[i][j]&3)==ACCEPT) 658 { 659 buffer.append("a"); 660 buffer.append(format.format(actions[i][j]>>2)); 661 buffer.append(" "); 662 } 663 } 664 665 if ((eofactions[i]&3)==SHIFT) 666 { 667 buffer.append("s"); 668 buffer.append(format.format(eofactions[i]>>2)); 669 buffer.append(" "); 670 } 671 else if ((eofactions[i]&3)==REDUCE) 672 { 673 buffer.append("r"); 674 buffer.append(format.format(eofactions[i]>>2)); 675 buffer.append(" "); 676 } 677 else if ((eofactions[i]&3)==ERROR) 678 { 679 if ((eofactions[i]>>2)!=0) 680 { 681 buffer.append("e"); 682 buffer.append(format.format(eofactions[i]>>2)); 683 buffer.append(" "); 684 } 685 else 686 buffer.append("--- "); 687 } 688 else if ((eofactions[i]&3)==ACCEPT) 689 { 690 buffer.append("a"); 691 buffer.append(format.format(eofactions[i]>>2)); 692 buffer.append(" "); 693 } 694 695 buffer.append("| "); 696 697 for (j = 0; j<transitions[i].length; j++) 698 if (transitions[i][j]>0) 699 { 700 buffer.append(format.format(transitions[i][j])); 701 buffer.append(" "); 702 } 703 else 704 buffer.append("-- "); 705 706 buffer.append("\n"); 707 } 708 709 return buffer.toString(); 710 } 711 } 712 | Popular Tags |