-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpEllyGrammar.py
executable file
·394 lines (310 loc) · 10 KB
/
dumpEllyGrammar.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/python3
# PyElly - rule-based tool for analyzing natural language (Python v3.8)
#
# dumpEllyGrammar.py : 05jul2020 CPM
# ------------------------------------------------------------------------------
# Copyright (c) 2019, Clinton Prentiss Mah
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
"""
methods to dump a grammar table
"""
import ellyChar
import cognitiveDefiner
import generativeDefiner
import sys
def dumpAll ( symbols , table , level ):
"""
show the rules in an Elly grammar table
arguments:
symbols - symbol table
table - grammar table
level - degree of detail
"""
noe = 0
print ( len(symbols.ntindx) , 'syntactic categories' )
# print ('subprocedure index=' , table.pndx )
dumpInitializations(table.initzn)
dumpMatrix(symbols,table.mat)
dumpFeatures(symbols)
noe += dumpSubprocedures(table.pndx,level > 0)
noe += dumpSplits(symbols,table.splits,level > 2,table.pndx)
noe += dumpExtensions(symbols,table.extens,level > 2,table.pndx)
noe += dumpDictionary(symbols,table.dctn,level > 1,table.pndx)
print ( "DONE" )
if noe > 0:
print ( '' )
print ( '**' )
print ( '**' , noe , 'call(s) to unknown subprocedure(s)!' )
print ( '' )
def dumpInitializations ( inits ):
"""
show global variable initializations
arguments:
inits - DerivabilityMatrix object
"""
print ()
print ( "Global Variable Initializations" )
print ( "-------------------------------" )
for x in inits:
print ( '{:<12.12s} = '.format(x[0]) , x[1] )
def dumpMatrix ( stb , matrix ):
"""
show derivability matrix
arguments:
stb - symbol table
matrix - DerivabilityMatrix object
"""
print ()
print ( "Derivability Matrix for Syntactic Structure Types" )
print ( "-------------------------------------------------" )
mat = matrix.dm
ntno = len(stb.ntindx)
for i in range(ntno):
print ( '{:2} '.format(i) , end=' ' )
print ( '{:<6.6s} : '.format(stb.ntname[i]) , end=' ' )
bmr = mat[i]
for j in range(ntno):
if i != j and bmr.test(j):
print ( '{} '.format(stb.ntname[j]) , end=' ' )
print ()
print ( " " + bmr.hexadecimal() + " " + str(bmr.count()) + ' bytes' )
def dumpCategories ( stb ):
"""
show syntactic categories
arguments:
stb - symbol table
"""
print ( "Syntactic Categories" )
print ( "--------------------" )
ntno = len(stb.ntname)
for i in range(ntno):
print ( '{:2}'.format(i) , stb.ntname[i] )
def dumpFeatures ( stb ):
"""
show feature sets
arguments:
stb - symbol table
"""
print ()
print ( "Feature Sets" )
print ( "------------" )
lb = [ 'Syntactic' , 'Semantic' ]
for fs in [ stb.sxindx , stb.smindx ]:
lbl = lb.pop(0)
print ( '--' , lbl )
fids = list(fs) # must do in Python 3
nols = len(fids)
for i in range(nols):
idn = fids[i]
fl = fs[idn]
print ( '[{0:2}] {1} '.format(i,idn) , end=' ' )
sls = [ ]
for f in fl:
sls.append([ f , fs[idn][f] ])
sls.sort(key=lambda x: x[1])
for sr in sls:
print ( '{0}={1}'.format(sr[0],sr[1]) , end=' ' )
print ()
def dumpSubprocedures ( index , full , pxl=None ):
"""
show standalone procedures
arguments:
index - procedure index
full - flag for full dump
pxl - semantic subprocedure index
returns:
number of calls to unknown subprocedures
"""
print ()
print ( "Semantic Subprocedures" )
print ( "----------------------" )
noe = 0
lps = index.keys()
for p in lps:
print ()
print ( p )
if index[p] == None:
print ( '** undefined!' )
elif full:
noe += generativeDefiner.showCode(index[p].logic,pxl)
return noe
def showMask ( msk ):
"""
produce hexadecimal representation of feature mask
arguments:
msk - bit string for mask
returns:
hexadecimal string
"""
ln = len(msk)//2 # must do in Python 3
hi = msk[ln:]
lo = msk[:ln]
sb = [ ]
sb.append('+')
for i in range(ln):
sb.append('{:02x}'.format(lo[i]))
sb.append('-')
for i in range(ln):
sb.append('{:02x}'.format(hi[i]))
return ''.join(sb)
def showProcedures ( r , pxl=None ):
"""
show semantic procedures for rule
arguments:
r - rule
pxl - semantic subprocedure index
returns:
number of calls to undefined subprocedures
"""
noe = 0
print ( ' ** cognitive' )
if r.cogs != None:
cognitiveDefiner.showCode(r.cogs.logic)
print ( ' ** generative' )
if r.gens != None:
noe += generativeDefiner.showCode(r.gens.logic,pxl)
print ()
return noe
def dumpSplits ( stb , splits , full , pxl=None ):
"""
show 2-branch rules
arguments:
stb - symbol table
splits - listing of 2-branch rules
full - flag for full dump
pxl - procedure list for optional checking
returns:
number of calls to undefined subprocedures
"""
print ()
print ( "Splitting Rules" )
print ( "---------------" )
noe = 0
no = 0
for i in range(len(splits)):
rv = splits[i]
k = len(rv)
if k == 0:
continue
ty = stb.ntname[i]
for j in range(k):
r = rv[j]
print ( '(' + str(r.seqn) + ')' , end=' ' )
print ( stb.ntname[r.styp] , end=' ' )
sets = r.sfet.hexadecimal(False)
rsts = r.sftr.hexadecimal(False)
print ( '[{0}-{1}]->'.format(sets,rsts) , end=' ' )
print ( ty + ' ' + showMask(r.ltfet) + ' ' , end=' ' )
print ( stb.ntname[r.rtyp] + ' ' + showMask(r.rtfet) )
if full: noe += showProcedures(r,pxl)
no += k
print ( no , "2-branch grammar rules" )
return noe
def dumpExtensions ( stb, extens , full , pxl=None ):
"""
show 1-branch rules
arguments:
stb - symbol table
extens - listing of 1-branch rules
full - flag for full dump
pxl - procedure list for optional checking
returns:
number of calls to undefined subprocedures
"""
print ()
print ( "Extending Rules" )
print ( "---------------" )
noe = 0
no = 0
for i in range(len(extens)):
rv = extens[i]
k = len(rv)
if k == 0:
continue
ty = stb.ntname[i]
for r in rv:
print ( '(' + str(r.seqn) + ')' , end=' ' )
print ( stb.ntname[r.styp] , end=' ' )
sets = r.sfet.hexadecimal(False)
rsts = r.sftr.hexadecimal(False)
print ( '[{0}-{1}]->'.format(sets,rsts) , end=' ' )
print ( ty + ' ' + showMask(r.utfet) )
if full: noe += showProcedures(r,pxl)
no += k
print ( no , "1-branch grammar rules" )
return noe
def dumpDictionary ( stb, dctn , full , pxl=None ):
"""
dump internal dictionary
arguments:
stb - symbol table
dctn - dictionary
full - flag for full dump
pxl - procedure list for optional checking
returns:
number of calls to undefined subprocedures
"""
print ()
print ( "Internal Dictionary" )
print ( "-------------------" )
noe = 0
no = 0
ws = dctn.keys()
# print ( ws )
for w in ws:
dv = dctn[w]
k = len(dv)
if k == 0:
continue
for dr in dv:
print ( stb.ntname[dr.styp] , end=' ' )
print ( '[{}]->'.format(dr.sfet.hexadecimal(False)) , end=' ' )
if w == ellyChar.RS:
print ( '<RS>' )
else:
us = '"' + w + '"'
print ( us )
if full: noe += showProcedures(dr,pxl)
no += k
print ( len(dctn) , 'unique tokens in' , no , "dictionary rules" )
return noe
#
# unit test
#
if __name__ == '__main__':
import ellyException
import ellyDefinition
import ellyPickle
nam = sys.argv[1] if len(sys.argv) > 1 else 'test'
lvl = sys.argv[2] if len(sys.argv) > 2 else '3'
ver = sys.argv[3] if len(sys.argv) > 3 else ''
rul = ellyPickle.load(nam + '.rules.elly.bin')
if rul == None:
try:
rul = ellyDefinition.Grammar(nam,True,ver)
except ellyException.TableFailure:
print ( 'grammar rules failed to compile' , file=sys.stderr )
sys.exit(1)
dumpAll(rul.stb,rul.gtb,int(lvl))