Skip to main content
GameDev.net gamedev.net
🔒 Locked

question about tokens?

Started by Stashi Nov 27, 2001 at 3:52 PM 3 replies 450+ views
Original Post
Stashi
Stashi
i think i have a basic understanding of what parsing is..correct me if im wrong but isnt it just taking a string and sticking every word (filtering out whitespace) into "tokens" which can be used later on? well, anyway... what exactly are "tokens" made of? does a person create an infinite array of strings that are called "tokens" in which the words can be placed in? what are they? how and in what are tokens stored in?
Shannon Barber
Shannon Barber
Parsing is a very general process - with regard to compiling it''s breaking down the textual code into lexical tokens (program language things, like a for loop, or a multiplication, assignment, etc...)

A token is the vague concept of a single item of something that represents something else. A Chucky-Cheese token represents 1/4 the amount of money you need to play a game. A compiler token represents one thing of language (again, things like for loops, assigments, an addition, etc...)

Magmai Kai Holmlor
- Not For Rent
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Oluseyi
Oluseyi
Continuing Magmai''s explanation, an English language token is a word.

You''re probably interested in string tokenization. There''s a Standard C Library function called strtok() especially for this purpose, while the Standard C++ Library requires you to use find_..._of() family of functions.

// C code
// tokenize takes the string, the array in which to store the
// tokens and the string of separators, and returns the number
// of tokens found
int tokenize(char *string, char **tokens, char *seps)
{
int nTokens = 0;
char *tok = strtok(string, seps);
while(tok != NULL)
{
// note that I''m simply assigning pointers here. You might
// want to memcpy() the string instead
tokens[nTokens] = tok;
++nTokens;
}
return nTokens;
}
.
// C++ code
// tokenize takes a string object, a vector of strings in which
// to store the tokens and the string of separators, and returns
// the number of tokens found
int tokenize(string str, vector<string> &tokens, string seps)
{
int nTokens = 0;
int pos0 = 0, pos1 = 0;
while(true)
{
pos1 = str.find_first_of(seps.c_str(), pos0);
if(pos1 == string::npos)
break;
tokens[nTokens] = str.substr(pos0, (pos1 - pos0));
// get ready for next extraction
pos0 = str.find_first_of(seps.c_str(), ++pos1);
++nTokens;
}
return nTokens;
}

If any of the above code doesn''t perform as expected, let me know so I can send you some code somewhere on my D drive that correctly tokenizes STL strings.


Start Here!
Stashi
Stashi
now in usage of scripting, how would i compare those tokens to something?

meaning if the word "resize" was tokenized, how would i have my program know to resize a matrix? will i hundreds of if/case statements to do all possible commands?
Arild Fines
Arild Fines
One way would be to use some form of table to map keywords to functions/objects. Something like this, f.ex:
  
class ICommand
{
public:
virtual void execute( const Arguments& args ) = 0;
};

//...


std::map< std::string, ICommand* > commandMap;

//...


std::string command = getNextCommand();
Arguments args = getArgs( );

( commandMap[ command ] )->execute( args );


Psalm 137:9: "Happy shall he be, that taketh and dasheth thy little ones against the stones."
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

Topic Locked

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

Sign in to reply to this topic.