-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
276 lines (217 loc) · 6.46 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
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
# fragment start *
"""
A recursive descent parser for Baresbone language
"""
from Lexer import Lexer
from Symbols import *
from Node import Node
from Token import Token
from Character import Character
class ParserError(Exception):
pass
def dq(s): return '"%s"' % s
token = None
verbose = False
indent = 0
numberOperator = ["+", "-", "/", "*"]
# -------------------------------------------------------------------
#
# -------------------------------------------------------------------
def get_token():
global token
if verbose:
if token:
# print the current token, before we get the next one
# print (" "*40 ) + token.show()
print((" " * indent) + " (" + token.show(align=False) + ")")
token = lexer.get()
# -------------------------------------------------------------------
# push and pop
# -------------------------------------------------------------------
def push(s):
global indent
indent += 1
if verbose:
print((" " * indent) + " " + s)
def pop(s):
global indent
if verbose:
# print((" "*indent) + " " + s + ".end")
pass
indent -= 1
# -------------------------------------------------------------------
# decorator track0
# -------------------------------------------------------------------
def track0(func):
def newfunc():
push(func.__name__)
func()
pop(func.__name__)
return newfunc
# -------------------------------------------------------------------
# decorator track
# -------------------------------------------------------------------
def track(func):
def newfunc(node):
push(func.__name__)
func(node)
pop(func.__name__)
return newfunc
# -------------------------------------------------------------------
# Error
# -------------------------------------------------------------------
def error(msg):
token.abort(msg)
# -------------------------------------------------------------------
# found
# -------------------------------------------------------------------
def found(arg_token_type):
if token.type == arg_token_type:
return True
return False
# -------------------------------------------------------------------
# consume
# -------------------------------------------------------------------
def consume(arg_token_type):
"""
Consume a token of a given type and get the next token.
If the current token is NOT of the expected type, then
raise an error.
"""
if token.type == arg_token_type:
get_token()
else:
error("Expecting to find "
+ dq(arg_token_type)
+ " but found "
+ token.show(align=False)
)
# -------------------------------------------------------------------
# parse
# -------------------------------------------------------------------
def parse(source_text, **kwargs):
global lexer, verbose
verbose = kwargs.get("verbose", False)
# create a Lexer object & pass it the sourceText
lexer = Lexer(source_text)
get_token()
program()
if verbose:
print("~" * 80)
print("Successful parse!")
print("~" * 80)
return ast
# --------------------------------------------------------
# program
# --------------------------------------------------------
@track0
def program():
"""
program = statement {statement} EOF.
"""
global ast
node = Node()
statement(node)
# Parse for statements until end of file
while not found(EOF):
statement(node)
consume(EOF)
ast = node
# --------------------------------------------------------
# statement
# --------------------------------------------------------
@track
def statement(node):
if found("clear"):
clear_statement(node)
elif found("incr"):
incr_statement(node)
elif found("decr"):
decr_statement(node)
elif found("while"):
while_statement(node)
else:
# Unexpected
error("Didn't reconize " + token.show())
# --------------------------------------------------------
# clear statement
# --------------------------------------------------------
@track
def clear_statement(node):
"""
Syntax = clear + variable + ;
"""
statement_node = Node(token)
consume("clear")
node.add_node(statement_node)
# Parse next variable
variable(statement_node)
consume(";")
# --------------------------------------------------------
# increase statement
# --------------------------------------------------------
@track
def incr_statement(node):
"""
Syntax = incr + variable + ;
"""
statement_node = Node(token)
consume("incr")
node.add_node(statement_node)
#Parse next variable
variable(statement_node)
consume(";")
# --------------------------------------------------------
# decrease statement
# --------------------------------------------------------
@track
def decr_statement(node):
"""
Syntax = decr + variable + ;
"""
statement_node = Node(token)
consume("decr")
node.add_node(statement_node)
# Parse next variable
variable(statement_node)
consume(";")
# --------------------------------------------------------
# while loop statement
# --------------------------------------------------------
@track
def while_statement(node):
"""
Syntax = while + variable + not + 0 + do + ;
+ list of statement
+ end + ;
"""
statement_node = Node(token)
consume("while")
node.add_node(statement_node)
# Parse next variable
variable(statement_node)
# Consume keywords
consume("not")
consume("Zero")
consume("do")
consume(";")
# Until find end consume all statement
while not found("end"):
# If there is no end keyword then raise an error
if found(EOF):
error("Expecting \"end\"")
# Add to children
statement(statement_node)
# Consume end keyword
consume("end")
consume(";")
# --------------------------------------------------------
# variable
# --------------------------------------------------------
def variable(node):
# Check if first character is degits
if token.cargo[0] not in string.ascii_letters:
error("Variable can not start with degits")
token.type = "variable"
node.add(token)
get_token()