|
![](/i/fill.gif) |
Invisible wrote:
>> OK, so how do you do
>>
>> (\+|-)[0-9]+(\.[0-9]+)?(E(\+|-)?[0-9]{1,3})?
>>
>> in your parser language?
>
> OK, that's one big ol' complex regex, right there.
That's a pretty simple regex, actually.
>> (That is, optional sign, one or more digits, optional decimal point
>> followed by one or more digits, optional E followed by optional sign
>> followed by one to three digits.)
>
> If I've understood the spec correctly, it's
>
> do
> option (char '+' <|> char '-')
> many1 digit
> option (char '.')
> many1 digit
> option (do char 'E'; option (char '+' <|> char '-'); many1 digit)
Bzzzt. Sorry. That requires at least a 2-digit number.
> Enforcing that the exponent is less than or equal to 3 digits would be
> slightly more wordy. The obvious way is
>
> xs <- many1 digit
> if length xs > 3 then fail else return ()
>
> Notice that since this is written in a /real/ programming language and
> not a text string, we can do
>
> sign = char '+' <|> char '-'
>
> number = do
> option sign
> many1 digit
> option (char '.')
> many1 digit
> option (do char 'E'; option sign; many1 digit)
>
> and save a little typing.
You can do that with many regexp engines too. That's what I meant by
"trivial macros".
> You can also factor the task into smaller pieces:
Yeah, that's just a text substitution in the regexp expression.
> With a regex, on the other hand, you cannot even statically guarantee
> that a given string is even a syntactically valid regex.
Nonsense. How'd you get that?
If you mean you can't use a regexp to parse a regexp, well, yeah, so?
--
Darren New, San Diego CA, USA (PST)
Serving Suggestion:
"Don't serve this any more. It's awful."
Post a reply to this message
|
![](/i/fill.gif) |