1 package org.simantics.scl.compiler.parser.generator.table;
3 import org.simantics.scl.compiler.parser.generator.grammar.AnaGrammar;
4 import org.simantics.scl.compiler.parser.generator.grammar.Prod;
7 public class Item implements Comparable<Item> {
8 public final int production;
9 public final int position;
12 public Item(int production, int position, int stackPos) {
13 this.production = production;
14 this.position = position;
15 this.stackPos = stackPos;
18 public int[] nextSymbols(AnaGrammar grammar) {
19 Prod prod = grammar.prods.get(production);
20 return prod.rhs.nextStates(position);
23 public int nextPosition(AnaGrammar grammar, int symbol) {
24 Prod prod = grammar.prods.get(production);
25 return prod.rhs.getTransition(position, symbol);
29 public int hashCode() {
30 return (production * 31 + position) * 31 + stackPos;
34 public boolean equals(Object obj) {
37 if (obj == null || getClass() != obj.getClass())
39 Item other = (Item) obj;
40 return production == other.production
41 && position == other.position
42 && stackPos == other.stackPos;
46 public int compareTo(Item o) {
47 if(production < o.production)
49 if(production > o.production)
51 if(position < o.position)
53 if(position > o.position)
58 public String toString(AnaGrammar grammar) {
59 Prod prod = grammar.prods.get(production);
60 StringBuilder b = new StringBuilder();
61 b.append(grammar.nonterminalNames[prod.lhs]);
63 /*prod.rhs.toRegexpTo(position).toString(b, grammar, 0);
65 prod.rhs.toRegexpFrom(position).toString(b, grammar, 0);*/
66 prod.rhs.toPositionalRegexp(position).toString(b, grammar, 0);
68 b.append(" (stack ").append(stackPos).append(')');
69 /*for(int i=0;i<position;++i)
70 b.append(' ').append(grammar.getName(prod.rhs[i]));
72 for(int i=position;i<prod.rhs.length;++i)
73 b.append(' ').append(grammar.getName(prod.rhs[i]));*/
77 public boolean isReducible(AnaGrammar grammar) {
78 return grammar.prods.get(production).rhs.getAccepts(position);