def apply_closure(state, my_item):
    if (my_item.isReduceItem == "Not-Reduce"):
        if (ffc.isNonTerminal(my_item.production[my_item.dot])):
            for production in grammar:
                if (production[0][0] == my_item.production[my_item.dot]):
                    if (production[0][3] == "#"):
                        new_item = create_new_item(production[0], "Closure", 3,
                                                   "Reduce")
                    else:
                        new_item = create_new_item(production[0], "Closure", 3,
                                                   "Not-Reduce")
                    if (new_item not in state.item_l):
                        state.add_item(new_item)
                        if (ffc.isNonTerminal(
                                new_item.production[new_item.dot])):
                            apply_closure(state, new_item)
def apply_closure(state, my_item, recursion):
    if (my_item.isReduceItem == "Not-Reduce"):
        if (ffc.isNonTerminal(my_item.production[my_item.dot])):
            for production in grammar:
                if (production[0][0] == my_item.production[my_item.dot]):
                    temp_lookAhead_l = []
                    if (my_item.dot == len(my_item.production) - 1):
                        for element in my_item.set_of_rec_equations:
                            if (element not in temp_lookAhead_l):
                                temp_lookAhead_l.append(element)
                    else:
                        p_prog = my_item.dot
                        stopped = False
                        while (p_prog + 1 <= len(my_item.production) - 1
                               and not stopped):
                            if (ffc.isTerminal(my_item.production[p_prog +
                                                                  1])):
                                if (my_item.production[p_prog + 1]
                                        not in temp_lookAhead_l):
                                    temp_lookAhead_l.append(
                                        my_item.production[p_prog + 1])
                                    stopped = True
                            else:
                                for nT in non_terminals:
                                    if (nT.name == my_item.production[p_prog +
                                                                      1]):
                                        for first_nT in nT.first_l:
                                            if (first_nT != "#"):
                                                if (first_nT not in
                                                        temp_lookAhead_l):
                                                    temp_lookAhead_l.append(
                                                        first_nT)
                                            else:
                                                if (p_prog + 1 == len(
                                                        my_item.production) -
                                                        1):
                                                    for item_clos_rec_eq in my_item.set_of_rec_equations:
                                                        if (item_clos_rec_eq
                                                                not in
                                                                temp_lookAhead_l
                                                            ):
                                                            temp_lookAhead_l.append(
                                                                item_clos_rec_eq
                                                            )
                            p_prog += 1
                    if (production[0][3] == "#"):
                        new_temp_item = create_new_lr0_item(
                            production[0], 3, "Closure", "Reduce")
                        temp_type = "Reduce"
                    else:
                        new_temp_item = create_new_lr0_item(
                            production[0], 3, "Closure", "Not-Reduce")
                        temp_type = "Not-Reduce"
                    found = False
                    for item_for_la_merge in state.item_l:
                        tmp_item = create_new_lr0_item(
                            item_for_la_merge.production,
                            item_for_la_merge.dot, item_for_la_merge.type,
                            item_for_la_merge.isReduceItem)
                        if (tmp_item == new_temp_item):
                            for la_to_merge in temp_lookAhead_l:
                                if (la_to_merge not in item_for_la_merge.
                                        set_of_rec_equations[0].symbol_list):
                                    item_for_la_merge.set_of_rec_equations[
                                        0].symbol_list.append(la_to_merge)
                            found = True
                    if (not found):
                        new_item = create_new_lr0_item(production[0], 3,
                                                       "Closure", temp_type)
                        new_item_rec_eq = create_new_rec_equation()
                        for symb_to_add in temp_lookAhead_l:
                            if (symb_to_add
                                    not in new_item_rec_eq.symbol_list):
                                new_item_rec_eq.symbol_list.append(symb_to_add)
                        add_rec_equation(new_item, new_item_rec_eq)
                        rec_equations.append(new_item_rec_eq)
                        if (new_item not in state.item_l):
                            state.item_l.append(new_item)
                            #print("Adding " + new_item.production + " to state " + str(state.name))
                            if (recursion < 2):
                                if (ffc.isNonTerminal(
                                        new_item.production[new_item.dot])):
                                    #print("recurring for " + new_item.production, recursion)
                                    apply_closure(state, new_item,
                                                  recursion + 1)
table = [["" for x in range(total_lenght)] for y in range(state_counter)]

# SLR(0)-parsing table computation
for idx_row in range(state_counter):
    for idx_col in range(total_lenght):
        if (idx_col == 0):
            table[idx_row][idx_col] = idx_row
        else:
            table[idx_row][idx_col] = []

for idx, element in enumerate(header):
    if (element == "$"):
        table[1][idx].append("Accept")
for transition in transitions:
    new_entry = ""
    if (ffc.isNonTerminal(transition.element)):
        new_entry = "Goto " + str(transition.ending_state)
        for idx, element in enumerate(header):
            if (element == transition.element):
                table[transition.starting_state][idx].append(new_entry)
    elif (ffc.isTerminal(transition.element)):
        new_entry = "S" + str(transition.ending_state)
        for idx, element in enumerate(header):
            if (element == transition.element):
                table[transition.starting_state][idx].append(new_entry)
for state in lr0_states:
    for item in state.item_l:
        found = False
        if ("Q->" not in item.production):
            new_entry = ""
            if (item.isReduceItem == "Reduce"):
Beispiel #4
0
                         driver_index = idx
                 for idx, element_2 in enumerate(terminal_names, 0):
                     if (element_2 == follow_nT):
                         terminal_index = idx
                 table[driver_index][terminal_index].append(production[0])
 elif (ffc.isTerminal(production[0][p_prog])):
     driver_index = 0
     terminal_index = 0
     for idx, element in enumerate(non_terminal_names, 0):
         if (element == production[0][0]):
             driver_index = idx
     for idx, element in enumerate(terminal_names, 0):
         if (element == production[0][p_prog]):
             terminal_index = idx
     table[driver_index][terminal_index].append(production[0])
 elif (ffc.isNonTerminal(production[0][p_prog])):
     stopped = False
     while (p_prog <= len(production[0]) - 1 and not stopped):
         for nT in non_terminals:
             if (nT.name == production[0][p_prog]):
                 if ("#" in nT.first_l):
                     #print(nT.name, derives_eps_check, derives_eps_check+1)
                     derives_eps_check += 1
                     for first_nT in nT.first_l:
                         if (first_nT != '#'):
                             driver_index = 0
                             terminal_index = 0
                             for idx, element_1 in enumerate(
                                     non_terminal_names, 0):
                                 if (element_1 == production[0][0]):
                                     driver_index = idx