-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·77 lines (65 loc) · 2.27 KB
/
main.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
#!/usr/bin/env python
from earley import parse
from grammar import G_org, G_unamb, G_fast
def parseAndPrintResult(G, i, incr):
if parse(G, i, incr): print 'Parsed OK'
else: print 'Syntax error'
# Profiling
import math, time
def run(G, inputs):
# profiling counters
points = 7
counters = [0]*points # Points is the number of instrumentation points
def incr(i,k=1):
counters[i] = counters[i] + k
# table to keep coutners from all runs
inputLengths = [len(i) for i in inputs]
table = dict([(k,[]) for k in inputLengths])
counterTotals = []
timeTotals = []
#
for i in inputs:
# reset counters
counters = [0]*len(counters)
# run parser
starttime = time.time()
# should run it more than once
parseAndPrintResult(G, i, incr)
endtime = time.time()
t = endtime - starttime
# record time and counters
table[len(i)] = [sum(counters),t]+counters
counterTotals.append(sum(counters))
timeTotals.append(t)
print "\nin-size\tsum(cntrs)\ttime\tindividual-counters"
for l in inputLengths:
print l, "\t",
for v in table[l]:
print v, "\t",
print
steps = ((math.log(max(counterTotals)) - math.log(min(counterTotals))) / \
(math.log(max(inputLengths)) - math.log(min(inputLengths))),)
print "\nSteps (approx trend) = N^%.3f" % steps
timefactor = ((math.log(max(timeTotals)) - math.log(min(timeTotals))) / \
(math.log(max(inputLengths)) - math.log(min(inputLengths))),)
print "Time (approx trend) = N^%.3f" % timefactor
# inputs
inputs = (\
# these first two are good for testing visualization
'a*a',\
'a*(a)',\
'a*a-a--(a*a)+-b--c,'*6 +'a*a-a--(a*a)+-b--c',\
'a*a-a--(a*a)+-b--c,'*12+'a*a-a--(a*a)+-b--c',\
'a*a-a--(a*a)+-b--c,'*18+'a*a-a--(a*a)+-b--c',\
'a*a-a--(a*a)+-b--c,'*24+'a*a-a--(a*a)+-b--c',\
# you probably won't want to run the one below until you have a faster grammar
#'a*a-a--(a*a)+-b--c,'*2000+'a*a-a--(a*a)+-b--c'
)
failing_inputs = (\
# you can use these to see how failing parses look
'a*a*',\
'a*(*-a)',
)
if __name__ == '__main__':
run(G_fast,inputs)
run(G_fast,failing_inputs)