Mercurial > lcfOS
annotate python/pyyacc.py @ 205:d77cb5962cc5
Added some handcoded arm code generation
author | Windel Bouwman |
---|---|
date | Sun, 23 Jun 2013 18:23:18 +0200 |
parents | 37ac6c016e0f |
children | 494828a7adf1 |
rev | line source |
---|---|
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
1 """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
2 Parser generator script |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
3 """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
4 |
191 | 5 from ppci import Token |
6 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
7 EPS = 'EPS' |
184 | 8 EOF = 'EOF' |
9 SHIFT = 1 | |
10 REDUCE = 2 | |
11 ACCEPT = 3 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
12 |
185 | 13 class ParserGenerationException(Exception): |
186 | 14 """ Raised when something goes wrong during parser generation """ |
185 | 15 pass |
16 | |
17 | |
18 class ParserException(Exception): | |
186 | 19 """ Raised during a failure in the parsing process """ |
185 | 20 pass |
21 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
22 |
178 | 23 class Grammar: |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
24 """ Defines a grammar of a language """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
25 def __init__(self, terminals): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
26 self.terminals = terminals |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
27 self.nonterminals = [] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
28 self.productions = [] |
185 | 29 self._first = None # Cached first set |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
30 |
194 | 31 def add_production(self, name, symbols, f=None): |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
32 """ Add a production rule to the grammar """ |
194 | 33 production = Production(name, symbols, f) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
34 self.productions.append(production) |
192 | 35 if name in self.terminals: |
36 raise ParserGenerationException("Cannot redefine terminal {0}".format(name)) | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
37 if not name in self.nonterminals: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
38 self.nonterminals.append(name) |
194 | 39 self._first = None # Invalidate cached version |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
40 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
41 def productionsForName(self, name): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
42 """ Retrieve all productions for a non terminal """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
43 return [p for p in self.productions if p.name == name] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
44 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
45 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
46 def Symbols(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
47 """ Get all the symbols defined by this grammar """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
48 return self.nonterminals + self.terminals |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
49 |
185 | 50 @property |
51 def first(self): | |
186 | 52 """ |
53 The first set is a mapping from a grammar symbol to a set of | |
54 set of all terminal symbols that can be the first terminal when | |
55 looking for the grammar symbol | |
56 """ | |
185 | 57 if not self._first: |
58 self._first = self.calcFirstSets() | |
59 return self._first | |
60 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
61 def calcFirstSets(self): |
184 | 62 """ |
63 Calculate first sets for each grammar symbol | |
64 This is a dictionary which maps each grammar symbol | |
65 to a set of terminals that can be encountered first | |
66 when looking for the symbol. | |
67 """ | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
68 first = {} |
184 | 69 for t in self.terminals + [EOF, EPS]: |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
70 first[t] = set([t]) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
71 for nt in self.nonterminals: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
72 first[nt] = set() |
185 | 73 epsset = {EPS} |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
74 while True: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
75 some_change = False |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
76 for p in self.productions: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
77 rhs = set() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
78 for beta in p.symbols: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
79 rhs = rhs | (first[beta] - epsset) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
80 if not EPS in first[beta]: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
81 break |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
82 else: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
83 if EPS in first[beta]: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
84 rhs.add(EPS) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
85 if rhs - first[p.name]: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
86 first[p.name] |= rhs |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
87 some_change = True |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
88 if not some_change: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
89 break |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
90 return first |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
91 |
184 | 92 def closure(self, itemset): |
93 """ Expand itemset by using epsilon moves """ | |
94 worklist = list(itemset) | |
95 def addIt(itm): | |
96 if not itm in itemset: | |
97 itemset.add(itm) | |
98 worklist.append(itm) | |
186 | 99 def first2(itm): |
184 | 100 # When using the first sets, create a copy: |
186 | 101 f = set(self.first[itm.NextNext]) |
184 | 102 if EPS in f: |
103 f.discard(EPS) | |
186 | 104 f.add(itm.look_ahead) |
184 | 105 return f |
185 | 106 # Start of algorithm: |
184 | 107 while worklist: |
108 item = worklist.pop(0) | |
109 if not item.IsShift: | |
110 continue | |
111 if not (item.Next in self.nonterminals): | |
112 continue | |
113 C = item.Next | |
114 for add_p in self.productionsForName(C): | |
186 | 115 for b in first2(item): |
184 | 116 addIt(Item(add_p, 0, b)) |
117 return frozenset(itemset) | |
118 | |
119 def initialItemSet(self): | |
120 """ Calculates the initial item set """ | |
121 iis = set() | |
122 for p in self.productionsForName(self.start_symbol): | |
123 iis.add(Item(p, 0, EOF)) | |
124 return self.closure(iis) | |
125 | |
126 def nextItemSet(self, itemset, symbol): | |
127 """ | |
128 Determines the next itemset for the current set and a symbol | |
129 This is the goto procedure | |
130 """ | |
131 next_set = set() | |
132 for item in itemset: | |
133 if item.can_shift_over(symbol): | |
134 next_set.add(item.shifted()) | |
135 return self.closure(next_set) | |
136 | |
137 def genCanonicalSet(self, iis): | |
185 | 138 states = [] |
184 | 139 worklist = [] |
185 | 140 transitions = {} |
184 | 141 def addSt(s): |
142 if not (s in states): | |
143 worklist.append(s) | |
185 | 144 states.append(s) |
184 | 145 addSt(iis) |
146 while len(worklist) > 0: | |
147 itemset = worklist.pop(0) | |
148 for symbol in self.Symbols: | |
149 nis = self.nextItemSet(itemset, symbol) | |
150 if not nis: | |
151 continue | |
152 addSt(nis) | |
185 | 153 transitions[(states.index(itemset), symbol)] = states.index(nis) |
154 return states, transitions | |
192 | 155 |
156 def checkSymbols(self): | |
157 """ Checks no symbols are undefined """ | |
158 for production in self.productions: | |
159 for symbol in production.symbols: | |
194 | 160 if symbol not in self.Symbols + [EPS]: |
192 | 161 raise ParserGenerationException('Symbol {0} undefined'.format(symbol)) |
162 | |
163 | |
184 | 164 def genParser(self): |
165 """ Generates a parser from the grammar """ | |
192 | 166 self.checkSymbols() |
184 | 167 action_table = {} |
185 | 168 goto_table = {} |
184 | 169 iis = self.initialItemSet() |
170 | |
171 # First generate all item sets by using the nextItemset function: | |
185 | 172 states, transitions = self.genCanonicalSet(iis) |
194 | 173 |
174 def action_str(act): | |
175 a, p = act | |
176 if a is SHIFT: | |
177 return 'Shift {0}'.format(0) | |
178 elif a is REDUCE: | |
179 return 'Reduce {0}'.format(p) | |
180 return 'Other' | |
185 | 181 |
182 def setAction(state, t, action): | |
183 key = (state, t) | |
184 if key in action_table: | |
185 action2 = action_table[key] | |
186 if action != action2: | |
194 | 187 if (action2[0] == REDUCE) and (action[0] == SHIFT): |
188 # Automatically resolve and do the shift action! | |
189 # Simple, but almost always what you want!! | |
190 action_table[key] = action | |
191 else: | |
192 if (action2[0] == SHIFT) and (action[0] == REDUCE): | |
193 pass | |
194 else: | |
195 a1 = action_str(action) | |
196 a2 = action_str(action2) | |
197 raise ParserGenerationException('LR construction conflict {0} vs {1}'.format(a1, a2)) | |
185 | 198 else: |
199 action_table[key] = action | |
184 | 200 |
201 # Fill action table: | |
202 for state in states: | |
185 | 203 # Detect conflicts: |
184 | 204 for item in state: |
205 if item.IsShift and item.Next in self.terminals: | |
185 | 206 # Rule 1, a shift item: |
207 nextstate = transitions[(states.index(state), item.Next)] | |
208 setAction(states.index(state), item.Next, (SHIFT, nextstate)) | |
209 if item.IsReduce: | |
210 if item.production.name == self.start_symbol and item.look_ahead == EOF: | |
211 # Rule 3: accept: | |
195 | 212 setAction(states.index(state), item.look_ahead, (ACCEPT, item.production)) |
184 | 213 else: |
185 | 214 # Rule 2, reduce item: |
215 setAction(states.index(state), item.look_ahead, (REDUCE, item.production)) | |
216 for nt in self.nonterminals: | |
217 key = (states.index(state), nt) | |
218 if key in transitions: | |
219 goto_table[key] = transitions[key] | |
184 | 220 |
195 | 221 return LRParser(action_table, goto_table, self.start_symbol) |
185 | 222 |
178 | 223 |
224 class Production: | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
225 """ Production rule for a grammar """ |
194 | 226 def __init__(self, name, symbols, f=None): |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
227 self.name = name |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
228 self.symbols = symbols |
194 | 229 self.f = f |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
230 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
231 def __repr__(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
232 return '{0} -> {1}'.format(self.name, self.symbols) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
233 |
179 | 234 |
178 | 235 class Item: |
186 | 236 """ |
237 Represents a partially parsed item | |
238 It has a production it is looking for, a position | |
239 in this production called the 'dot' and a look ahead | |
240 symbol that must follow this item. | |
241 """ | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
242 def __init__(self, production, dotpos, look_ahead): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
243 self.production = production |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
244 self.dotpos = dotpos |
184 | 245 assert self.dotpos <= len(self.production.symbols) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
246 self.look_ahead = look_ahead |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
247 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
248 def getdata(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
249 """ Gets the members as a tuple """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
250 return (self.production, self.dotpos, self.look_ahead) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
251 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
252 def __eq__(self, other): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
253 if type(other) is type(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
254 return self.getdata() == other.getdata() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
255 return False |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
256 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
257 def __hash__(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
258 return self.getdata().__hash__() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
259 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
260 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
261 def IsReduce(self): |
186 | 262 """ Check if this item has the dot at the end """ |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
263 return self.dotpos == len(self.production.symbols) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
264 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
265 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
266 def IsShift(self): |
186 | 267 """ Check if this item is a shift item, i.e. the dot can proceed """ |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
268 return not self.IsReduce |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
269 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
270 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
271 def Next(self): |
186 | 272 """ Returns the symbol after the dot """ |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
273 return self.production.symbols[self.dotpos] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
274 |
184 | 275 def can_shift_over(self, symbol): |
276 """ Determines if this item can shift over the given symbol """ | |
277 return self.IsShift and self.Next == symbol | |
278 | |
279 def shifted(self): | |
280 """ Creates a new item that is shifted one position """ | |
281 return Item(self.production, self.dotpos + 1, self.look_ahead) | |
282 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
283 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
284 def NextNext(self): |
186 | 285 """ Gets the symbol after the next symbol, or EPS if at the end """ |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
286 if self.dotpos + 1 >= len(self.production.symbols): |
184 | 287 return EPS |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
288 else: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
289 return self.production.symbols[self.dotpos + 1] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
290 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
291 def __repr__(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
292 prod = self.production |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
293 predot = ' '.join(prod.symbols[0:self.dotpos]) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
294 postdot = ' '.join(prod.symbols[self.dotpos:]) |
186 | 295 name = prod.name |
296 args = (name, predot, postdot, self.look_ahead) | |
185 | 297 return '[{0} -> {1} . {2} -> {3}]'.format(*args) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
298 |
179 | 299 |
300 class LRParser: | |
184 | 301 """ LR parser """ |
195 | 302 def __init__(self, action_table, goto_table, start_symbol): |
184 | 303 self.action_table = action_table |
185 | 304 self.goto_table = goto_table |
195 | 305 self.start_symbol = start_symbol |
184 | 306 |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
307 def parse(self, toks): |
191 | 308 """ Parse an iterable with tokens """ |
309 assert hasattr(toks, '__iter__'), '{0} not iter type'.format(type(toks)) | |
185 | 310 stack = [0] |
195 | 311 r_data_stack = [] |
194 | 312 try: |
313 look_ahead = toks.__next__() | |
314 except StopIteration: | |
315 look_ahead = Token(EOF, EOF) | |
191 | 316 assert type(look_ahead) is Token |
195 | 317 # TODO: exit on this condition: |
318 while stack != [0, self.start_symbol, 2222]: | |
319 #print(stack) | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
320 state = stack[-1] # top of stack |
191 | 321 key = (state, look_ahead.typ) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
322 if not key in self.action_table: |
194 | 323 raise ParserException('Error parsing at character {0}'.format(look_ahead)) |
191 | 324 action, param = self.action_table[key] |
184 | 325 if action == REDUCE: |
194 | 326 f_args = [] |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
327 for s in param.symbols: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
328 stack.pop() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
329 stack.pop() |
195 | 330 f_args.append(r_data_stack.pop()) |
331 f_args.reverse() | |
332 r_data = None | |
194 | 333 if param.f: |
195 | 334 r_data = param.f(*f_args) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
335 state = stack[-1] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
336 stack.append(param.name) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
337 stack.append(self.goto_table[(state, param.name)]) |
195 | 338 r_data_stack.append(r_data) |
184 | 339 elif action == SHIFT: |
191 | 340 stack.append(look_ahead.typ) |
185 | 341 stack.append(param) |
195 | 342 r_data_stack.append(look_ahead.val) |
191 | 343 try: |
344 look_ahead = toks.__next__() | |
345 except StopIteration: | |
194 | 346 look_ahead = Token(EOF, EOF) |
191 | 347 assert type(look_ahead) is Token |
184 | 348 elif action == ACCEPT: |
195 | 349 # Pop last rule data off the stack: |
350 f_args = [] | |
351 for s in param.symbols: | |
352 stack.pop() | |
353 stack.pop() | |
354 f_args.append(r_data_stack.pop()) | |
355 f_args.reverse() | |
356 if param.f: | |
357 param.f(*f_args) | |
358 # Break out! | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
359 break |
195 | 360 # At exit, the stack must be 1 long |
361 # TODO: fix that this holds: | |
362 #assert len(stack) == 1, 'stack {0} not totally reduce'.format(stack) | |
178 | 363 |