-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplelex.py
178 lines (140 loc) · 2.98 KB
/
simplelex.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
174
175
176
177
# SIMPLE: A Simple Programming Language I Am Designing
reserved = { 'if' : 'IF', 'then' : 'THEN', 'else' : 'ELSE',}
tokens = list(reserved.values()) + ['ID','PLUS', 'NUMBER', 'MINUS', 'TIMES', 'DIVIDE', 'RPAREN', 'LPAREN', 'LT', 'GT', 'EQ', 'NE', 'LE', 'GE', 'AND', 'OR', 'NOT']
# tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LT = r'<'
t_GT = r'>'
t_EQ = r'='
t_NE = r'!='
t_LE = r'<='
t_GE = r'>='
t_AND = r'&'
t_OR = r'\|'
t_NOT = r'!!'
t_IF = r'IF'
t_THEN = r'THEN'
t_ELSE = r'ELSE'
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID') #check for reserved words
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
# ignored characters
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.lex.skip(1)
# build the lexer
import ply.lex as lex
lex.lex()
# precedence rules
precedence = (
('left', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE'),
('left', 'AND', 'OR'),
# ('right', 'UMINUS'),
)
# dictionary of ids
ids = {}
def p_statement_assign(p):
'statement : ID EQ expr'
ids[p[1]] = p[3]
def p_statement_if(p):
'statement : IF LPAREN boolexpr RPAREN THEN LPAREN expr RPAREN ELSE LPAREN expr RPAREN'
if p[3] == 0 : print(p[11])
elif p[3] == 1 : print(p[7])
def p_statement_expr(p):
'statement : expr'
print(p[1])
def p_boolexpr(p):
'''boolexpr : boolexpr AND boolexpr
| boolexpr OR boolexpr'''
if (len(p) > 1):
if p[2] == 'OR':
if p[1] == 1 or p[3] == 1 : p[0] = 1
else:
p[0] = 0
elif p[2] == 'AND':
if p[1] == 0 or p[3] == 0 : p[0] = 0
else:
p[0] = 1
else:
p[0] = p[1]
def p_bool(p):
'boolexpr : boolfactor'
p[0] = p[1]
def p_boolfactor_lt(p):
'boolfactor : expr LT expr'
if p[1] < p[3] : p[0] = 1
else:
p[0] = 0
def p_relexpr_gt(p):
'boolfactor : expr GT expr'
if p[1] > p[3] : p[0] = 1
else:
p[0] = 0
def p_relexpr_eq(p):
'boolfactor : expr EQ expr'
if p[1] == p[3] : p[0] = 1
else:
p[0] = 0
def p_relexpr_neq(p):
'boolfactor : expr NE expr'
if p[1] == p[3] : p[0] = 0
else:
p[0] = 1
def p_relexpr_le(p):
'boolfactor : expr LE expr'
if p[1] <= p[0] : p[0] = 1
else:
p[0] = 0
def p_relexpr_ge(p):
'boolfactor : expr GE expr'
if p[1] >= p[3] : p[0] = 1
else:
p[0] = 0
def p_expr_add(p):
'expr : expr PLUS expr'
p[0] = p[1] + p[3]
def p_expr_minus(p):
'expr : expr MINUS expr'
p[0] = p[1] - p[3]
def p_expr_times(p):
'expr : expr TIMES expr'
p[0] = p[1] * p[3]
def p_expr_divide(p):
'expr : expr DIVIDE expr'
p[0] = p[1] / p[3]
#def p_expr_bool(p):
# 'expr : boolfactor'
# p[0] = p[1]
def p_expr_number(p):
'expr : NUMBER'
p[0] = p[1]
def p_expr_id(p):
'expr : ID'
try:
p[0] = ids[p[1]]
except LookupError:
print("UNDEFINED VARIABLE: %s" % p[1])
p[0] = 0
def p_error(p):
print("ERROR AT %s" % p.value)
import ply.yacc as yacc
yacc.yacc()
while 1:
try:
s = raw_input('simple => ')
except EOFError:
break
yacc.parse(s)