Mercurial > lcfOS
diff python/pyyacc.py @ 191:6b2bec5653f1
Added assembler testset
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 15:28:07 +0200 |
parents | 46d62dadd61b |
children | 6cd6260789a1 |
line wrap: on
line diff
--- a/python/pyyacc.py Sat May 25 15:15:42 2013 +0200 +++ b/python/pyyacc.py Sun May 26 15:28:07 2013 +0200 @@ -2,6 +2,8 @@ Parser generator script """ +from ppci import Token + EPS = 'EPS' EOF = 'EOF' SHIFT = 1 @@ -272,15 +274,17 @@ self.goto_table = goto_table def parse(self, toks): + """ Parse an iterable with tokens """ + assert hasattr(toks, '__iter__'), '{0} not iter type'.format(type(toks)) stack = [0] - look_ahead = toks.pop(0) + look_ahead = toks.__next__() + assert type(look_ahead) is Token while True: state = stack[-1] # top of stack - key = (state, look_ahead) + key = (state, look_ahead.typ) if not key in self.action_table: - print(key) - raise Exception('Error parsing') - action, param = self.action_table[(state, look_ahead)] + raise Exception('Error parsing at character {0}'.format(look_ahead)) + action, param = self.action_table[key] if action == REDUCE: for s in param.symbols: stack.pop() @@ -289,9 +293,13 @@ stack.append(param.name) stack.append(self.goto_table[(state, param.name)]) elif action == SHIFT: - stack.append(look_ahead) + stack.append(look_ahead.typ) stack.append(param) - look_ahead = toks.pop(0) + try: + look_ahead = toks.__next__() + except StopIteration: + look_ahead = Token(EOF, EOF, 0) + assert type(look_ahead) is Token elif action == ACCEPT: break