Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
def build_parsing_table(G, firsts, follows):
    # init parsing table
    M = {}

    # P: X -> alpha
    for production in G.Productions:
        X = production.Left
        alpha = production.Right

        term = G.terminals
        term.append(G.EOF)

        for item in term:
            if item in firsts[alpha]:
                if (tuple([X, item]) in M and M[tuple([X, item])] == [
                        production,
                ]) or tuple([X, item]) not in M:
                    M[tuple([X, item])] = [
                        production,
                    ]
                else:
                    st.error('La gramatica no es LL(1)')
                    return None
                    #raise EnvironmentError("Error de insercion en la tabla, problemas en la produccion: ", production, " Existe conflicto!")

            if firsts[alpha].contains_epsilon and (item in follows[X]):
                if (tuple([X, item]) in M and M[tuple([X, item])] == [
                        production,
                ]) or tuple([X, item]) not in M:
                    temp_container = ContainerSet()
                    temp_container.set_epsilon()
                    M[tuple([X, item])] = [
                        production,
                    ]
                else:
                    st.error('La gramatica no es LL(1)')
                    return None
                    #raise EnvironmentError("Error de insercion en la tabla, problemas en la produccion: ", production, " Existe conflicto!")

    # parsing table is ready!!!
    return M
Ejemplo n.º 8
0
    def _build_parsing_table(self):
        G = self._G
        firsts = self._firsts
        follows = self._follows

        # init parsing table
        M = {}

        # P: X -> alpha
        for production in G.Productions:
            X = production.Left
            alpha = production.Right

            term = [item for item in G.terminals]
            term.append(G.EOF)

            for item in term:
                if item in firsts[alpha]:
                    if (tuple([X, item]) in M and M[tuple([X, item])] == [
                            production,
                    ]) or tuple([X, item]) not in M:
                        M[tuple([X, item])] = [
                            production,
                        ]
                    else:
                        M[tuple([X, item])].append(production)

                if firsts[alpha].contains_epsilon and (item in follows[X]):
                    if (tuple([X, item]) in M and M[tuple([X, item])] == [
                            production,
                    ]) or tuple([X, item]) not in M:
                        temp_container = ContainerSet()
                        temp_container.set_epsilon()
                        M[tuple([X, item])] = [
                            production,
                        ]
                    else:
                        M[tuple([X, item])].append(production)

        # parsing table is ready!!!
        return M
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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()
        return first_alpha
    ###################################################
    ###################################################
    # alpha = X1 ... XN
    # First(X1) 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>                   #
    change = False
    for i in alpha:
        if not firsts[i].contains_epsilon:
            # epsilon pertenece a First(X1)...First(Xi) ? First(Xi+1) subconjunto de First(X) y First(alpha)
            change = True
        for j in firsts[i]:
            first_alpha.add(j)
        if change:
            break
    if not change:
        # epsilon pertenece a First(X1)...First(XN) ? epsilon pertence a First(X) y al First(alpha)
        first_alpha.set_epsilon()    
    ###################################################
    # First(alpha)
    return first_alpha