python Analizadorléxico

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Analizadorlé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()

以上是关于python Analizadorléxico的主要内容,如果未能解决你的问题,请参考以下文章

python générateurde phrase drole en python

python Récupérerlesdonnéessuitesàunesuppress d'objet

python 检查清单:Générationdelistedecritères

Python2中编码错误---é??ç»?人表ç?®ç??é?

python Python - 控制台:écrirelurlamêmeligne

python #Cescriptrécupèreuneliste de nomsetvérified'abords'il existent dans Wi