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