[MUD-Dev] Text Parsing

Albert thecheezeman at earthlink.net
Fri May 28 14:35:49 CEST 1999


Delurking :)

I'm currently building a text adventure game, with intentions of expanding it into some sort of 
mud server once I master network programming. In any case, I was spending some time thinking 
about how to parse text and came to the follow conclusion. Nearly any command can be broken apart 
into four different strings: verb, direct object, indirect object, and adjectives. I have a class 
called ParsedCommand that looks like this:

class ParsedCommand { public:
        string verb;
        string direct_object; // Noun being manipulated
        string indirect_object; // Noun that receives the action
        string direct_object_adj; // Adjectives describing direct object
        string indirect_object_adj; // Adjectives describing the indirect object
        bool IsNewClause;
}

Then say the player enter a command like: buy the biggest ball from the 3rd shopkeeper
The convoluted pseudocode would be:

void Parse(string & rhs) {
        ParsedCommand pc;
        string word;
	while (1) {
		word = GetNextWord(rhs); // Would magically keep pointer position
		if (word.empty())	break;		
		switch (GetKind(word)) {
		case VERB:
			if (pc.IsNewClause)
				pc.verb = word;
			else
				SendToQueue(pc);
			break;
		case DIRECT_OBJECT:
			if (pc.IsNewClause) {
				pc.direct_object = word; 
				pc.IsNewClause = false;
			} else
				pc.direct_object += " " += word;
			break;
		case INDIRECT_OBJECT:
		case ADJECTIVE:
			// Pretty much the same code for direct_object
		case CONJUNCTION:
			SendToQueue(pc); // Would send a copy to the queue, but pc still exists
			pc.IsNewClause = true; // Not technically true, but...
			break;
		}
	}
}

I know, this system is hugely inefficient. But I believe it allows the user to enter complicated 
commands like "get the red ball and the 4th rock then kill the troll and ogre". Because pc 
persists, it can use data interpreted from previous commands and simply send copies to the work 
queue. Comments?

--
Author: Albert Yi (TheCheeseMan)  ICQ: 14617788
home.earthlink.net/~thecheezeman AOL: EvlWombat

Author: Albert Yi (TheCheeseMan)  ICQ: 14617788
home.earthlink.net/~thecheezeman




_______________________________________________
MUD-Dev maillist  -  MUD-Dev at kanga.nu
http://www.kanga.nu/lists/listinfo/mud-dev




More information about the mud-dev-archive mailing list