Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Compilers > Re: Generate te...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 6 Topic 2369 of 2472
Post > Topic >>

Re: Generate text, given a regex

by m.helvensteijn@[EMAIL PROTECTED] Mar 27, 2008 at 12:49 AM

Midhat wrote:

> Hi. I want to generate text based on a given regex. just any text that
> satisifies the regex. Are there any existing tools/libraries to do
> that.

I don't know any tools/libraries, but it seems like this problem isn't so
difficult.

* Create a syntax tree of the regex.

* Generate text (T(r)) for the leafs of the tree (r). The atomic regexes:
  - If r = a character, T(r) = that character
  - If r = a character class, T(r) = any character satisfying that class
  - If r = . , T(r) = any character (except perhaps \n)

* Recursively generate text (T(R)) for the non-atomic sub-regexes (R) of
the
tree, assuming text has already been generated for all the immediate
sub-regexes of R:
  - If R = x*,  T(R) = a concatenation of any number of T(x).
  - If R = x+,  T(R) = a concatenation of 1 or more of T(x).
  - If R = x?,  T(R) = the empty string or T(x).
  - If R = xy,  T(R) = the concatenation of T(x) and T(y).
  - If R = x|y, T(R) = T(x) or T(y)
  - If R = (x), T(R) = T(x)

Any choices you make here will lead to T(regex), the text you want. Of
course, if any text will do, you can immediately return empty strings for
x* and x?, without processing x.

And I am assuming here that we are talking about regular expressions in
the
classical sense. So without recalling previously captured sub-expressions
and such. However, if you want to have those anyway, there are still
relatively easy adaptations you can make to this algorithm to make it
work.

Good luck!

--
Michiel Helvensteijn
 




 6 Posts in Topic:
Generate text, given a regex
Midhat <midhatali@[EMA  2008-03-23 23:48:39 
Re: Generate text, given a regex
m.helvensteijn@[EMAIL PRO  2008-03-27 00:49:58 
Generate text, given a regex
Domenico Bianculli <do  2008-03-27 23:56:04 
Re: Generate text, given a regex
Gene <gene.ressler@[EM  2008-03-26 21:15:45 
Re: Generate text, given a regex
Gene <gene.ressler@[EM  2008-04-11 13:23:32 
Re: Generate text, given a regex
"Russ Cox" <  2008-04-11 16:59:01 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 2:54:43 CDT 2008.