Skip to main content
gamedev.net
🔒 Locked

parser help, please

Started by randomDecay Jan 6, 2002 at 12:38 AM 0 replies 150+ views
Original Post
randomDecay
randomDecay
I am working on my text parser right now. I don't want to post a lot of code, so here is the gist of it. I separate the string of input into tokens and call three functions to examine them to determine which word is what type and in what position. My parser only takes two words so you're guaranteed either one or two words.
        
	int verbLoc = 0;
	int objLoc  = 0;
	int motLoc  = 0;

	// analyze each word to find out what kind of word it is

	// and which word contains what type

	verbLoc = Verb();
	objLoc  = ObjectWord();
	motLoc  = MotionWord();
  
a return of 0 = no word match a return of 1 = match in word 1 a return of 2 = match in word 2 what is the best or easiest way to determine what the hell the two-word sentence means and what actions the game takes? Thanks edit: here is the code I use after the previous code bit, which I think is sloppy and not very professional:
  
	if ( m_nWords == 1 )
	{
		if ( verbLoc == 1 )
		{
			// need object, but check for save, inventory, etc..

		}
		else if ( objLoc == 1 )
		{
			// need verb

		}
		else if ( motLoc == 1 )
		{
			m_doTravel = true;
		}
		else
		{
			// doesn't make sense

		}
	}
	// process these possibilities of two word input

	else if ( m_nWords == 2 )
	{
		if ( verbLoc == 1 && objLoc == 2 )
		{
			m_doAction = true;
		}
		else if ( m_vIdent == GO && motLoc == 2 )
		{
			m_doTravel = true;
		}
		else
		{
			cout << "What?" << endl;
		}
			
	}
      
Edited by - randomDecay on January 6, 2002 1:42:25 AM
mutex
mutex
consider using bison to write a parser? it looks intimidating at first but once i got the hang of the language, i was able to write a parser for a C-like language (but never got around to finishing the bytecode compiler anyway, here''s one point that took a while for me to get:

flex: converts text to tokens (ie: numbers, keywords, strings...)

bison: parses the tokens and carries out actions for specific patterns

you can find info on bison and flex at www.gnu.org

it''s buried somewhere in there

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.