Skip to content

Commit 8bc401a

Browse files
authored
Clean implementation of Parser/pgen and fix some style issues (GH-12156)
1 parent 97c288d commit 8bc401a

File tree

4 files changed

+14
-28
lines changed

4 files changed

+14
-28
lines changed

Parser/pgen/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .pgen import ParserGenerator
44

5+
56
def main():
67
parser = argparse.ArgumentParser(description="Parser generator main program.")
78
parser.add_argument(

Parser/pgen/grammar.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
import collections
22

3-
class Grammar:
4-
"""Pgen parsing tables conversion class.
5-
6-
Once initialized, this class supplies the grammar tables for the
7-
parsing engine implemented by parse.py. The parsing engine
8-
accesses the instance variables directly. The class here does not
9-
provide initialization of the tables; several subclasses exist to
10-
do this (see the conv and pgen modules).
113

12-
The load() method reads the tables from a pickle file, which is
13-
much faster than the other ways offered by subclasses. The pickle
14-
file is written by calling dump() (after loading the grammar
15-
tables using a subclass). The report() method prints a readable
16-
representation of the tables to stdout, for debugging.
4+
class Grammar:
5+
"""Pgen parsing tables class.
176
187
The instance variables are as follows:
198
@@ -36,8 +25,7 @@ class Grammar:
3625
dfas -- a dict mapping symbol numbers to (DFA, first)
3726
pairs, where DFA is an item from the states list
3827
above, and first is a set of tokens that can
39-
begin this grammar rule (represented by a dict
40-
whose values are always 1).
28+
begin this grammar rule.
4129
4230
labels -- a list of (x, y) pairs where x is either a token
4331
number or a symbol number, and y is either None
@@ -92,14 +80,12 @@ def print_labels(self, writer):
9280
"static label labels[{n_labels}] = {{\n".format(n_labels=len(self.labels))
9381
)
9482
for label, name in self.labels:
95-
if name is None:
96-
writer(" {{{label}, 0}},\n".format(label=label))
97-
else:
98-
writer(
99-
' {{{label}, "{label_name}"}},\n'.format(
100-
label=label, label_name=name
101-
)
83+
label_name = '"{}"'.format(name) if name is not None else 0
84+
writer(
85+
' {{{label}, {label_name}}},\n'.format(
86+
label=label, label_name=label_name
10287
)
88+
)
10389
writer("};\n")
10490

10591
def print_dfas(self, writer):
@@ -114,10 +100,9 @@ def print_dfas(self, writer):
114100
+ "0, {n_states}, states_{dfa_index},\n".format(
115101
n_states=len(dfa), dfa_index=dfaindex
116102
)
103+
+ ' "'
117104
)
118-
writer(' "')
119105

120-
k = [name for label, name in self.labels if label in first_sets]
121106
bitset = bytearray((len(self.labels) >> 3) + 1)
122107
for token in first_sets:
123108
bitset[token >> 3] |= 1 << (token & 7)

Parser/pgen/pgen.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from . import grammar, token
55

6+
67
class ParserGenerator(object):
78

89
def __init__(self, grammar_file, token_file, stream=None, verbose=False):
@@ -183,11 +184,8 @@ def parse(self):
183184
dfa = self.make_dfa(a, z)
184185
if self.verbose:
185186
self.dump_dfa(name, dfa)
186-
oldlen = len(dfa)
187187
self.simplify_dfa(dfa)
188-
newlen = len(dfa)
189188
dfas[name] = dfa
190-
#print name, oldlen, newlen
191189
if startsymbol is None:
192190
startsymbol = name
193191
return dfas, startsymbol
@@ -355,7 +353,7 @@ def raise_error(self, msg, *args):
355353
if args:
356354
try:
357355
msg = msg % args
358-
except:
356+
except Exception:
359357
msg = " ".join([msg] + list(map(str, args)))
360358
raise SyntaxError(msg, (self.filename, self.end[0],
361359
self.end[1], self.line))

Parser/pgen/token.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import itertools
22

3+
34
def generate_tokens(tokens):
45
numbers = itertools.count(0)
56
for line in tokens:
@@ -16,6 +17,7 @@ def generate_tokens(tokens):
1617
yield ('N_TOKENS', next(numbers))
1718
yield ('NT_OFFSET', 256)
1819

20+
1921
def generate_opmap(tokens):
2022
for line in tokens:
2123
line = line.strip()

0 commit comments

Comments
 (0)