annotate python/pyyacc.py @ 318:e84047f29c78

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