przetwarzanie_jezyka_natura.../D/calclex.py
2023-01-19 21:22:01 +01:00

77 lines
1.2 KiB
Python

from ply import lex
tokens = (
'NUMBER',
'OPERATE',
'SIZE',
'KIND',
'COLOR',
'MATERIAL'
)
def t_OPERATE(t):
r'Buy | Sell'
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
def t_SIZE(t):
r'tiny | small | big | large'
if t.value == 'tiny':
t.value = 1
elif t.value == 'small':
t.value = 2
elif t.value == 'big':
t.value = 3
elif t.value == 'large':
t.value = 4
return t
def t_COLOR(t):
r'(black | white | red | green | blue)'
if t.value == 'black':
t.value = 1
elif t.value == 'white':
t.value = 2
elif t.value == 'red':
t.value = 3
elif t.value == 'green':
t.value = 4
elif t.value == 'blue':
t.value = 5
return t
def t_MATERIAL(t):
r'metal | plastic'
if t.value == 'metal':
t.value = 1
elif t.value == 'plastic':
t.value = 2
return t
def t_KIND(t):
r'box(es)? | ring(s)?'
if t.value[0] == 'b':
t.value = 1
else:
t.value = 2
return t
def t_error(t):
print("Illegal character '%s" % t.value[0])
t.lexer.skip(1)
t_ignore = ' \t'
lexer = lex.lex()