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

67 lines
1.3 KiB
Python

from ply import yacc
from calclex import tokens
def p_command(p):
'command : operate NUMBER article'
index = p[3]
if p[1] == 'Buy':
tab[index] += p[2]
print('OK. I am buying ' + str(p[2]) + ' new articles indexed as ' + str(index) + '.')
print('No of articles in shop: ' + str(tab[index]))
elif p[1] == 'Sell':
if p[2] > tab[index]:
print('I do not have as many articles as you want.')
else:
tab[index] -= p[2]
print('OK. I am selling ' + str(p[2]) + ' articles indexed as ' + str(index) + '.')
print('No of articles in shop: ' + str(tab[index]))
def p_article_attribute(p):
'article : attribute article'
p[0] = p[1] + p[2]
def p_attribute_color(p):
'attribute : COLOR'
p[0] = p[1]
def p_attribute_material(p):
'attribute : MATERIAL'
p[0] = 10 * p[1]
def p_attribute_size(p):
'attribute : SIZE'
p[0] = 100 * p[1]
def p_article_kind(p):
'article : KIND'
p[0] = 1000 * p[1]
def p_operate(p):
'operate : OPERATE'
p[0] = p[1]
def p_error(p):
print("Syntax error in input!")
tab = []
for index in range(3000):
tab.append(0)
parser = yacc.yacc()
while True:
s = input('What can I do for you? \n')
if s == 'Bye':
break
parser.parse(s)