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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#!/bin/env python3
import sys
import argparse
import logging
import ply.lex as lex
import ply.yacc as yacc
from enum import Enum
# args
parser = argparse.ArgumentParser(
prog='henshin'
)
parser.add_argument('filename')
args = parser.parse_args()
input = open(args.filename).read()
# log
logging.basicConfig(
level = logging.DEBUG,
filename = "parselog.txt",
filemode = "w",
)
log = logging.getLogger()
# Lexer
reserved = {
"function": "FUNCTION",
"if": "IF",
"else": "ELSE",
"return": "RETURN",
"const": "CONST",
"var": "VAR",
"integer": "TYPE_INTEGER",
"string": "TYPE_STRING",
"void": "TYPE_VOID",
}
tokens = [
"OPERATOR_PLUS",
"OPERATOR_MINUS",
"OPERATOR_MULTIPLY",
"OPERATOR_DIVIDE",
"ASSIGN",
"OPERATOR_PIPE",
"OPERATOR_PIPE_REPLACEMENT",
"PARENTHESIS_LEFT",
"PARENTHESIS_RIGHT",
"BRACE_LEFT",
"BRACE_RIGHT",
"BRACKET_LEFT",
"BRACKET_RIGHT",
"BIT_OR",
"NAMESPACE_ACCESSOR",
"COMMA",
"COLON",
"SEMICOLON",
"IDENTIFIER",
"NUMBER",
"STRING",
"COMMENT",
] + list(reserved.values())
t_OPERATOR_PLUS = r'\+'
t_OPERATOR_MINUS = r'-'
t_OPERATOR_MULTIPLY = r'\*'
t_OPERATOR_DIVIDE = r'/'
t_ASSIGN = '='
t_OPERATOR_PIPE = r'\|>'
t_OPERATOR_PIPE_REPLACEMENT = r'\$'
t_PARENTHESIS_LEFT = r'\('
t_PARENTHESIS_RIGHT = r'\)'
t_BRACE_LEFT = r'\['
t_BRACE_RIGHT = r'\]'
t_BRACKET_LEFT = r'{'
t_BRACKET_RIGHT = r'}'
t_BIT_OR = r'\|'
t_NAMESPACE_ACCESSOR = r'\.'
t_COMMA = r','
t_COLON = r':'
t_SEMICOLON = r';'
def t_IDENTIFIER(t):
r'[a-zA-Z][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'IDENTIFIER')
return t
def t_NUMBER(t):
r'[0-9]+'
t.value = int(t.value)
return t
def t_STRING(t):
r'(".+"|\'.*\')'
return t
def t_COMMENT(t):
r'//.*'
pass
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
t_ignore = ' \t'
def t_error(t):
print("undefined: '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex(debug=True, debuglog=log)
lexer.input(input)
lineno = 0
for token in lexer:
if token.lineno != lineno:
lineno = token.lineno
print("\nLine %s:" % token.lineno)
# print('%s: "%s" --' % (token.type, token.value), end=' ')
print(token.value, end=' ')
print("\n")
# Parser
class AstNodeExpressionType(Enum):
IDENTIFIER = 'identifier'
NUMBER = 'number'
STRING = 'string'
class AstNode: pass
class AstNodeVariableDeclarationStatement(AstNode):
def __init__(self, type, name, value_type, value):
self.type = type
self.name = name
self.value_type = value_type
self.value = value
class AstNodeVariableReassignmentStatement(AstNode):
def __init__(self, name, value):
self.type = type
self.value = value
class AstNodeExpression(AstNode):
def __init__(self, type, value):
self.type = type
self.value = value
class AstNodeOperatorExpression(AstNode):
def __init__(self, type, left, right):
self.type = type
self.left = left
self.right = right
precedence = (
('left', 'OPERATOR_PLUS', 'OPERATOR_MINUS'),
('left', 'OPERATOR_MULTIPLY', 'OPERATOR_DIVIDE'),
)
def p_statements(p):
'''statements : statement
| statement statements'''
def resolve_nodes(node, level):
node_dict = node.__dict__
for property in node_dict:
if isinstance(node_dict[property], AstNode):
print('> '*level, property, node_dict[property].__class__.__name__)
resolve_nodes(node_dict[property], level+1)
else:
print('> '*level, property, node_dict[property])
if p[1]:
print(p[1].__class__.__name__)
resolve_nodes(p[1], 0)
def p_statement(p):
'''statement : variable_declaration_statement
| variable_reassignment_statement'''
p[0] = p[1]
# def p_function(p):
# '''function : FUNCTION IDENTIFIER PARENTHESIS_LEFT function_parameters PARENTHESIS_RIGHT COLON return_type BRACKET_LEFT statements BRACKET_RIGHT'''
# pass
def p_variable_declaration_statement(p):
'''variable_declaration_statement : variable_declarator IDENTIFIER COLON variable_type ASSIGN expression SEMICOLON'''
p[0] = AstNodeVariableDeclarationStatement(p[1], p[2], p[4], p[6])
def p_variable_reassignment_statement(p):
'''variable_reassignment_statement : IDENTIFIER ASSIGN expression SEMICOLON'''
pass
def p_variable_declarator(p):
'''variable_declarator : CONST
| VAR'''
p[0] = p[1]
def p_variable_type(p):
'''variable_type : TYPE_INTEGER
| TYPE_STRING'''
p[0] = p[1]
def p_identifier(p):
'''identifier : IDENTIFIER'''
p[0] = AstNodeExpression(AstNodeExpressionType.IDENTIFIER, p[1])
def p_number(p):
'''number : NUMBER'''
p[0] = AstNodeExpression(AstNodeExpressionType.NUMBER, p[1])
def p_string(p):
'''string : STRING'''
p[0] = AstNodeExpression(AstNodeExpressionType.STRING, p[1])
def p_expression(p):
'''expression : identifier
| number
| string
| expression OPERATOR_PLUS expression
| expression OPERATOR_MINUS expression
| expression OPERATOR_MULTIPLY expression
| expression OPERATOR_DIVIDE expression'''
if len(p) == 4:
p[0] = AstNodeOperatorExpression(p[2], p[1], p[3])
else:
p[0] = p[1]
def p_error(p):
print("Syntax error in input!", p)
parser = yacc.yacc(debug=True, debuglog=log)
result = parser.parse(input)
# print(result)
|