def nfa_to_dfa(automaton): transitions = {} start = epsilon_closure(automaton, [automaton.start]) start.id = 0 start.is_final = any(s in automaton.finals for s in start) states = [start] pending = [start] while pending: state = pending.pop() for symbol in automaton.vocabulary: e_closure = ContainerSet(*move(automaton, state, symbol)) e_closure.update(epsilon_closure(automaton, e_closure.set)) if (len(e_closure) == 0): continue try: transitions[state.id, symbol] = states[states.index(e_closure)].id except ValueError: e_closure.id = len(states) e_closure.is_final = any(s in automaton.finals for s in e_closure) states.append(e_closure) pending.append(e_closure) transitions[state.id, symbol] = e_closure.id finals = [state.id for state in states if state.is_final] dfa = DFA(len(states), finals, transitions) return dfa
def compute_local_first(firsts, alpha): """ Computes First(alpha), given First(Vt) and First(Vn) alpha in (Vt U Vn)* """ first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False # alpha == epsilon ? First(alpha) = { epsilon } if alpha_is_epsilon: first_alpha.set_epsilon() # alpha = X1 ... XN # First(Xi) subset of First(alpha) # epsilon in First(X1)...First(Xi) ? First(Xi+1) subset of First(X) & First(alpha) # epsilon in First(X1)...First(XN) ? epsilon in First(X) & First(alpha) else: for symbol in alpha: first_symbol = firsts[symbol] first_alpha.update(first_symbol) if not first_symbol.contains_epsilon: break else: first_alpha.set_epsilon() return first_alpha
def ComputeLocalFirst(firsts: dict, alpha) -> ContainerSet: """ Computa el conjunto First de la cadena alpha, esta cadena puede tener tanto terminales como non-terminales. """ first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False # alpha == epsilon ? First(alpha) = { epsilon } if alpha_is_epsilon: first_alpha.set_epsilon() # alpha = X1 ... XN # First(Xi) subset of First(alpha) # epsilon in First(X1)...First(Xi) ? First(Xi+1) subset of First(X) & First(alpha) # epsilon in First(X1)...First(XN) ? epsilon in First(X) & First(alpha) else: for symbol in alpha: first_symbol = firsts[symbol] first_alpha.update(first_symbol) if not first_symbol.contains_epsilon: break else: first_alpha.set_epsilon() return first_alpha
def compute_local_first_queue(firsts, alpha): first_alpha = ContainerSet() temp = [] try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False if alpha_is_epsilon: first_alpha.set_epsilon(True) return first_alpha breaked = False while len(alpha) > 0: symbol = alpha.pop() temp.append(symbol) first_alpha.update(firsts[symbol]) if not firsts[symbol].contains_epsilon: breaked = True break if not breaked: first_alpha.set_epsilon(True) while len(temp) > 0: alpha.append(temp.pop()) return first_alpha
def Compact_Automata(self, automata): new_states = {} for state in automata: new_states[state] = state states_to_compress = [] for state1 in automata: if not new_states[state1] == state1: continue states_to_compress = [state1] for state2 in automata: if state1 == state2 or not new_states[state2] == state2: continue node1 = state1.state node2 = state2.state are_equals = False if len(node1) == len(node2): for item1 in node1: are_equals = False for item2 in node2: if item1.Center() == item2.Center(): are_equals = True if not are_equals: break if are_equals: states_to_compress.append(state2) compress_set = ContainerSet() for state in states_to_compress: node = state.state compress_set.update(ContainerSet(*node)) new_node = self.compress(compress_set) new_state = State(frozenset(new_node), True) for state in states_to_compress: new_states[state] = new_state new_automata = new_states[automata] for state in automata: for key in state.transitions: for to_state in state.transitions[key]: try: assert new_states[to_state] in new_states[state].transitions[key] except: new_states[state].add_transition(key, new_states[to_state]) return new_automata
def expand(item, firsts): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] lookaheads = ContainerSet() # Your code here!!! (Compute lookahead for child items) for string in item.Preview(): lookaheads.update(compute_local_first(firsts, string)) assert not lookaheads.contains_epsilon # Your code here!!! (Build and return child items) return [Item(prod, 0, lookaheads) for prod in next_symbol.productions]
def expand(item, firsts): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] lookaheads = ContainerSet() for preview in item.Preview(): local_first = compute_local_first(firsts, preview) lookaheads.update(local_first) assert not lookaheads.contains_epsilon return [Item(p, 0, lookaheads) for p in next_symbol.productions]
def closure_lr1(self, items, firsts): closure = ContainerSet(*items) changed = True while changed: changed = False new_items = ContainerSet() for item in closure: new_items.update(ContainerSet(*self.expand(item, firsts))) changed = closure.update(new_items) return self.compress(closure)
def expand(item, firsts): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] lookaheads = ContainerSet() for prev in item.Preview(): new_first = compute_local_first(firsts, prev) lookaheads.update(new_first) assert not lookaheads.contains_epsilon result = [] for prod in next_symbol.productions: result.append(Item(prod, 0, lookaheads)) return result
def expand(item, firsts): next_symbol = item.NextSymbol if next_symbol is None or next_symbol.IsTerminal: return [] lookaheads = ContainerSet() result = [] for preview in item.Preview(): lookaheads.update(compute_local_firsts(firsts, preview)) assert not lookaheads.contains_epsilon, "lookaheads contains epsilon" result = [] for i, production in enumerate(next_symbol.productions): result.append(Item(production, 0, lookaheads)) return result
def expand(item, firsts): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] lookaheads = ContainerSet( ) # lookahead = que yo quiero ver cuando vaya a reducir # Your code here!!! (Compute lookahead for child items) for prev in item.Preview(): lookaheads.update(compute_local_first(firsts, prev)) assert not lookaheads.contains_epsilon # Your code here!!! (Build and return child items) return [Item(x, 0, lookaheads) for x in next_symbol.productions]
def expand(item, firsts, with_lookaheads=True): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] if with_lookaheads: lookaheads = ContainerSet() # Your code here!!! (Compute lookahead for child items) preview = item.Preview() for sentence in preview: lookaheads.update(compute_local_first(firsts, sentence)) assert not lookaheads.contains_epsilon return [ Item(production, 0, [l for l in lookaheads]) for production in next_symbol.productions ]
def expand(item, firsts): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] lookaheads = ContainerSet() # (Compute lookahead for child items) previews = item.Preview() for preview in previews: lookaheads.update(compute_local_first(firsts, preview)) assert not lookaheads.contains_epsilon # (Build and return child items) items = [] for production in next_symbol.productions: items.append(Item(production, 0, lookaheads)) return items
def compute_local_first(firsts, alpha): first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False if alpha_is_epsilon: first_alpha.set_epsilon() else: for x in alpha: symbol_first = firsts[x] first_alpha.update(symbol_first) if not symbol_first.contains_epsilon: break else: first_alpha.set_epsilon() # First(alpha) return first_alpha
def closure_lr1(items, firsts): closure = ContainerSet(*items) changed = True while changed: new_items = ContainerSet() for item in closure: new_items.extend(expand(item, firsts)) changed = closure.update(new_items) return compress(closure)
def _compute_local_first(firsts, alpha): first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False if alpha_is_epsilon: first_alpha.set_epsilon() for symbol in alpha: first_alpha.update(firsts[symbol]) if not firsts[symbol].contains_epsilon: break else: first_alpha.set_epsilon() return first_alpha
def compute_local_first(firsts, alpha): first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except AttributeError: alpha_is_epsilon = False if alpha_is_epsilon: first_alpha.set_epsilon() else: for symbol in alpha: first_symbol = firsts[symbol] first_alpha.update(first_symbol) if not first_symbol.contains_epsilon: break else: first_alpha.set_epsilon() return first_alpha
def compute_local_first(firsts, alpha): first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False if alpha_is_epsilon: first_alpha.set_epsilon() else: for item in alpha: first_alpha.update(firsts[item]) if not firsts[item].contains_epsilon: break else: first_alpha.set_epsilon() # First(alpha) return first_alpha
def ClosureLR1(items, firsts): closure = ContainerSet(*items) changed = True while changed: changed = False new_items = ContainerSet() for item in closure: new_items.extend(LR1Parser.Expand(item, firsts)) changed = closure.update(new_items) return LR1Parser.Compress(closure)
def compute_local_first(firsts, alpha): first_alpha = ContainerSet() try: alpha_is_epsilon = alpha.IsEpsilon except: alpha_is_epsilon = False ################################################### # alpha == epsilon ? First(alpha) = { epsilon } ################################################### # <CODE_HERE> # ################################################### if alpha_is_epsilon: first_alpha.set_epsilon() ################################################### # alpha = X1 ... XN # First(Xi) subconjunto First(alpha) # epsilon pertenece a First(X1)...First(Xi) ? First(Xi+1) subconjunto de First(X) y First(alpha) # epsilon pertenece a First(X1)...First(XN) ? epsilon pertence a First(X) y al First(alpha) ################################################### # <CODE_HERE> # ################################################### else: for w in alpha: f = firsts[w] first_alpha.update(f) if not f.contains_epsilon: break else: first_alpha.set_epsilon() # First(alpha) return first_alpha
def closure_lr1(items, firsts): closure = ContainerSet(*items) changed = True while changed: changed = False new_items = ContainerSet() # Your code here!!! for x in closure: new_items.extend(expand(x, firsts)) changed = closure.update(new_items) return compress(closure)
def expand(self, item, firsts): next_symbol = item.NextSymbol if next_symbol is None or not next_symbol.IsNonTerminal: return [] lookaheads = ContainerSet() new_items = [] previews = item.Preview() for preview in previews: sentence = self.G.Epsilon for symbol in preview: sentence = sentence + symbol try: prev_first = firsts[sentence] except KeyError: prev_first = firsts[sentence] = compute_local_first(firsts, preview) lookaheads.update(prev_first) for prod in next_symbol.productions: new_item = Item(prod, 0, lookaheads = lookaheads) new_items.append(new_item) return new_items
def closure_lr1(items, firsts, with_lookaheads=True): closure = ContainerSet(*items) changed = True while changed: changed = False new_items = ContainerSet() # Your code here!!! for item in closure: new_items.extend(expand(item, firsts)) changed = closure.update(new_items) if with_lookaheads: return compress(closure) else: return closure