source: proto/RE/demo/grep_demo.py @ 3738

Last change on this file since 3738 was 3738, checked in by ksherdy, 4 years ago

Added tests. Added -b -o grep flags.

File size: 7.3 KB
Line 
1#
2# grep - A very simple example to demonstrate grep with parallel bit streams.
3#
4#        This prototypes demonstrates a compilable prototype with
5#        minimal reliance on complementary hand-written template code.
6#        In addition, this examples draws attention to missing features in the Pablo
7#        language which prevent the elimination of hand-written templates.
8#
9#  LF    -> trailing line feeds as part of a matched line
10#  NO LF -> GNU grep - trailing line feeds are not part of a matched line
11#
12#
13# Ken Herdy
14# July 29, 2013
15#
16# grep program flow
17#
18# StreamInput' -> Transpose' -> ClassifyBytes -> Match -> MatchLines -> FilterMatchLines -> StreamOutput'
19#                                                                 
20# grep -c program flow
21#
22# ... MatchLines -> CountLines -> StreamOutput''
23#
24# grep | wc -l program flow
25#
26# ... StreamOutput' -> Transpose' -> MatchLF -> PopCount -> StreamOutPut''
27#   
28# ' marks denote missing functionality which prevent the direct compilation of Pablo code to C/C++ code
29#
30# Observations:
31#
32# 1) Both sequential and bit parall_matchedel computations are performed.
33#   
34#    Examples include:
35#       
36#          a) sequential iteration over the matches and line feed streams in the 'Match' routine
37#          b) sequential and/or parall_matchedel output of marked lines in the 'CompressLines' / 'WriteLines' routine   
38#
39# 2) Creation of spans are problematic at the start of an input data stream.
40#
41#    A solution approach to this problem is to:
42#               
43#    a) formalize the meaning of the expressions of the language as expressions
44#    operating on sequences (streams) of 2^k bit data items both preceded and succeeded by an unbounded number of
45#    2^k with zero value items.
46#
47#    i.e. leverage denotational semenatics to define what parall_matchedel bit stream programs do.
48#
49#    b) define the 'line starts' stream as line_starts = ~Adv~(0) | Advance(lines_end_marks)
50#
51#    This technique can then be generalized to create spans in the case ends but not starts are marked.
52#               
53# 3) Options such as -c represent separate branches in the underlying stream graph.
54#
55# 4) Support of pipes and filter type applications requires subgraph composition but only a single transposition step.
56#    As in SPL a composite program structure could support UNIX-style pipe and filters archicture for
57#    parall_matchedel bit stream apps, e.g. composite { graph { /* program flow */ } }
58#
59import sys
60import pablo
61import optparse
62import grep_demo_config
63
64class Basis(): 
65        bit_0 = 0
66        bit_1 = 0
67        bit_2 = 0
68        bit_3 = 0
69        bit_4 = 0
70        bit_5 = 0
71        bit_6 = 0
72        bit_7 = 0
73
74class Lex():
75        a  = 0
76        p  = 0
77        l  = 0
78        e  = 0
79        LF = 0
80       
81class Output():
82        matches                 = 0
83        lines                   = 0
84        line_starts             = 0
85        line_ends               = 0
86        byte_data               = 0
87
88def ClassifyBytes(basis_bits, lex): 
89        temp1 = (basis_bits.bit_1 &~ basis_bits.bit_0)
90        temp2 = (basis_bits.bit_2 &~ basis_bits.bit_3)
91        temp3 = (temp1 & temp2)
92        temp4 = (basis_bits.bit_4 | basis_bits.bit_5)
93        temp5 = (basis_bits.bit_7 &~ basis_bits.bit_6)
94        temp6 = (temp5 &~ temp4)
95        lex.a = (temp3 & temp6)
96        temp7 = (basis_bits.bit_2 & basis_bits.bit_3)
97        temp8 = (temp1 & temp7)
98        temp9 = (basis_bits.bit_6 | basis_bits.bit_7)
99        temp10 = (temp4 | temp9)
100        lex.p = (temp8 &~ temp10)
101        temp11 = (basis_bits.bit_4 & basis_bits.bit_5)
102        temp12 = (temp11 &~ temp9)
103        lex.l = (temp3 & temp12)
104        temp13 = (basis_bits.bit_5 &~ basis_bits.bit_4)
105        temp14 = (temp13 & temp5)
106        lex.e = (temp3 & temp14)
107        temp15 = (basis_bits.bit_0 | basis_bits.bit_1)
108        temp16 = (basis_bits.bit_2 | basis_bits.bit_3)
109        temp17 = (temp15 | temp16)
110        temp18 = (basis_bits.bit_4 &~ basis_bits.bit_5)
111        temp19 = (basis_bits.bit_6 &~ basis_bits.bit_7)
112        temp20 = (temp18 & temp19)
113        lex.LF = (temp20 &~ temp17)
114       
115def Match(lex, output):
116        m0 = lex.a
117        m1 = pablo.Advance(m0) & lex.p
118        m2 = pablo.Advance(m1) & lex.p
119        m3 = pablo.Advance(m2) & lex.l
120        m4 = pablo.Advance(m3) & lex.e
121        output.matches = m4
122
123def MatchLines(lex, output):
124        crt_line_start  = pablo.Advance(~0) ^ ~0 # line_start == pablo.First()
125        cursor                  = crt_line_start
126
127        LF_or_match     = lex.LF | output.matches
128
129        while(pablo.inFile(cursor)):
130                cursor = pablo.ScanTo(cursor, LF_or_match)
131                if(cursor & lex.LF):
132                        crt_line_start = pablo.Advance(cursor)
133                        cursor = crt_line_start
134                if(cursor & output.matches):
135
136                        output.line_starts  |= crt_line_start
137                        output.line_ends    |= pablo.AdvanceThenScanTo(cursor, lex.LF)
138
139                        output.lines |= pablo.InclusiveSpan(output.line_starts, output.line_ends) # LF
140                        # output.lines |= pablo.SpanUpTo(line_start, cursor) &~ lex.LF                          # no LF
141                        cursor = pablo.Advance(cursor)
142
143def MatchLines2(lex, output): # verbose with temporary streams
144        line_start      = 0
145        line_end        = 0
146        line_start      = pablo.Advance(~line_start) ^ ~line_start # line_start == pablo.First()
147        output.line_starts = line_start
148
149        LF_or_match     = lex.LF | output.matches
150        cursor                  = pablo.ScanToFirst(LF_or_match)
151
152        while(pablo.inFile(cursor)):
153                if(cursor & lex.LF):
154                        line_start = pablo.Advance(cursor)
155                        cursor = line_start
156                if(cursor & output.matches):
157                        line_end = pablo.AdvanceThenScanTo(cursor, lex.LF)
158                        output.line_starts  |= line_start
159                        output.line_ends    |= line_end
160
161                        output.lines |= pablo.InclusiveSpan(line_start, line_end) | line_end    # LF
162                        # output.lines |= pablo.SpanUpTo(line_start, cursor) &~ lex.LF                  # no LF
163                        cursor = line_end
164                        cursor = pablo.ScanTo(cursor, LF_or_match)
165                        # cursor = next_end
166                               
167def FilterMatchLines(data, output):
168        output.byte_data = pablo.filter_bytes(data, ~output.lines)
169        # output.byte_data = pablo.filter_bytes(data, ~output.lines)                                                            # LF
170
171def CountLines(lex, output):
172        ### return pablo.PopCount(pablo.MatchStar(output.lines, ~lex.LF) & lex.LF)  # LF
173        return pablo.PopCount(output.lines & lex.LF)                                                                                                                            # LF
174        # return pablo.PopCount(pablo.Advance(output.lines) & lex.LF)                                           # no LF
175
176basis_bits      = Basis()
177lex             = Lex()
178output          = Output()
179
180if __name__ == "__main__":
181
182        option_parser = grep_demo_config.get_option_parser() 
183        options, args = option_parser.parse_args(sys.argv[1:])
184
185        if len(args) != 1:
186                option_parser.print_usage()
187                sys.exit()
188        else:
189                input_file = args[0]
190
191  # ReadStreamInput(data)
192        global data
193        data = pablo.readfile(input_file)
194 
195        global lgth
196        lgth = len(data)
197
198  # Transpose(data, basis)
199        pablo.EOF_mask = pablo.transpose_streams(data, basis_bits)
200        ClassifyBytes(basis_bits, lex)
201        Match(lex, output)
202        MatchLines(lex, output)
203
204  # WriteStreamOutput(Output)
205        if(options.count):
206          print str(CountLines(lex, output))
207
208        elif(options.invertMatch):
209            output.lines = ~output.lines                                                                        # LF
210            # output.lines = (~output.lines) &~ lex.LF          # no LF
211            FilterMatchLines(data, output)
212            print output.byte_data
213        else:
214            FilterMatchLines(data, output)
215            print output.byte_data
216
217        if(options.debug):
218                print ""
219                print "data.replace('LF','$')   " + data.replace("\n","$")
220                print "lex.a                    " + pablo.bitstream2string(lex.a, lgth)
221                print "lex.p                    " + pablo.bitstream2string(lex.p, lgth)
222                print "lex.l                    " + pablo.bitstream2string(lex.l, lgth)
223                print "lex.e                    " + pablo.bitstream2string(lex.e, lgth)
224                print "lex.LF                   " + pablo.bitstream2string(lex.LF, lgth)
225                print "output.matches           " + pablo.bitstream2string(output.matches, lgth)
226                print "output.line_starts       " + pablo.bitstream2string(output.line_starts, lgth)
227                print "output.line_ends         " + pablo.bitstream2string(output.line_ends, lgth)
228                print "output.lines             " + pablo.bitstream2string(output.lines, lgth)
229                print ""
230
231        #lgth = len(data)
232
233
234               
235       
236
237
Note: See TracBrowser for help on using the repository browser.