Beispiel #1
0
def obtch():
    from auxiliares import error
    global fin_archivo
    global offset
    global ll
    global linea
    global MAXLINEA
    global contadorLineas
    if (fin_archivo == 1):
        error(32)
    #Le pongo el or pues si es una linea de comentario deberia de saltarse esa linea y leer otra
    if (offset == ll - 1 or Lexico.token == Lexico.simbolo.linecomment):
        if (Lexico.token == Lexico.simbolo.linecomment):
            contadorLineas += 1
        ll = getline(MAXLINEA)
        offset = -1
        if (ll == 0):
            fin_archivo = 1
            #consolef.write(linea)
            #print(linea)
            offset -= 1
    offset += 1
    #
    if (offset == -1):
        return ' '
    actual = linea[contadorLineas][offset]
    if ((fin_archivo == 1)):
        return ' '
    elif (actual == '\n'):
        contadorLineas += 1
        return ' '
    else:
        return (linea[contadorLineas][offset])
Beispiel #2
0
def main(argv):
    global fp
    if argv == None:
        print(
            "\nNo se ha proporcionado el nombre del programa fuente (uso: parser1 progfuente)"
        )

    else:
        fp = open(argv, "r")
        if fp == None:
            print("\nNo se encontro el programa fuente indicado")
        else:
            timer = datetime.datetime.today()

            #print("\n\nCompilador de cpiton version 69.0/parser1 --- Octubre de 2019 --- \n")
            #print(timer)
            #print("\nLexema                  Token\n")

            from auxiliares import inicializar_espec
            #print(auxiliares.hola)

            inicializar_espec()

            scanner.consolef.write(
                "\n\nCompilador de cpiton version 69.0/parser1 --- Octubre de 2019 --- \n"
            )
            scanner.consolef.write(timer.strftime('%d/%m/%Y'))
            scanner.consolef.write("\nLexema                  Token\n")

            scanner.ch = ' '
            scanner.fin_de_archivo = 0
            scanner.offset = -1
            scanner.ll = 0
            No_de_errores = 0

            #inicializacion de conjuntos de estabilizacion (en conjuntos.py)
            inicializar_conjuntos()

            scanner.obtoken()

            tds.it = 0
            #global token
            #token = scanner.obtoken()
            #while(token != lexico.simbolo.mdputok):
            #    token = scanner.obtoken()
            #
            parseador.inicio()

            from auxiliares import error
            if lexico.token != lexico.simbolo.mdputok:
                error(32)

            from auxiliares import estadisticas
            estadisticas()
            scanner.consolef.close()
            fp.close()

    return 0
Beispiel #3
0
def poner(k):
    from auxiliares import error
    global it
    it += 1
    if (it > MAXIT):
        error(31)
    else:
        nuevo = registro(Scanner.lex, k)
        tabla.insert(it, nuevo)
        mergeSort(tabla)
Beispiel #4
0
def leerints(lexid, i, j, MAX, checkDecimal):
    global ch
    while (1):
        try:
            prueba = int(ch)
            if (i < MAX):
                i += 1
                lexid += ch
            j += 1
            ch = obtch()
        except ValueError:
            if (checkDecimal):
                if (j == 1 and ch == ';'):
                    error(
                        1
                    )  #si en la primera iteracion no viene un numero es como que pongan 2. y nada mas
            break
    if (j > MAX):
        error(33)
    return lexid
Beispiel #5
0
def obtoken():
    global lextoken
    global caracteres
    #from auxiliares import error
    lexid = None
    global lex
    ok = 0
    global ch

    while (ch == ' ' or ch == '\n' or ch == '\t'):
        ch = obtch()

    if (ch.isalpha()):
        lexid = ch
        i = 1
        ch = obtch()
        while (ch.isalnum() or ord(ch) == '95'):
            if (i < MAXID):
                i += 1
                lexid += ch
            ch = obtch()

        for j in range(0, MAXPAL):
            if (lexid == Lexico.lexpal[j]):
                ok = 1
                break

        if (ok == 1):
            Lexico.token = Lexico.tokpal[j]
        else:
            Lexico.token = Lexico.simbolo.ident

        lex = lexid[:]

    else:
        try:
            prueba = int(ch)
            lexid = ch
            i = j = 1
            ch = obtch()
            isDouble = False
            lexid = leerints(lexid, i, j, MAXDIGIT, False)
            #VERIFICANDO SI ES DECIMAL
            if (ch == '.'):
                isDouble = True
                lexid += ch
                ch = obtch()
                i = j = 1
                lexid = leerints(lexid, i, j, MAXDECIMAL, True)

            if (isDouble):
                Lexico.token = Lexico.simbolo.decimal
                valor = float(lexid)
            else:
                Lexico.token = Lexico.simbolo.numero
                valor = int(lexid)
        except ValueError:
            if (ch == ':'):
                caracteres = ch
                ch = obtch()
                if (ch == 'v'):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.dputok
                    ch = obtch()
                else:
                    error(28)
            elif (ch == '<'):
                caracteres = ch
                ch = obtch()
                if (ch == '='):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.mei
                    ch = obtch()
                else:
                    Lexico.token = Lexico.simbolo.mnr
            elif (ch == '>'):
                caracteres = ch
                ch = obtch()
                if (ch == '='):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.mai
                    ch = obtch()
                elif (ch == ':'):
                    caracteres += ch
                    ch = obtch()
                    if (ch == 'v'):
                        caracteres += ch
                        Lexico.token = Lexico.simbolo.mdputok
                        ch = obtch()
                    else:
                        Lexico.token = Lexico.simbolo.nulo
                        ch = obtch()
                        error(28)
                else:
                    Lexico.token = Lexico.simbolo.myr
            elif (ch == '='):
                caracteres = ch
                ch = obtch()
                if (ch == '='):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.igl
                    ch = obtch()
                else:
                    Lexico.token = Lexico.simbolo.asignacion
            elif (ch == '!'):
                caracteres = ch
                ch = obtch()
                if (ch == '='):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.nig
                    ch = obtch()
                else:
                    error(15)
            elif (ch == '/'):
                caracteres = ch
                ch = obtch()
                if (ch == '/'):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.linecomment
                    ch = obtch()
                elif (ch == '*'):
                    caracteres += ch
                    Lexico.token = Lexico.simbolo.startcomment
                    lextoken = caracteres + "                  " + str(
                        Lexico.token) + "\n"
                    consolef.write(lextoken)
                    while (1):
                        ch = obtch()
                        if (ch == '*'):
                            caracteres = ch
                            ch = obtch()
                            if (ch == '/'):
                                caracteres += ch
                                Lexico.token = Lexico.simbolo.endcomment
                                ch = obtch()
                                break
                        if (ch == ''):
                            error(46)
                            break
                else:
                    error(15)
            #elif(ch == '*'):
            #    caracteres = ch
            #    ch = obtch()
            #    if(ch == '/'):
            #        caracteres += ch
            #        Lexico.token = Lexico.simbolo.endcomment
            #        ch = obtch()
            #    else:
            #        Lexico.token = Lexico.simbolo.por
            elif (ord(ch) == 34):
                lexid = ch
                ch = obtch()
                while (ord(ch) != 34):
                    lexid += ch
                    ch = obtch()
                if (ch == ''):
                    error(30)
                lexid += ch
                Lexico.token = Lexico.simbolo.texto
                ch = obtch()
            elif (ord(ch) == 39):
                lexid = ch
                ch = obtch()
                lexid += ch
                ch = obtch()
                if (ord(ch) != 39):
                    error(31)
                else:
                    lexid += ch
                    Lexico.token = Lexico.simbolo.caracter
                    ch = obtch()
            else:
                caracteres = ch
                Lexico.token = Lexico.espec[ord(ch)]
                ch = obtch()

    if (lexid is None):
        lexid = caracteres
    lextoken = lexid + "                  " + str(Lexico.token) + "\n"
    consolef.write(lextoken)
    if (Lexico.token == Lexico.simbolo.linecomment):
        #para que no vuelva a entrar al if de la funcion obtch()
        Lexico.token = Lexico.simbolo.nulo
        obtoken()
    if (Lexico.token == Lexico.simbolo.endcomment):
        Lexico.token = Lexico.simbolo.nulo
        obtoken()