-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
246 lines (184 loc) · 4.59 KB
/
parser.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from ply.lex import lex
from ply.yacc import yacc
# Reserved words
reserved = {
"se": "IF",
"senao": "ELSE",
"enquanto": "WHILE",
"vai_de": "FOR",
"amostre": "PRINT",
"eh_mermo": "BOOLEAN",
"migue": "BOOLEAN",
"ate": "FORRANGE",
}
# List of token names
tokens = [
"PLUS",
"MINUS",
"TIMES",
"DIVIDE",
"LPAREN",
"RPAREN",
"EQ",
"NE",
"LT",
"GT",
"LE",
"GE",
"ASSIGN",
"LBRACE",
"RBRACE",
"SEMICOLON",
"NAME",
"NUMBER",
"STRING",
] + list(set(reserved.values()))
# Ignored characters
t_ignore = " \t\r"
# Token matching rules
t_PLUS = r"\+"
t_MINUS = r"-"
t_TIMES = r"\*"
t_DIVIDE = r"/"
t_LPAREN = r"\("
t_RPAREN = r"\)"
t_EQ = r"=="
t_NE = r"!="
t_LE = r"<="
t_GE = r">="
t_LT = r"<"
t_GT = r">"
t_LBRACE = r"\{"
t_RBRACE = r"\}"
t_ASSIGN = r"="
t_SEMICOLON = r";"
def t_NUMBER(t):
r"\d+"
t.value = int(t.value)
return t
def t_STRING(t):
r"\"([^\\\n]|(\\.))*?\" "
t.value = t.value[1:-1]
return t
def t_NAME(t):
r"[a-zA-Z_][a-zA-Z0-9_]*"
t.type = reserved.get(t.value, "NAME")
return t
def t_newline(t):
r"\n+"
t.lexer.lineno += len(t.value)
# Error handling
def t_error(t):
print(f"Illegal character {t.value[0]!r}")
t.lexer.skip(1)
lexer = lex()
# PARSER
# Operator precedence
precedence = (
("right", "ASSIGN"),
("left", "EQ", "NE"),
("left", "LT", "LE", "GT", "GE"),
("left", "PLUS", "MINUS"),
("left", "TIMES", "DIVIDE"),
)
def p_statements_multiple(p):
"statements : statements statement"
p[0] = p[1] + "\n" + p[2]
def p_statements_single(p):
"statements : statement"
p[0] = p[1]
def p_statement(p):
"""
statement : assignment SEMICOLON
| if_statement
| for_statement
| while_statement
| block_statement
| print_statement SEMICOLON
"""
p[0] = p[1]
def p_for_statement(p):
"for_statement : FOR NAME LPAREN NUMBER FORRANGE NUMBER RPAREN block_statement"
loop_variable = p[2]
starting_index = p[4]
finish_index = p[6]
loop_body = p[8]
loop_code = f"for {loop_variable} in range({starting_index},{finish_index}):\n"
loop_body_indented = indent(loop_body)
loop_code += loop_body_indented
p[0] = loop_code
def p_while_statement(p):
"while_statement : WHILE LPAREN expression RPAREN block_statement"
condition = p[3]
p[0] = f"while ({condition}):\n{indent(p[5])}"
def p_assignment(p):
"assignment : NAME ASSIGN expression"
p[0] = f"{p[1]} = {p[3]}"
def p_if_statement(p):
"""
if_statement : IF LPAREN expression RPAREN block_statement
| IF LPAREN expression RPAREN block_statement ELSE block_statement
"""
condition = p[3]
if len(p) == 6:
# No else clause
p[0] = f"if ({condition}):\n{indent(p[5])}"
else:
# With else clause
p[0] = f"if ({condition}):\n{indent(p[5])}else:\n{indent(p[7])}"
def p_block_statement(p):
"block_statement : LBRACE statements RBRACE"
p[0] = p[2]
def p_print_statement(p):
"print_statement : PRINT LPAREN expression RPAREN"
p[0] = f"print({p[3]})"
def p_expression_binop(p):
"""
expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
| expression EQ expression
| expression NE expression
| expression LT expression
| expression LE expression
| expression GT expression
| expression GE expression
"""
p[0] = f"({p[1]} {p[2]} {p[3]})"
def p_expression_assignment(p):
"expression : NAME ASSIGN expression"
p[0] = f"{p[1]} = {p[3]}"
def p_expression_factor(p):
"expression : factor"
p[0] = p[1]
def p_factor_boolean(p: list[str]):
"factor : BOOLEAN"
if p[1] == "migue":
p[0] = "False"
else:
p[0] = "True"
def p_factor_number(p):
"factor : NUMBER"
p[0] = str(p[1])
def p_factor_name(p):
"factor : NAME"
p[0] = p[1]
def p_factor_string(p):
"factor : STRING"
p[0] = f'"{p[1]}"'
def p_factor_grouped(p):
"factor : LPAREN expression RPAREN"
p[0] = f"({p[2]})"
def p_factor_unary(p):
"factor : MINUS factor"
p[0] = f"(-{p[2]})"
def p_error(p):
print(f"Syntax error at {p.value!r}")
def indent(code):
indented_code = ""
for line in code.splitlines():
if line.strip() != "":
indented_code += " " + line + "\n"
return indented_code
parser = yacc()