1 | # |
---|
2 | # Recursive Parenthesis Matching |
---|
3 | # |
---|
4 | # |
---|
5 | # Robert D. Cameron |
---|
6 | # October 14, 2012 |
---|
7 | # |
---|
8 | import sys |
---|
9 | import pablo |
---|
10 | |
---|
11 | class Basis_bits(): |
---|
12 | bit_0 = 0 |
---|
13 | bit_1 = 0 |
---|
14 | bit_2 = 0 |
---|
15 | bit_3 = 0 |
---|
16 | bit_4 = 0 |
---|
17 | bit_5 = 0 |
---|
18 | bit_6 = 0 |
---|
19 | bit_7 = 0 |
---|
20 | |
---|
21 | class Lex (): |
---|
22 | LParen = 0 |
---|
23 | RParen = 0 |
---|
24 | |
---|
25 | class Matches() : |
---|
26 | closed = 0 |
---|
27 | instring = 0 |
---|
28 | error = 0 |
---|
29 | |
---|
30 | |
---|
31 | def Classify_bytes(basis_bits, lex): |
---|
32 | temp1 = (basis_bits.bit_0 | basis_bits.bit_1) |
---|
33 | temp2 = (basis_bits.bit_2 &~ basis_bits.bit_3) |
---|
34 | temp3 = (temp2 &~ temp1) |
---|
35 | temp4 = (basis_bits.bit_4 &~ basis_bits.bit_5) |
---|
36 | temp5 = (basis_bits.bit_6 | basis_bits.bit_7) |
---|
37 | temp6 = (temp4 &~ temp5) |
---|
38 | lex.LParen = (temp3 & temp6) |
---|
39 | temp7 = (basis_bits.bit_7 &~ basis_bits.bit_6) |
---|
40 | temp8 = (temp4 & temp7) |
---|
41 | lex.RParen = (temp3 & temp8) |
---|
42 | |
---|
43 | def Match_Parens(lex, matches): |
---|
44 | parens = lex.LParen | lex.RParen |
---|
45 | pscan = pablo.AdvanceThenScanTo(lex.LParen, parens) |
---|
46 | matches.closed = pscan & lex.RParen |
---|
47 | matches.instring = pablo.ExclusiveSpan(lex.LParen, pscan) |
---|
48 | matches.error = pablo.atEOF(pscan) |
---|
49 | # Not matched, still pending. |
---|
50 | pending_LParen = pscan & lex.LParen |
---|
51 | RParen_unmatched = lex.RParen &~ matches.closed |
---|
52 | inPlay = pending_LParen | RParen_unmatched |
---|
53 | while pending_LParen: |
---|
54 | pscan = pablo.AdvanceThenScanTo(pending_LParen, inPlay) |
---|
55 | matches.instring |= pablo.SpanUpTo(pending_LParen, pscan) |
---|
56 | matches.closed |= pscan & lex.RParen |
---|
57 | matches.error |= pablo.atEOF(pscan) |
---|
58 | pending_LParen = pscan & lex.LParen |
---|
59 | RParen_unmatched = lex.RParen &~ matches.closed |
---|
60 | inPlay = pending_LParen | RParen_unmatched |
---|
61 | # |
---|
62 | # Any closing paren that was not actually used to close |
---|
63 | # an opener is in error. |
---|
64 | matches.error |= lex.RParen &~ matches.closed |
---|
65 | |
---|
66 | |
---|
67 | basis_bits = Basis_bits() |
---|
68 | lex = Lex() |
---|
69 | matches = Matches() |
---|
70 | |
---|
71 | if __name__ == "__main__": |
---|
72 | #print "Starting ..." |
---|
73 | if len(sys.argv) > 1: |
---|
74 | u8data = pablo.readfile(sys.argv[1]) |
---|
75 | pablo.EOF_mask = pablo.transpose_streams(u8data, basis_bits) |
---|
76 | Classify_bytes(basis_bits, lex) |
---|
77 | Match_Parens(lex, matches) |
---|
78 | lgth = len(u8data) |
---|
79 | print "data:" + " "*(16-5) + u8data |
---|
80 | print "errors" + " "*(16-6) + pablo.bitstream2string(matches.error, lgth+1) |
---|
81 | print "instring:" + " "*(16-9) + pablo.bitstream2string(matches.instring, lgth) |
---|
82 | |
---|
83 | else: |
---|
84 | print("Usage: python parenmatch.py <file>") |
---|
85 | |
---|
86 | |
---|
87 | |
---|