01: package ro.infoiasi.donald.compiler.cfg;
02:
03: public class Production {
04: public static final int LAST_TERMINAL_PRECEDENCE = Terminal.NO_PRECEDENCE - 1;
05:
06: private NonTerminal lhs;
07: private Word rhs;
08: private int index;
09: 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: /** Used to remove useless nonterminals and productions (package access) */
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) && index == ((Production) o).index);
67: }
68:
69: public int hashCode() {
70: return index;
71: }
72:
73: public String toString() {
74: return lhs + " ::= " + rhs;
75: }
76:
77: }
|