All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 # Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
5 from . import grammar, token, tokenize
8 class PgenGrammar(grammar.Grammar):
12 class ParserGenerator(object):
13 def __init__(self, filename, stream=None):
16 stream = open(filename)
17 close_stream = stream.close
18 self.filename = filename
20 self.generator = tokenize.generate_tokens(stream.readline)
21 self.gettoken() # Initialize lookahead
22 self.dfas, self.startsymbol = self.parse()
23 if close_stream is not None:
25 self.first = {} # map from symbol name to set of tokens
28 def make_grammar(self):
30 names = list(self.dfas.keys())
32 names.remove(self.startsymbol)
33 names.insert(0, self.startsymbol)
35 i = 256 + len(c.symbol2number)
36 c.symbol2number[name] = i
37 c.number2symbol[i] = name
43 for label, next in sorted(state.arcs.items()):
44 arcs.append((self.make_label(c, label), dfa.index(next)))
46 arcs.append((0, dfa.index(state)))
48 c.states.append(states)
49 c.dfas[c.symbol2number[name]] = (states, self.make_first(c, name))
50 c.start = c.symbol2number[self.startsymbol]
53 def make_first(self, c, name):
54 rawfirst = self.first[name]
56 for label in sorted(rawfirst):
57 ilabel = self.make_label(c, label)
58 ##assert ilabel not in first # XXX failed on <> ... !=
62 def make_label(self, c, label):
63 # XXX Maybe this should be a method on a subclass of converter?
64 ilabel = len(c.labels)
65 if label[0].isalpha():
66 # Either a symbol name or a named token
67 if label in c.symbol2number:
68 # A symbol name (a non-terminal)
69 if label in c.symbol2label:
70 return c.symbol2label[label]
72 c.labels.append((c.symbol2number[label], None))
73 c.symbol2label[label] = ilabel
76 # A named token (NAME, NUMBER, STRING)
77 itoken = getattr(token, label, None)
78 assert isinstance(itoken, int), label
79 assert itoken in token.tok_name, label
80 if itoken in c.tokens:
81 return c.tokens[itoken]
83 c.labels.append((itoken, None))
84 c.tokens[itoken] = ilabel
87 # Either a keyword or an operator
88 assert label[0] in ('"', "'"), label
90 if value[0].isalpha():
92 if value in c.keywords:
93 return c.keywords[value]
95 c.labels.append((token.NAME, value))
96 c.keywords[value] = ilabel
99 # An operator (any non-numeric token)
100 itoken = grammar.opmap[value] # Fails if unknown token
101 if itoken in c.tokens:
102 return c.tokens[itoken]
104 c.labels.append((itoken, None))
105 c.tokens[itoken] = ilabel
108 def addfirstsets(self):
109 names = list(self.dfas.keys())
112 if name not in self.first:
114 # print name, self.first[name].keys()
116 def calcfirst(self, name):
117 dfa = self.dfas[name]
118 self.first[name] = None # dummy to detect left recursion
122 for label, next in state.arcs.items():
123 if label in self.dfas:
124 if label in self.first:
125 fset = self.first[label]
127 raise ValueError("recursion for rule %r" % name)
129 self.calcfirst(label)
130 fset = self.first[label]
131 totalset.update(fset)
132 overlapcheck[label] = fset
135 overlapcheck[label] = {label: 1}
137 for label, itsfirst in overlapcheck.items():
138 for symbol in itsfirst:
139 if symbol in inverse:
141 "rule %s is ambiguous; %s is in the"
142 " first sets of %s as well as %s"
143 % (name, symbol, label, inverse[symbol])
145 inverse[symbol] = label
146 self.first[name] = totalset
151 # MSTART: (NEWLINE | RULE)* ENDMARKER
152 while self.type != token.ENDMARKER:
153 while self.type == token.NEWLINE:
155 # RULE: NAME ':' RHS NEWLINE
156 name = self.expect(token.NAME)
157 self.expect(token.OP, ":")
158 a, z = self.parse_rhs()
159 self.expect(token.NEWLINE)
160 # self.dump_nfa(name, a, z)
161 dfa = self.make_dfa(a, z)
162 # self.dump_dfa(name, dfa)
164 self.simplify_dfa(dfa)
167 # print name, oldlen, newlen
168 if startsymbol is None:
170 return dfas, startsymbol
172 def make_dfa(self, start, finish):
173 # To turn an NFA into a DFA, we define the states of the DFA
174 # to correspond to *sets* of states of the NFA. Then do some
175 # state reduction. Let's represent sets as dicts with 1 for
177 assert isinstance(start, NFAState)
178 assert isinstance(finish, NFAState)
182 addclosure(state, base)
185 def addclosure(state, base):
186 assert isinstance(state, NFAState)
190 for label, next in state.arcs:
192 addclosure(next, base)
194 states = [DFAState(closure(start), finish)]
195 for state in states: # NB states grows while we're iterating
197 for nfastate in state.nfaset:
198 for label, next in nfastate.arcs:
199 if label is not None:
200 addclosure(next, arcs.setdefault(label, {}))
201 for label, nfaset in sorted(arcs.items()):
203 if st.nfaset == nfaset:
206 st = DFAState(nfaset, finish)
208 state.addarc(st, label)
209 return states # List of DFAState instances; first one is start
211 def dump_nfa(self, name, start, finish):
212 print("Dump of NFA for", name)
214 for i, state in enumerate(todo):
215 print(" State", i, state is finish and "(final)" or "")
216 for label, next in state.arcs:
225 print(" %s -> %d" % (label, j))
227 def dump_dfa(self, name, dfa):
228 print("Dump of DFA for", name)
229 for i, state in enumerate(dfa):
230 print(" State", i, state.isfinal and "(final)" or "")
231 for label, next in sorted(state.arcs.items()):
232 print(" %s -> %d" % (label, dfa.index(next)))
234 def simplify_dfa(self, dfa):
235 # This is not theoretically optimal, but works well enough.
236 # Algorithm: repeatedly look for two states that have the same
237 # set of arcs (same labels pointing to the same nodes) and
238 # unify them, until things stop changing.
240 # dfa is a list of DFAState instances
244 for i, state_i in enumerate(dfa):
245 for j in range(i + 1, len(dfa)):
247 if state_i == state_j:
248 # print " unify", i, j
251 state.unifystate(state_j, state_i)
256 # RHS: ALT ('|' ALT)*
257 a, z = self.parse_alt()
258 if self.value != "|":
265 while self.value == "|":
267 a, z = self.parse_alt()
274 a, b = self.parse_item()
275 while self.value in ("(", "[") or self.type in (token.NAME, token.STRING):
276 c, d = self.parse_item()
281 def parse_item(self):
282 # ITEM: '[' RHS ']' | ATOM ['+' | '*']
283 if self.value == "[":
285 a, z = self.parse_rhs()
286 self.expect(token.OP, "]")
290 a, z = self.parse_atom()
292 if value not in ("+", "*"):
301 def parse_atom(self):
302 # ATOM: '(' RHS ')' | NAME | STRING
303 if self.value == "(":
305 a, z = self.parse_rhs()
306 self.expect(token.OP, ")")
308 elif self.type in (token.NAME, token.STRING):
311 a.addarc(z, self.value)
316 "expected (...) or NAME or STRING, got %s/%s", self.type, self.value
319 def expect(self, type, value=None):
320 if self.type != type or (value is not None and self.value != value):
322 "expected %s/%s, got %s/%s", type, value, self.type, self.value
329 tup = next(self.generator)
330 while tup[0] in (tokenize.COMMENT, tokenize.NL):
331 tup = next(self.generator)
332 self.type, self.value, self.begin, self.end, self.line = tup
333 # print token.tok_name[self.type], repr(self.value)
335 def raise_error(self, msg, *args):
340 msg = " ".join([msg] + list(map(str, args)))
341 raise SyntaxError(msg, (self.filename, self.end[0], self.end[1], self.line))
344 class NFAState(object):
346 self.arcs = [] # list of (label, NFAState) pairs
348 def addarc(self, next, label=None):
349 assert label is None or isinstance(label, str)
350 assert isinstance(next, NFAState)
351 self.arcs.append((label, next))
354 class DFAState(object):
355 def __init__(self, nfaset, final):
356 assert isinstance(nfaset, dict)
357 assert isinstance(next(iter(nfaset)), NFAState)
358 assert isinstance(final, NFAState)
360 self.isfinal = final in nfaset
361 self.arcs = {} # map from label to DFAState
363 def addarc(self, next, label):
364 assert isinstance(label, str)
365 assert label not in self.arcs
366 assert isinstance(next, DFAState)
367 self.arcs[label] = next
369 def unifystate(self, old, new):
370 for label, next in self.arcs.items():
372 self.arcs[label] = new
374 def __eq__(self, other):
375 # Equality test -- ignore the nfaset instance variable
376 assert isinstance(other, DFAState)
377 if self.isfinal != other.isfinal:
379 # Can't just return self.arcs == other.arcs, because that
380 # would invoke this method recursively, with cycles...
381 if len(self.arcs) != len(other.arcs):
383 for label, next in self.arcs.items():
384 if next is not other.arcs.get(label):
388 __hash__ = None # For Py3 compatibility.
391 def generate_grammar(filename="Grammar.txt"):
392 p = ParserGenerator(filename)
393 return p.make_grammar()