-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMini_interpret.py
173 lines (144 loc) · 4.05 KB
/
Mini_interpret.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -----------------------------------------------------------------------------
# calc.py
#
# Expressions arithmétiques sans variables
# -----------------------------------------------------------------------------
#from genereTreeGraphviz2 import printTreeGraph
reserved = {
'print' : 'PRINT'
#print en minuscule reconnu dans calc
}
tokens = [
'NUMBER','MINUS',
'PLUS','TIMES','DIVIDE',
'LPAREN','RPAREN', 'AND', 'OR','TRUE','FALSE', 'SEMICOLON','NAME','AFFECT', 'INFTO', 'SUPTO'
] + list(reserved.values())
# Tokens
t_SEMICOLON = r'\;'
t_PLUS = r'\+'
t_MINUS = r'\-'
t_AND = r'\&'
t_OR = r'\|'
t_TIMES = r'\*'
t_DIVIDE = r'\/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_TRUE = r'T'
t_FALSE = r'F'
t_AFFECT = r'='
t_INFTO = r'\<'
t_SUPTO = r'\>'
names = {}
def t_NAME(t):
r'_[a-zA-Z_0-9]+ | [a-zA-Z][a-zA-Z_0-9]*'
t.type = reserved.get(t.value,'NAME')
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
# Ignored characters
t_ignore = " \t"
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
import ply.lex as lex
lex.lex()
precedence = (
('left', 'AND'),
('left', 'OR'),
('nonassoc', 'INFTO', 'SUPTO', 'AFFECT'),
('left','PLUS','MINUS'),
('left','TIMES','DIVIDE'),
#('right','UMINUS'),
)
def p_bloc(p):
''' START : START statement SEMICOLON
| statement SEMICOLON '''
p[0] = p[1]
def p_statement_print(p):
'statement : PRINT LPAREN expression RPAREN'
print(p[3])
def p_statement_affect(p):
'statement : NAME AFFECT expression '
names[p[1]] = p[3]
def p_affect(p):
'''
expression : NAME AFFECT expression
'''
names[p[1]] = p[3]
def p_expression_binop_plus(p):
'expression : expression PLUS expression'
p[0] = p[1] + p[3]
def p_expression_binop_times(p):
'expression : expression TIMES expression'
p[0] = p[1] * p[3]
def p_expression_binop_divide_and_minus(p):
'''expression : expression MINUS expression
| expression DIVIDE expression'''
if p[2] == '-': p[0] = p[1] - p[3]
else : p[0] = p[1] / p[3]
def p_expression_group(p):
'expression : LPAREN expression RPAREN'
p[0] = p[2]
def p_expression_number(p):
'expression : NUMBER'
p[0] = p[1]
def p_expression_name(p):
'expression : NAME'
p[0] = names[p[1]]
def p_expressionTrue(p) :
'expression : TRUE'
p[0] = True
def p_expressionFalse(p) :
'expression : FALSE'
p[0] = False
def p_expression_binop_boop(p) :
'''expression : expression AND expression
| expression OR expression'''
if p[2] == '&' :
p[0] = (p[1] and p[3])
elif p[2] == '|' :
p[0] = (p[1] or p[3])
def p_expression_inequal(p) :
'''expression : expression INFTO expression
| expression SUPTO expression '''
if p[2] == '<':
p[0] = p[1] < p[3]
elif p[2] == '>':
p[0] = p[1] > p[3]
def p_error(p):
print("Syntax error at '%s'" % p.value)
def eval(t) :
#print(' eval de ', t)
if type(t) is int:
return t
if type(t) is str:
return names[t]
if type(t) is tuple:
if t[0] == '+':
return eval(t[1]) + eval(t[2])
elif t[0] == '-':
return eval(t[1]) - eval(t[2])
elif t[0] == '*':
return eval(t[1]) * eval(t[2])
elif t[0] == '/':
return eval(t[1]) / eval(t[2])
elif t[0] == '&':
return eval(t[1]) & eval(t[2])
elif t[0] == '|':
return eval(t[1]) | eval(t[2])
elif t[0] == '<':
return eval(t[1]) < eval(t[2])
elif t[0] == '>':
return eval(t[1]) > eval(t[2])
import ply.yacc as yacc
yacc.yacc()
# print(eval(tup))
s = input('Tappez votre code >> ')
#s = ' print(eval(tup)) ; '
yacc.parse(s)