[REBOL] Re: Parse versus Regular Expressions
From: lmecir:mbox:vol:cz at: 9-Apr-2003 17:12
> PARSE seems to partake of the same issue as Prolog; its seems to be
> implemented in a way that makes it a hybrid between declarative and
> procedural programming, with the result that sometimes things have
> side effects or behaviors that can be difficult to figure out.
I have read some grammar articles and came to an opinion, that PARSE the most closely
resembles lambda calculus, which I would call *purely procedural*. It looks so natural,
that it confused not just Joel, I think.
> I've just tripped over a tidy example of a task that's trivial to
> solve if backtracking is available.
> Find a repeated group of characters at the end of a string (one or more
> characters, repeated two or more times consecutively). For example,
> "my doggyoggy"
> "has as as as as "
> Note that some cases can be solved in multiple ways:
> in which case any solution is OK.
> The Perl/Python/etc-style regular expression which tests for this
> situation and provides the first occurrence of the repeated group
> of characters is
> which, for those not familar with RE "line noise" ;-) can be read
> (.+) a group (the parens) of one or more (the plus)
> arbitrary characters (the dot)
> \1 ... followed by the same group previously matched
> + ... one or more times
> $ ... at the end of the string
> I'm really interested in how a PARSE expert would approach the same
> task. Any takers?
To not start any flame wars, I would call the above expression an Enhanced
Regular Expression, because as we will see below, it isn't a Regular Expression at all.
We have seen the PARSE solution. I used my freshly acquired "knowledge" of grammar stuff
and tried to analyse the grammar. Here are the rules I came to(supposing, that the base
alphabet is just "ab", to simplify the expressions, S is the starting symbol):
First, some Regular Rules:
Context Free Rules:
Context Preserving Rules:
And, at last, Context Changing Rules:
Uff! This points to the fact, that the discussed grammar is neither Regular, nor Context
Free, and not even Context Preserving and it can hardly be called "simple".
As opposed to this, the earlier PARSE rule example:
z: [#"a" z #"b" | none]
can be expressed as:
Context Free Rule:
i.e. it is a Context Free Grammar.