1
1
import collections
2
2
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).
11
3
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.
17
6
18
7
The instance variables are as follows:
19
8
@@ -36,8 +25,7 @@ class Grammar:
36
25
dfas -- a dict mapping symbol numbers to (DFA, first)
37
26
pairs, where DFA is an item from the states list
38
27
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.
41
29
42
30
labels -- a list of (x, y) pairs where x is either a token
43
31
number or a symbol number, and y is either None
@@ -92,14 +80,12 @@ def print_labels(self, writer):
92
80
"static label labels[{n_labels}] = {{\n " .format (n_labels = len (self .labels ))
93
81
)
94
82
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
102
87
)
88
+ )
103
89
writer ("};\n " )
104
90
105
91
def print_dfas (self , writer ):
@@ -114,10 +100,9 @@ def print_dfas(self, writer):
114
100
+ "0, {n_states}, states_{dfa_index},\n " .format (
115
101
n_states = len (dfa ), dfa_index = dfaindex
116
102
)
103
+ + ' "'
117
104
)
118
- writer (' "' )
119
105
120
- k = [name for label , name in self .labels if label in first_sets ]
121
106
bitset = bytearray ((len (self .labels ) >> 3 ) + 1 )
122
107
for token in first_sets :
123
108
bitset [token >> 3 ] |= 1 << (token & 7 )
0 commit comments