package yacc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by sxk on 06/05/17.
 */
public class LALR1 {

    private Set<LR1.ItemSet> itemSetOfLR1;
    private Set<UnionItemSet> itemSetOfLALR1 = new HashSet<>();

    LALR1(Set<LR1.ItemSet> itemSets) {
        this.itemSetOfLR1 = itemSets;
    }

    class UnionItemSet {
        private ArrayList<LR1.ItemSet> unionItemSet = new ArrayList<>();
        private Set<String> lookaheadSymbols = new HashSet<>();

        public LR1.ItemSet getFirstItem() {
            return unionItemSet.get(0);
        }

        public Set<String> getLookaheadSymbols() {
            return lookaheadSymbols;
        }

        public void addItemSet(LR1.ItemSet state) {
            unionItemSet.add(state);
        }

        public void addLookaheadSymbols(String symbol) {
            lookaheadSymbols.add(symbol);
        }

        public boolean findItemSet(LR1.ItemSet state) {
            for (LR1.ItemSet stateOfItemSet : unionItemSet) {
                if (state.getState() == stateOfItemSet.getState()) {
                    return true;
                }
            }
            return false;
        }
    }

    public void generateLALR1() {

        //合并项集
        boolean ifAdd = false;
        for (LR1.ItemSet itemSet : itemSetOfLR1) {
            ifAdd = false;
            for (UnionItemSet unionItemSet : itemSetOfLALR1) {
                if (itemSet.equalItemSet(unionItemSet.getFirstItem())) {
                    unionItemSet.addItemSet(itemSet);
                    ifAdd = true;
                }
            }
            if (!ifAdd) {
                UnionItemSet unionSet = new UnionItemSet();
                unionSet.addItemSet(itemSet);
                itemSetOfLALR1.add(unionSet);
            }
        }

        //添加向前看符号
        for (UnionItemSet unionItemSet : itemSetOfLALR1) {

        }
    }
}

@@ -158,28 +158,24 @@
 private Set%3C
-LR1.
 ItemSet%3E ite
@@ -264,20 +264,16 @@
 LR1(Set%3C
-LR1.
 ItemSet%3E
@@ -355,16 +355,43 @@
 emSet %7B%0A
+        private int state;%0A
         
@@ -408,20 +408,16 @@
 rayList%3C
-LR1.
 ItemSet%3E
@@ -467,26 +467,38 @@
 private 
-Set%3CString
+ArrayList%3CSet%3CInteger%3E
 %3E lookah
@@ -510,30 +510,32 @@
 mbols = new 
-HashSe
+ArrayLis
 t%3C%3E();%0A%0A    
@@ -545,20 +545,16 @@
  public 
-LR1.
 ItemSet 
@@ -640,18 +640,30 @@
 lic 
-Set%3CString
+ArrayList%3CSet%3CInteger%3E
 %3E ge
@@ -755,36 +755,32 @@
 void addItemSet(
-LR1.
 ItemSet state) %7B
@@ -829,64 +829,363 @@
     
-%7D%0A%0A        public void addLookaheadSymbols(String s
+    if (lookaheadSymbols.isEmpty())%7B%0A                for (Item item : state.getItems()) %7B%0A                    lookaheadSymbols.add(item.getLookaheadSymbols());%0A                %7D%0A            %7D else %7B%0A                int i = 0;%0A                for (Item item : state.getItems()) %7B%0A                    for (Integer integerSymbols : item.getLookaheadS
 ymbol
+s()
 ) %7B%0A
@@ -1188,32 +1188,49 @@
 ) %7B%0A            
+           
+ if (!
 lookaheadSymbols
@@ -1234,21 +1234,196 @@
 ols.
-add(s
+get(i).contains(integerSymbols))%7B%0A                            lookaheadSymbols.get(i).add(integerS
 ymbol
+s
 );%0A
+                        %7D%0A                    %7D%0A%0A                %7D%0A            %7D%0A
     
@@ -1464,20 +1464,16 @@
 ItemSet(
-LR1.
 ItemSet 
@@ -1490,36 +1490,32 @@
            for (
-LR1.
 ItemSet stateOfI
@@ -1704,24 +1704,178 @@
 e;%0A        %7D
+%0A%0A        public int getState() %7B%0A            return state;%0A        %7D%0A%0A        public void setState(int state) %7B%0A            this.state = state;%0A        %7D
 %0A    %7D%0A%0A    
@@ -1943,32 +1943,55 @@
  ifAdd = false;%0A
+        int state = 0;%0A
         for (LR1
@@ -1987,20 +1987,16 @@
    for (
-LR1.
 ItemSet 

change add method