-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathabnf2bnf.py
167 lines (143 loc) · 4.51 KB
/
abnf2bnf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import parglare
from tools import timeit
from parglare_adapter import to_parglare_grammar
from grammar_desugaring import (
StringTerminal, RangeTerminal, Name,
Sequence, Alternative, Optional, RangeRepetition, ExactRepetition,
Grammar,
)
abnf_grammar, start_symbol = to_parglare_grammar(
{
'start': [['_?', 'rules', '_?']],
'rules': [['rules', '_', 'rule'], ['rule']],
'rule': [['identifier', '_?', '=', '_?', 'definition0']],
'definition0': [
['definition0', '_?', '/', '_?', 'definition1'],
['definition1'],
],
'definition1': [
['definition1', '_', 'definition2'],
['definition2'],
],
'definition2': [
['dec', 'definition3'],
['dec?', '*', 'dec?', 'definition3'],
['definition3'],
],
'definition3': [
['(', '_?', 'definition0', '_?', ')'],
['[', '_?', 'definition0', '_?', ']'],
['%x', 'hex', '-', 'hex'],
['%d', 'dec', '-', 'dec'],
['string'],
['identifier'],
],
'hex_string': [['hex_string', 'dot', 'hex'], ['hex']],
'dec_string': [['dec_string', 'dot', 'dec'], ['dec']],
'string': [
['%x', 'hex_string'],
['%d', 'dec_string'],
['"', 'quoted_string', '"'],
['"', '"'],
],
'_?': [['_'], []],
'dec?': [['dec'], []],
},
{
'*': ('string', '*'),
'-': ('string', '-'),
'_': ('regexp', r'([ \r\n\t]|;[^\r\n]*\r\n)+'),
'=': ('string', '='),
'"': ('string', '"'),
'(': ('string', '('),
')': ('string', ')'),
'/': ('string', '/'),
'[': ('string', '['),
']': ('string', ']'),
'%d': ('string', r'%d'),
'dec': ('regexp', r'0|([1-9][0-9]*)'),
'dot': ('string', '.'),
'hex': ('regexp', r'[0-9A-Fa-f]+'),
'identifier': ('regexp', r'[a-zA-Z][0-9a-zA-Z\-_]*'),
'quoted_string': ('regexp', r'[^"]+'),
'%x': ('string', r'%x'),
},
'start',
)
def default(v, default):
if v is None:
return default
else:
return v
def take(constructor, indexes):
def f(_, c):
return constructor(*(c[i] for i in indexes))
return f
def take_raw(i):
def f(_, c):
return c[i]
return f
concat_0_2_actions = [
lambda _, c: c[0] + [c[2]],
lambda _, c: c,
]
with timeit('making grammar parser'):
abnf_parser = parglare.GLRParser(
abnf_grammar,
ws='',
actions={
'dec?': parglare.actions.optional,
'hex?': parglare.actions.optional,
'hex': lambda _, v: int(v, 16),
'dec': lambda _, v: int(v, 10),
'hex_string': concat_0_2_actions,
'dec_string': concat_0_2_actions,
'string': [
lambda _, c: ''.join(map(chr, c[1])),
lambda _, c: ''.join(map(chr, c[1])),
take_raw(1),
lambda _, c: '',
],
'definition0': [
lambda _, c: Alternative((c[0], c[4])),
take_raw(0),
],
'definition1': [
lambda _, c: Sequence((c[0], c[2])),
take_raw(0),
],
'definition2': [
lambda _, c: ExactRepetition(c[0], c[1]),
lambda _, c: RangeRepetition(c[3], c[0], c[2]),
take_raw(0),
],
'definition3': [
take_raw(2),
take(Optional, [2]),
take(RangeTerminal, [1, 3]),
take(RangeTerminal, [1, 3]),
take(StringTerminal, [0]),
take(Name, [0]),
],
'rule': lambda _, c: (c[0], c[4]),
'rules': concat_0_2_actions,
'start': lambda _, c: Grammar(dict(c[1])),
start_symbol: take_raw(0),
},
)
def abnf2bnf(grammar, start):
with timeit('parsing the grammar'):
trees = abnf_parser.parse(grammar)
assert len(trees) == 1
tree = trees[0]
with timeit('converting into BNF'):
productions, terminals = tree.to_productions_dict()
return productions, terminals, start
if __name__ == '__main__':
import json
import sys
if len(sys.argv) != 2:
print('usage: {} start-symbol-name'.format(sys.argv[0]), file=sys.stderr)
exit(1)
start = sys.argv[1]
json.dump(abnf2bnf(sys.stdin.read(), start), sys.stdout)