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

Last change on this file since 3734 was 3734, checked in by ksherdy, 6 years ago

Updated to match lines without LFs. GNU grep does this.

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