Kireck211
2/11/2017 - 3:31 AM

Analizador léxico

Analizador léxico

def main():
    while (True):
        print('Para salir del programa escriba -1 y enter')
        string = str(input('Escriba el input\n>'))
        if ('-1' in string):
            print('Adios!')
            break;
        print(getRelationOperators(string))
        print('\n\n')


def getRelationOperators(string):
    tokens = list(string)
    state = 0
    lexeme = ''
    for character in tokens:
        lexeme += character
        if state == 0:
            if character.isdigit():
                state = 1
            else:
                return '<Error 1>';
        elif state == 1:
            if character.isdigit():
                state = 1
            elif (character == '.'):
                state = 2
            elif (character.lower() == 'e'):
                state = 4
            else:
                return '<Error 1>'
        elif state == 2:
            if character.isdigit():
                state = 3
            else:
               return '<Error 1>' 
        elif state == 3:
            if character.isdigit():
                state = 3
            elif (character.lower() == 'e'):
                state = 4
            else:
                return '<Error 1>'
        elif state == 4:
            if (character == '+' or character == '-'):
                state = 5
            elif character.isdigit():
                state = 6
            else:
                return '<Error 1>'
        elif state == 5:
            if character.isdigit():
                state = 6
            else:
                return '<Error 1>'
        elif state == 6:
            if character.isdigit():
                state = 6

    if (state == 1):
        return '<entero, ' + lexeme + '>'
    if (state == 3):
        return '<decimal, ' + lexeme + '>'
    elif (state == 6):
        return '<exponente, ' + lexeme + '>'


main()