Instalando e usando o Ply no Python

Ply é um analisador sintático muito pratico para quem quer fazer esse tipo de coisa no python, onde ele retorna sua saída em forma de tokens, a sua instalação se da de maneira fácil através do pip.

Instalação

$ pip install ply

Exemplo

# ------------------------------------------------------------
# calclex.py
#
# tokenizer for a simple expression evaluator for
# numbers and +,-,*,/
# ------------------------------------------------------------
import ply.lex as lex

# List of token names.   This is always required
tokens = (
'NUMBER',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'LPAREN',
'RPAREN',
)

# Regular expression rules for simple tokens
t_PLUS    = r'\+'
t_MINUS   = r'-'
t_TIMES   = r'\*'
t_DIVIDE  = r'/'
t_LPAREN  = r'\('
t_RPAREN  = r'\)'

# A regular expression rule with some action code
def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)    
    return t

# Define a rule so we can track line numbers
def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)

# STRING contendo caracteres a serem ignorados(espaços e tabs)
t_ignore  = ' \t'

# Se deu Erro
def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

# Construindo o LEX
lexer = lex.lex()

# Entrada de Exemplo
data = '''
3 + 4 * 10
+ -20 *2
'''

# Passa a entrada para o analizador
lexer.input(data)

# Tokenize
while True:
    tok = lexer.token()
    if not tok: 
        break      # Termina a entrada
    print(tok)



Referências:

[ 1 ] – https://www.dabeaz.com/ply/ply.html