[DGD] parse_string

Mikael Lind mikkelin at gmail.com
Mon Feb 19 11:41:34 CET 2007


Hello,

2007/2/19, chris . <psych_mayo at hotmail.com>:
> I have 2 questions.  First, I am trying to make what should be a real easy
> auto-fun.  It would check to see if str2 contains str.  If yes return 1, if
> no return 0.  I am getting an error, and would like to know where i am going
> wrong (aside from not understanding compiler-ish).

You did not specify the error message, but I am guessing that it is a
tokenizing error. You define a token for your pattern, but there is no
token for anything else that could be in the searched string.
parse_string() allows you to define a nomatch token, that will match
anything else. You define it like this:

dummy = nomatch

Noah Gibbs has compiled some documentation and posts on parse_string():

http://phantasmal.sourceforge.net/DGD/Programming/Parse_String.html

There are some other problems with your code as well. Can you trust
the search string? What happens if it contains a token delimiter
(slash)? Also remember that tokens are regular expression, so if you
want a plain substring match, you may get unexpected results if the
search string contains characters that have a special meaning in
regular expressions.

If you want a plain substring match, I would go with explode() or
sscanf() instead of parse_string(). I expect a sscanf-based solution
to be more efficient, but slightly more complex because you need to
escape percent signs in the substring.

explode()-based solution:

int searchstr(string str,string str2) {
    return sizeof(explode(str + str2 + str, str)) >= 2;
}

sscanf()-based solution:

int searchstr(string str,string str2) {
    if (sscanf(str, "%*s%%") != 0) {
        /* escape all % in substring */
        str = implode(explode("%" + str + "%", "%"), "%%");
    }
    return sscanf(str2, "%*s" + str) != 0;
}

(The code above is untested.)

> My second question: This stuff seems like it can go pretty deep, and it
> would take a lot of effort for me to get out in the deep end.  I am not
> seeing myself writing complex parsers for player input.  Should I bother
> myself to pick up a textbook on compiler-ish, or in my situation does the
> effort outweigh the benefits?

The parser is powerful and is not limited to parsing player commands,
though it is probably overkill for this specific problem.

Regards,
Mikael



More information about the DGD mailing list