source: proto/RE/Haskell/REcompile.hs @ 3614

Last change on this file since 3614 was 3614, checked in by cameron, 5 years ago

Add regexp parser; move compiler into REcompile

File size: 7.1 KB
Line 
1-- Module REcompile is a research compiler for prototyping
2-- algorithms and ideas in regular expression processing using
3-- parallel bit streams and MatchStar.
4-- It uses a canonical representation for regular expressions
5-- that uses a small number of alternative forms (e.g., combining
6-- all forms of repetition (Kleene star, Kleene plus, ?, {m,n}) into
7-- a single Rep structure. 
8
9-- Robert D. Cameron, 2014
10
11module REcompile (compile) where
12       
13import Data.Char
14import CanonicalRE
15import REparse
16import RunPablo
17
18--
19-- The Symbol Environment keeps track of the symbols generated
20-- during compilation, so that new unique symbols can be
21-- generated (gensym) as needed.   All symbols are formed from
22-- a string prefix and a numeric suffix.
23--
24-- For example, if the symbols "marker0", "marker1", "condition0"
25-- have been generated and no others have, the environment is
26-- [("marker", 1), ("condition", 0)]
27--
28type Env = [(String, Int)]
29
30gensym :: (Env, String) -> (Env, String)
31gensym ([], s) = ([(s, 0)], s ++ show 0)
32gensym ((prefix1, count1):env, s) 
33  | prefix1 == s = ((s, count1+1):env, s ++ show(count1+1))
34  | otherwise    = ((prefix1, count1):newenv, newsym)
35  where (newenv, newsym) = gensym(env, s)
36
37
38--
39-- To keep track of the current context for code generation during
40-- compilation, we define a CodeGenState that represents the output
41-- at each compilation step.   This state consists of an environment
42-- defining the symbols used so far, the sequence of generated PabloS
43-- statements generated so far and the name of the variable that
44-- holds the match result for the overall expression so far.
45--
46type CodeGenState = (Env, [PabloS], String)
47
48--
49-- In compiling to regular expression form, we use a helper function
50-- that takes a regular expression and a code generation state, and
51-- returns the code generation state.   The main compile routine
52-- initializes the process by setting an initial marker stream
53-- of all ones.
54--
55--
56compile :: RE -> CodeGenState
57re2pablo_helper :: (RE, CodeGenState) -> CodeGenState
58
59-- We also define helper functions for the working through
60-- the RE lists in Seq and Alt structures, as well as the
61-- iteration required for Rep structures.
62seq_helper :: ([RE], CodeGenState) -> CodeGenState
63alt_helper :: ([RE], CodeGenState) -> CodeGenState
64rep_helper :: (RE, Int, RepLimit, CodeGenState) -> CodeGenState
65
66compile(re) = re2pablo_helper(re, (env, [Assign(marker, All(1))], marker))
67  where 
68    (env, marker) = gensym([], "start_marker")
69
70-- To match a character class, use bitwise and of the marker and
71-- character class, then advance 1. 
72
73re2pablo_helper(CC(c), (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
74   where 
75     (newenv, newsym) = gensym(env, "marker")
76     new_stmt = Assign (newsym, Advance(And(Var(last_marker), CharClass(c))))
77
78-- To match "^" we must be at the start of line, i.e., one past
79-- a newline character or at the beginning of file.  We use the
80-- trick of advancing the negated newline stream to determine these
81-- positions.
82--
83re2pablo_helper(Start, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
84   where 
85     (newenv, newsym) = gensym(env, "start_of_line_marker")
86     new_stmt = Assign (newsym, And(Var(last_marker), Not(Advance(Not(CharClass("\n"))))))
87
88-- To match "$" we must have reached end of line.
89--
90re2pablo_helper(End, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
91   where 
92     (newenv, newsym) = gensym(env, "end_of_line_marker")
93     new_stmt = Assign (newsym, And(Var(last_marker), CharClass("\n")))
94
95-- For the structured types (Seq, Alt, Rep), just call the specific helper.
96--
97re2pablo_helper(Seq rs, cg_state) = seq_helper(rs, cg_state)
98re2pablo_helper(Alt rs, cg_state) = alt_helper(rs, cg_state)
99re2pablo_helper(Rep (r, lb, ub), cg_state) = rep_helper(r, lb, ub, cg_state)
100
101
102-- Seq [] is the empty regexp which matches the empty string. 
103-- We just leave the current marker unchanged, with no new statements
104-- needed to compute it.
105--
106seq_helper([], cg_state) = cg_state
107-- Seq (r1:rs): generate code to match r1, the first subexpression,
108-- then to match the rest, concatenating the statements.
109seq_helper((r1:rs), cg_state) = seq_helper(rs, re2pablo_helper(r1, cg_state))
110
111-- Alt[r] has a single alternative r to match, just match
112-- it with no other possibility.
113alt_helper([r], cg_state) = re2pablo_helper(r, cg_state)
114
115-- For completeness, we define Alt[] as the regular expression that
116-- always fails (since no alternatives will match).
117alt_helper([], (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
118   where 
119     (newenv, newsym) = gensym(env, "always_fail_marker")
120     new_stmt = Assign (newsym, All(0))
121
122-- Alt (r1:rs): we have r1 and some other alternatives, the
123-- match succeeds if either r1 matches or any of the others
124-- does.
125alt_helper(r1:rs, (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
126  where 
127   (e1, s1, alt1_marker) = re2pablo_helper(r1, (env, stmts, last_marker))
128   (e2, s2, alt2_marker) = alt_helper(rs, (e1, s1, last_marker))
129   (newenv, newsym) = gensym(e2, "alt_marker")
130   new_stmts = s2 ++ [Assign (newsym, Or(Var(alt1_marker), Var(alt2_marker)))]
131
132-- For Kleene Star character class repetition, use MatchStar
133rep_helper(CC c, 0, Unbounded, (env, stmts, last_marker)) = (newenv, stmts ++ [new_stmt], newsym)
134   where 
135     (newenv, newsym) = gensym(env, "marker")
136     new_stmt = Assign (newsym, MatchStar(Var(last_marker), CharClass(c))) 
137
138-- For general Kleene Star, we need a while loop.
139rep_helper(r, 0, Unbounded, (env, stmts, last_marker)) = (newenv, stmts ++ while_init ++ [loop_stmt], while_accum)
140  where
141    (e1, while_test) = gensym(env, "while_test")
142    (e2, while_accum) = gensym(e1, "while_accum")
143    (newenv, loop_body_stmts, loop_result) = re2pablo_helper(r, (e2, [], while_test))
144    while_init = [Assign(while_test, Var(last_marker)), Assign(while_accum, Var(last_marker))]
145    repeat = [Assign(while_test, And(Var(loop_result), Not(Var(while_accum)))),
146              Assign(while_accum, Or(Var(while_accum), Var(loop_result)))]
147    loop_stmt = While(Var(while_test), loop_body_stmts ++ repeat)
148
149rep_helper(r, lb, Unbounded, cg_state) = rep_helper(r, lb-1, Unbounded, cg1_state)
150  where 
151   cg1_state = re2pablo_helper(r, cg_state)
152
153-- Now Bounded Repetition: use multiple copies
154   
155rep_helper(r, 0, UpperBound 0, cg_state) = cg_state
156
157rep_helper(r, 0, UpperBound ub, (env, stmts, last_marker)) = (newenv, new_stmts, newsym)
158  where 
159   (e1, s1, rep1_marker) = re2pablo_helper(r, (env, stmts, last_marker))
160   (e2, s2, rep2plus_marker) = rep_helper(r, 0, UpperBound(ub-1), (e1, s1, rep1_marker))
161   (newenv, newsym) = gensym(e2, "alt_marker")
162   new_stmts = s2 ++ [Assign (newsym, Or(Var(last_marker), Var(rep2plus_marker)))]
163
164rep_helper(r, lb, UpperBound ub, cg_state) = rep_helper(r, lb-1, UpperBound(ub-1), cg1_state)
165  where 
166   cg1_state = re2pablo_helper(r, cg_state)
167
168---------------
169
170searchRE :: (RE, String) -> PabloVal
171searchRE(regexp, str) = evalPabloS(compiled, marker, str)
172  where (e, compiled, marker) = compile(regexp)
173
174searchRegExp :: (String, String) -> Maybe PabloVal
175searchRegExp(regexp, str) =
176  case parseRE(regexp) of
177    ParseSuccess r  -> Just (searchRE(r, str))
178    _ -> Nothing
179
Note: See TracBrowser for help on using the repository browser.