-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofe.py
113 lines (88 loc) · 1.93 KB
/
profe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import ply.lex as lex
import ply.yacc as yacc
reserved={
'mas' : 'SU',
'menos' : 'RE',
'es' : 'ASIGN'
}
tokens = [
'N',
'ID'
] + list(reserved.values())
t_SU = r'mas'
t_RE = r'menos'
t_ASIGN = r'es'
def t_N(t):
r'[0-9]+'
t.value = int(t.value)
return t
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value,'ID')
return t
t_ignore = ' \t'
def t_error(t):
print("Invalid Token:",t.value[0])
t.lexer.skip(1)
precedence = (( 'left', 'SU', 'RE' ),)
variables = {}
def p_resultado(t):
'resultado : s'
print(t[1])
def p_asignacion(t):
'resultado : ID ASIGN s'
variables[t[1]] = t[3]
def p_expr_num(t):
's : N'
t[0] = t[1]
def p_expr_id(t):
's : ID'
try:
t[0] = variables[t[1]]
except LookupError:
print("Undefined variable '%s'" % t[1])
t[0] = 0
def p_expr_op(t):
'''s : s SU s
| s RE s'''
if t[2] == 'mas':
t[0] = t[1] + t[3]
elif t[2] == 'menos':
t[0] = t[1] - t[3]
def p_error(t):
print("Syntax error in input!")
lexer = lex.lex()
parser = yacc.yacc()
res = parser.parse("2 mas 3") # the input
while True:
try:
data = input()
except EOFError:
break
parser.parse(data)
# def t_newline(t):
# r'\n+'
# t.lexer.lineno += len( t.value )
# def p_s2uminus(t) :
# 's : RESTA s %prec RESTA'
# t[0] = - t[2]
# def p_mult_div(t) :
# '''s : s MULTIPLICA s
# | s DIVIDE s'''
# if t[2] == 'mult' :
# t[0] = t[1] * t[3]
# else :
# if t[3] == 0 :
# print("Can't divide by 0")
# raise ZeroDivisionError('integer division by 0')
# t[0] = t[1] / t[3]
# def p_s_NUM(t) : ##
# 's : N'
# t[0] = t[1]
# def p_s_id(t): ##
# 's : ID'
# try:
# t[0] = variables[t[1]]
# except LookupError:
# print("Undefined variable '%s'" % t[1])
# t[0] = 0