Mercurial > lcfOS
annotate python/pyyacc.py @ 191:6b2bec5653f1
Added assembler testset
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 15:28:07 +0200 |
parents | 46d62dadd61b |
children | 6cd6260789a1 |
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 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
31 def add_production(self, name, symbols): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
32 """ Add a production rule to the grammar """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
33 production = Production(name, symbols) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
34 self.productions.append(production) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
35 assert not name in self.terminals, "Cannot redefine terminal" |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
36 if not name in self.nonterminals: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
37 self.nonterminals.append(name) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
38 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
39 def productionsForName(self, name): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
40 """ Retrieve all productions for a non terminal """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
41 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
|
42 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
43 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
44 def Symbols(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
45 """ Get all the symbols defined by this grammar """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
46 return self.nonterminals + self.terminals |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
47 |
185 | 48 @property |
49 def first(self): | |
186 | 50 """ |
51 The first set is a mapping from a grammar symbol to a set of | |
52 set of all terminal symbols that can be the first terminal when | |
53 looking for the grammar symbol | |
54 """ | |
185 | 55 if not self._first: |
56 self._first = self.calcFirstSets() | |
57 return self._first | |
58 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
59 def calcFirstSets(self): |
184 | 60 """ |
61 Calculate first sets for each grammar symbol | |
62 This is a dictionary which maps each grammar symbol | |
63 to a set of terminals that can be encountered first | |
64 when looking for the symbol. | |
65 """ | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
66 first = {} |
184 | 67 for t in self.terminals + [EOF, EPS]: |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
68 first[t] = set([t]) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
69 for nt in self.nonterminals: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
70 first[nt] = set() |
185 | 71 epsset = {EPS} |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
72 while True: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
73 some_change = False |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
74 for p in self.productions: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
75 rhs = set() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
76 for beta in p.symbols: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
77 rhs = rhs | (first[beta] - epsset) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
78 if not EPS in first[beta]: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
79 break |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
80 else: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
81 if EPS in first[beta]: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
82 rhs.add(EPS) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
83 if rhs - first[p.name]: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
84 first[p.name] |= rhs |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
85 some_change = True |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
86 if not some_change: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
87 break |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
88 return first |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
89 |
184 | 90 def closure(self, itemset): |
91 """ Expand itemset by using epsilon moves """ | |
92 worklist = list(itemset) | |
93 def addIt(itm): | |
94 if not itm in itemset: | |
95 itemset.add(itm) | |
96 worklist.append(itm) | |
186 | 97 def first2(itm): |
184 | 98 # When using the first sets, create a copy: |
186 | 99 f = set(self.first[itm.NextNext]) |
184 | 100 if EPS in f: |
101 f.discard(EPS) | |
186 | 102 f.add(itm.look_ahead) |
184 | 103 return f |
185 | 104 # Start of algorithm: |
184 | 105 while worklist: |
106 item = worklist.pop(0) | |
107 if not item.IsShift: | |
108 continue | |
109 if not (item.Next in self.nonterminals): | |
110 continue | |
111 C = item.Next | |
112 for add_p in self.productionsForName(C): | |
186 | 113 for b in first2(item): |
184 | 114 addIt(Item(add_p, 0, b)) |
115 return frozenset(itemset) | |
116 | |
117 def initialItemSet(self): | |
118 """ Calculates the initial item set """ | |
119 iis = set() | |
120 for p in self.productionsForName(self.start_symbol): | |
121 iis.add(Item(p, 0, EOF)) | |
122 return self.closure(iis) | |
123 | |
124 def nextItemSet(self, itemset, symbol): | |
125 """ | |
126 Determines the next itemset for the current set and a symbol | |
127 This is the goto procedure | |
128 """ | |
129 next_set = set() | |
130 for item in itemset: | |
131 if item.can_shift_over(symbol): | |
132 next_set.add(item.shifted()) | |
133 return self.closure(next_set) | |
134 | |
135 def genCanonicalSet(self, iis): | |
185 | 136 states = [] |
184 | 137 worklist = [] |
185 | 138 transitions = {} |
184 | 139 def addSt(s): |
140 if not (s in states): | |
141 worklist.append(s) | |
185 | 142 states.append(s) |
184 | 143 addSt(iis) |
144 while len(worklist) > 0: | |
145 itemset = worklist.pop(0) | |
146 for symbol in self.Symbols: | |
147 nis = self.nextItemSet(itemset, symbol) | |
148 if not nis: | |
149 continue | |
150 addSt(nis) | |
185 | 151 transitions[(states.index(itemset), symbol)] = states.index(nis) |
152 return states, transitions | |
184 | 153 |
154 def genParser(self): | |
155 """ Generates a parser from the grammar """ | |
156 action_table = {} | |
185 | 157 goto_table = {} |
184 | 158 iis = self.initialItemSet() |
159 | |
160 # First generate all item sets by using the nextItemset function: | |
185 | 161 states, transitions = self.genCanonicalSet(iis) |
162 | |
163 def setAction(state, t, action): | |
164 key = (state, t) | |
165 if key in action_table: | |
166 action2 = action_table[key] | |
167 if action != action2: | |
168 raise ParserGenerationException('LR construction conflict') | |
169 else: | |
170 action_table[key] = action | |
184 | 171 |
172 # Fill action table: | |
173 for state in states: | |
185 | 174 # Detect conflicts: |
184 | 175 for item in state: |
176 if item.IsShift and item.Next in self.terminals: | |
185 | 177 # Rule 1, a shift item: |
178 nextstate = transitions[(states.index(state), item.Next)] | |
179 setAction(states.index(state), item.Next, (SHIFT, nextstate)) | |
180 if item.IsReduce: | |
181 if item.production.name == self.start_symbol and item.look_ahead == EOF: | |
182 # Rule 3: accept: | |
183 setAction(states.index(state), item.look_ahead, (ACCEPT, None)) | |
184 | 184 else: |
185 | 185 # Rule 2, reduce item: |
186 setAction(states.index(state), item.look_ahead, (REDUCE, item.production)) | |
187 for nt in self.nonterminals: | |
188 key = (states.index(state), nt) | |
189 if key in transitions: | |
190 goto_table[key] = transitions[key] | |
184 | 191 |
185 | 192 return LRParser(action_table, goto_table) |
193 | |
178 | 194 |
195 class Production: | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
196 """ Production rule for a grammar """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
197 def __init__(self, name, symbols): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
198 self.name = name |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
199 self.symbols = symbols |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
200 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
201 def __repr__(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
202 return '{0} -> {1}'.format(self.name, self.symbols) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
203 |
179 | 204 |
178 | 205 class Item: |
186 | 206 """ |
207 Represents a partially parsed item | |
208 It has a production it is looking for, a position | |
209 in this production called the 'dot' and a look ahead | |
210 symbol that must follow this item. | |
211 """ | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
212 def __init__(self, production, dotpos, look_ahead): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
213 self.production = production |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
214 self.dotpos = dotpos |
184 | 215 assert self.dotpos <= len(self.production.symbols) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
216 self.look_ahead = look_ahead |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
217 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
218 def getdata(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
219 """ Gets the members as a tuple """ |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
220 return (self.production, self.dotpos, self.look_ahead) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
221 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
222 def __eq__(self, other): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
223 if type(other) is type(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
224 return self.getdata() == other.getdata() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
225 return False |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
226 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
227 def __hash__(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
228 return self.getdata().__hash__() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
229 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
230 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
231 def IsReduce(self): |
186 | 232 """ 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
|
233 return self.dotpos == len(self.production.symbols) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
234 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
235 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
236 def IsShift(self): |
186 | 237 """ 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
|
238 return not self.IsReduce |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
239 |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
240 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
241 def Next(self): |
186 | 242 """ Returns the symbol after the dot """ |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
243 return self.production.symbols[self.dotpos] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
244 |
184 | 245 def can_shift_over(self, symbol): |
246 """ Determines if this item can shift over the given symbol """ | |
247 return self.IsShift and self.Next == symbol | |
248 | |
249 def shifted(self): | |
250 """ Creates a new item that is shifted one position """ | |
251 return Item(self.production, self.dotpos + 1, self.look_ahead) | |
252 | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
253 @property |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
254 def NextNext(self): |
186 | 255 """ 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
|
256 if self.dotpos + 1 >= len(self.production.symbols): |
184 | 257 return EPS |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
258 else: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
259 return self.production.symbols[self.dotpos + 1] |
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 __repr__(self): |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
262 prod = self.production |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
263 predot = ' '.join(prod.symbols[0:self.dotpos]) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
264 postdot = ' '.join(prod.symbols[self.dotpos:]) |
186 | 265 name = prod.name |
266 args = (name, predot, postdot, self.look_ahead) | |
185 | 267 return '[{0} -> {1} . {2} -> {3}]'.format(*args) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
268 |
179 | 269 |
270 class LRParser: | |
184 | 271 """ LR parser """ |
185 | 272 def __init__(self, action_table, goto_table): |
184 | 273 self.action_table = action_table |
185 | 274 self.goto_table = goto_table |
184 | 275 |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
276 def parse(self, toks): |
191 | 277 """ Parse an iterable with tokens """ |
278 assert hasattr(toks, '__iter__'), '{0} not iter type'.format(type(toks)) | |
185 | 279 stack = [0] |
191 | 280 look_ahead = toks.__next__() |
281 assert type(look_ahead) is Token | |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
282 while True: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
283 state = stack[-1] # top of stack |
191 | 284 key = (state, look_ahead.typ) |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
285 if not key in self.action_table: |
191 | 286 raise Exception('Error parsing at character {0}'.format(look_ahead)) |
287 action, param = self.action_table[key] | |
184 | 288 if action == REDUCE: |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
289 for s in param.symbols: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
290 stack.pop() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
291 stack.pop() |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
292 state = stack[-1] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
293 stack.append(param.name) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
294 stack.append(self.goto_table[(state, param.name)]) |
184 | 295 elif action == SHIFT: |
191 | 296 stack.append(look_ahead.typ) |
185 | 297 stack.append(param) |
191 | 298 try: |
299 look_ahead = toks.__next__() | |
300 except StopIteration: | |
301 look_ahead = Token(EOF, EOF, 0) | |
302 assert type(look_ahead) is Token | |
184 | 303 elif action == ACCEPT: |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
304 break |
178 | 305 |
184 | 306 def testSimpleGrammar(): |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
307 # 1. define a simple grammar: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
308 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
309 g.add_production('input', ['expression']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
310 g.add_production('expression', ['term']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
311 g.add_production('expression', ['expression', '+', 'term']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
312 g.add_production('term', ['factor']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
313 g.add_production('term', ['term', '*', 'factor']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
314 g.add_production('factor', ['(', 'expression', ')']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
315 g.add_production('factor', ['identifier']) |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
316 g.start_symbol = 'input' |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
317 # 2. define input: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
318 tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF'] |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
319 # 3. build parser: |
184 | 320 p = g.genParser() |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
321 # 4. feed input: |
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
322 p.parse(tokens) |
178 | 323 |
184 | 324 |
181
216da5e46efc
Changed indent to 4 spaces to comply to convention
Windel Bouwman
parents:
180
diff
changeset
|
325 if __name__ == '__main__': |
184 | 326 testSimpleGrammar() |