1 package fri.patterns.interpreter.parsergenerator.syntax; 2 3 import java.util.*; 4 import java.io.Serializable ; 5 6 14 15 public class Rule implements Serializable 16 { 17 private List symbols; 18 19 20 public Rule(String nonterminal, int rightSize) { 21 this(new ArrayList(rightSize + 1)); 22 symbols.add(nonterminal); 23 } 24 25 26 public Rule(String [] symbols) { 27 this(SyntaxUtil.ruleToList(symbols)); 28 } 29 30 31 public Rule(List symbols) { 32 if (symbols == null) 33 throw new IllegalArgumentException ("Can not construct rule without nonterminal: "+symbols); 34 this.symbols = symbols; 35 } 36 37 38 protected Rule() { 39 } 40 41 42 public String getNonterminal() { 43 return (String ) symbols.get(0); 44 } 45 46 public int rightSize() { 47 return symbols.size() - 1; 48 } 49 50 public String getRightSymbol(int i) { 51 return (String ) symbols.get(i + 1); 52 } 53 54 public void setRightSymbol(String symbol, int i) { 55 symbols.set(i + 1, symbol); 56 } 57 58 public void addRightSymbol(String symbol) { 59 symbols.add(symbol); 60 } 61 62 public int indexOnRightSide(String symbol) { 63 for (int i = 0; i < rightSize(); i++) 64 if (getRightSymbol(i).equals(symbol)) 65 return i; 66 return -1; 67 } 68 69 70 71 public boolean equals(Object o) { 72 return ((Rule)o).symbols.equals(symbols); 73 } 74 75 76 public int hashCode() { 77 return symbols.hashCode(); 78 } 79 80 81 public String toString() { 82 StringBuffer sb = new StringBuffer (getNonterminal()+" ::= "); 83 for (int i = 0; i < rightSize(); i++) { 84 sb.append(getRightSymbol(i)); 85 sb.append(" "); 86 } 87 sb.append(";"); 88 return sb.toString(); 89 } 90 91 } 92 | Popular Tags |