1 package ro.infoiasi.donald.compiler.cfg; 2 3 public class Production { 4 public static final int LAST_TERMINAL_PRECEDENCE = Terminal.NO_PRECEDENCE-1; 5 6 private NonTerminal lhs; 7 private Word rhs; 8 private int index; 9 private int precedence; 10 private SemanticAction action; 11 12 Production(NonTerminal lhs, Word rhs, int index) { 13 this(lhs, rhs, LAST_TERMINAL_PRECEDENCE, null, index); 14 } 15 16 Production(NonTerminal lhs, Word rhs, int precedence, 17 SemanticAction action, int index) { 18 this.lhs = lhs; 19 this.rhs = rhs; 20 if (precedence == LAST_TERMINAL_PRECEDENCE) { 21 WordIterator it = rhs.iterator(true); 22 if (it.hasPrevTerminal()) { 23 this.precedence = it.prevTerminal().getPrecedence(); 24 } else { 25 this.precedence = Terminal.NO_PRECEDENCE; 26 } 27 } else { 28 this.precedence = precedence; 29 } 30 this.action = action; 31 this.index = index; 32 } 33 34 public NonTerminal getLHS() { 35 return lhs; 36 } 37 38 public Word getRHS() { 39 return rhs; 40 } 41 42 public int getPrecedence() { 43 return precedence; 44 } 45 46 public SemanticAction getSemanticAction() { 47 return action; 48 } 49 50 void setSemanticAction(SemanticAction action) { 51 this.action = action; 52 } 53 54 public int getIndex() { 55 return index; 56 } 57 58 59 void setIndex(int index) { 60 this.index = index; 61 } 62 63 public boolean equals(Object o) { 64 return (o instanceof Production && 65 lhs.equals(((Production)o).lhs) && 66 rhs.equals(((Production)o).rhs) && 67 index == ((Production)o).index); 68 } 69 70 public int hashCode() { 71 return index; 72 } 73 74 public String toString() { 75 return lhs+" ::= "+rhs; 76 } 77 78 } 79 | Popular Tags |