Sunday, 25 November 2012

Regular expression - How not to do it

I just ordered a book on regular expressions, after giving in to the realization that they kept coming up again and again. I figure if I'm going to keep using them I might as well try to make the best of a bad situation.

Until amazon can ship me a copy of Mastering Regular Expressions over from the UK, I'll have to do this the hard way. Let's take a look at exercise 1 for the quiz tomorrow:

Devise an NFSA to accept L( ( (0 + 1) (01)*)* )
I'm not convinced this color  coding has made that any more approachable but, i'll take a crack:
EDIT: This was admittedly a bad crack at this question, but i'll leave it.

 The language is made up of all strings that are made up of any number of the substrings '0X', '1X' where X is string made up of any number of the substring '01'.
Examples:
  • empty string
  • 0
  • 1
  •  001
  • 101

Maybe if I listed them in a more ordered way:

len(x)= = 0
  • ''
numstrings = 1
 len(x) = 1
  • '0'
  • '1'
numstrings = 2
len(x) == 2
numstrings = 0

len(x) == 3
  • '001'
  • '101'
numstrings = 2
len(x) == 4
  • '0010'
  • '0011'
  • '1010'
  • '1011'
numstrings = 2

Again a pattern emerges in the listing process.  I think i should consider the length of the strings in this language. So that should be easy to figure out and probably relevant to the later proof.

for an arbitrary string x in L:
length(x) == (1 + 2x )y for arbitrary naturals y,x

if y == 0:
    return 0
else:
    if x==0:
        return y
    else:
        return (1+2x)*y

In the first return statement, we take care of the empty string. In the second, we take care of the stuation in which x is 0. Namely, all the strings in L that do not contain the substring 01.

It seems my initial list of valid strings may have been flawed. I'll come back to this later.

No comments:

Post a Comment