Mercurial > lcfOS
comparison python/pyyacc.py @ 191:6b2bec5653f1
Added assembler testset
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 15:28:07 +0200 |
parents | 46d62dadd61b |
children | 6cd6260789a1 |
comparison
equal
deleted
inserted
replaced
190:65dda7e7e8bd | 191:6b2bec5653f1 |
---|---|
1 """ | 1 """ |
2 Parser generator script | 2 Parser generator script |
3 """ | 3 """ |
4 | |
5 from ppci import Token | |
4 | 6 |
5 EPS = 'EPS' | 7 EPS = 'EPS' |
6 EOF = 'EOF' | 8 EOF = 'EOF' |
7 SHIFT = 1 | 9 SHIFT = 1 |
8 REDUCE = 2 | 10 REDUCE = 2 |
270 def __init__(self, action_table, goto_table): | 272 def __init__(self, action_table, goto_table): |
271 self.action_table = action_table | 273 self.action_table = action_table |
272 self.goto_table = goto_table | 274 self.goto_table = goto_table |
273 | 275 |
274 def parse(self, toks): | 276 def parse(self, toks): |
277 """ Parse an iterable with tokens """ | |
278 assert hasattr(toks, '__iter__'), '{0} not iter type'.format(type(toks)) | |
275 stack = [0] | 279 stack = [0] |
276 look_ahead = toks.pop(0) | 280 look_ahead = toks.__next__() |
281 assert type(look_ahead) is Token | |
277 while True: | 282 while True: |
278 state = stack[-1] # top of stack | 283 state = stack[-1] # top of stack |
279 key = (state, look_ahead) | 284 key = (state, look_ahead.typ) |
280 if not key in self.action_table: | 285 if not key in self.action_table: |
281 print(key) | 286 raise Exception('Error parsing at character {0}'.format(look_ahead)) |
282 raise Exception('Error parsing') | 287 action, param = self.action_table[key] |
283 action, param = self.action_table[(state, look_ahead)] | |
284 if action == REDUCE: | 288 if action == REDUCE: |
285 for s in param.symbols: | 289 for s in param.symbols: |
286 stack.pop() | 290 stack.pop() |
287 stack.pop() | 291 stack.pop() |
288 state = stack[-1] | 292 state = stack[-1] |
289 stack.append(param.name) | 293 stack.append(param.name) |
290 stack.append(self.goto_table[(state, param.name)]) | 294 stack.append(self.goto_table[(state, param.name)]) |
291 elif action == SHIFT: | 295 elif action == SHIFT: |
292 stack.append(look_ahead) | 296 stack.append(look_ahead.typ) |
293 stack.append(param) | 297 stack.append(param) |
294 look_ahead = toks.pop(0) | 298 try: |
299 look_ahead = toks.__next__() | |
300 except StopIteration: | |
301 look_ahead = Token(EOF, EOF, 0) | |
302 assert type(look_ahead) is Token | |
295 elif action == ACCEPT: | 303 elif action == ACCEPT: |
296 break | 304 break |
297 | 305 |
298 def testSimpleGrammar(): | 306 def testSimpleGrammar(): |
299 # 1. define a simple grammar: | 307 # 1. define a simple grammar: |