
Title:
0305 Concept Inventory

Description:

I would like to start out with an inventory of the concepts that are going to be used.

So far we have the notion of a pattern or a regular expression,

of a text to match against, of a result, which will also be a string of some kind.

It doesn't seem like there's all that many more concepts.

One thing that it looks like we'll need is some notion of a partial result,

and some notion of control over the iteration.

What do I mean by that?

Well, some of the matches are easy.

If we match search for a literal 'def' within 'abcdef,'

it's easy to imagine just going down the line of this string and saying

does it match here? No. Here? No. Here? No. Here? Yes. We'll return that result.

But what if we're matching with the patternlet's say we have the expression 'a*b+'

any number of a's followed by one or more b's.

In our API notation, we would write that as seq(start(lit('a') plus(lit('b')).

Let's say we're matching that against the string 'aaab.'

Now if we had a control structure that says sequence, look to match the first,

and then look at the second, and if the firststar(lit('a'))only had one possible result,

then it would say, yes, it matches here right at the start, now look for something after that.

Does it match plus(lit('b'))? No, it doesn't.

I've got to have iteration over the possible results.

I have to say that star(lit('a')) can match in more than one location.

It can match with zero instances of a, with 1, with 2, with 3,

and it's only after 3 that then we can match the second part, find the b,

and then find that the whole expression matches.

That's going to be the tricky part is worrying about this control

when one part of a sequence doesn't match or similarly when we have an alternative

between two possibilitiesa or b or alt(a, b).

This trickiness seems like it's going to be difficult,

but it all resolves itself after we make one good choice.

It takes some experience to see what that choice can be,

but if we decide to represent these partial results as a set of remainders of the text,

then everything falls into place. What do I mean by remainder?

I mean everything else after the match.

If we match a literal "a" the remainder after we match zero characters of this string

would be "aaab"three a's followed by a b.

The remainder after we match one a would be two a's followed by a b and so on.

What I'm going to do is define an auxiliary function called "match set,"

and it takes a pattern and a text, and it returns this set of remainders.

When given just this pattern here as the input, star(lit('a')), and this text as the text

then the remainder would be the set consisting of three a's and a b,

two a's and a b, one a and a b, or just b.

In other words, star(lit('a')) could have consumed 0, 1, 2, or 3 a's,

and that's the remainder that's left over.

So the result will be this set.