-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_19_a.py
82 lines (72 loc) · 1.31 KB
/
puzzle_19_a.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
import sys
import re
import copy
ruleflag = 1
mesg = []
nterms = {}
terms = {}
ind = 0
total = 0
ln = 0
def parse(rule):
global stack
global ind
if ind >= ln:
return 0
print("parse..rule",rule,"index",ind)
if rule in nterms:
anyrule = 0
index = ind
for rrule in nterms[rule]: #each different possibility
ind = index
lst = rrule.split(' ')
print(lst)
parsed = 1
for i in range(len(lst)): #each component of a rule
if not parse(lst[i]):
parsed = 0
break
if parsed:
anyrule = ind
if anyrule:
ind = anyrule
return anyrule
elif rule in terms:
token = stack[ind]
if token == terms[rule]:
print("Match", token, "index", ind,"\n")
ind+=1
return 1
else:
print("NNNNOMatch", token, "index", ind,"\n")
return 0
for line in sys.stdin:
print(line)
line = line.strip()
line = re.sub('"','',line)
if line == '':
ruleflag = 0
continue
if ruleflag :
(lhs,rhs) = line.split(': ')
if rhs in 'ab':
terms[lhs] = rhs
else:
nterms[lhs] = []
rules = rhs.split(' | ')
for rl in rules:
nterms[lhs].append(rl)
else:
mesg.append(line)
print(nterms,"===")
print(terms)
print(mesg)
for msg in mesg:
ind = 0
print(msg,"MESG")
stack = list(msg)
ln = len(stack)
print(stack,"STARTINNG")
if parse('0') and ind == ln:
total+=1
print(total)