parsing a string

Started by
22 comments, last by daerid 19 years, 3 months ago
Have you considered making use of tools like flex/lex and bison/yacc?

Have you read the Dragon Book?

(Note: all of the above dates to the C era, so you will have to do your own adaptations to "modern C++ style" including proper use to std::strings.)
Advertisement
Yes I have read the dragon book! That is where I’ve gotten all the information I know about compiler construction. But a compiler is not what I’m looking for. More like a stripped down version of the front end of a compiler. I just need to tokenize word in a string. Evaluate the tokens for valid commands and then process them.

By the way, wouldn’t a single or double linked list work better then a tree for syntax or semantics? I think they would because you could use an algorithm similar to a translation diagram for finding syntax or semantic errors. A tree just seams to complicate things even more. Why branch off into leaves when you can just go straight and evaluate syntax using an FSM as I spoke of in earlier posts?
Take back the internet with the most awsome browser around, FireFox
Quote:Original post by sakky
Pretty much all I need to do is build something very similar to a command line argument parser. Like the one for Windows. All is needed is to parse through the string and build tokens separated by white space. The token are pushed onto a toke table. Then the table is walked through using a FSM. The FSM is just like a translation diagram. I can make a set of error definitions so that the FSM will return once an error occurs. So if a token was improper then the FSM would return E_INVALIDVALUE or E_INVALIDTOKEN. It would also return a pointer to the last token parsed. So then I could use the information to inform the user about the error.

I’m thinking about using std::string and std::list instead of building my own lists and string stuff. These thing is very simple.
Good to see you've come to your senses and put away your sledgehammer-flyswat. Most of the ideas thrown around here are way more than you need. You should write a simple description of valid input and code it to parse exactly that (or return an error) with the minimal code possible.
Hell, I doubt there was ever any need whatsoever to optimise this. It isn't something that should even touch your framerate, and loading times differences would be unnoticeable. K.I.S.S!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Here's probably the easiest way to do something like that:

#include <iostream>#include <string>#include <vector>#include <map>#include <boost/bind.hpp>#include <boost/algorithm/string.hpp>using namespace std;using namespace boost;typedef vector<string> stringvector_t;typedef map<string,string> optmap_t;optmap_t parse_opts(const string& cmdline){    optmap_t ret;    stringvector_t opts;    split(opts,cmdline,is_space());        // loop through each    stringvector_t::iterator current = opts.begin();    stringvector_t::iterator end = opts.end();    for(;current != end; ++current)    {        stringvector_t vals;        split(vals,*current,bind(equal_to<char>(),'=',_1));        ret[vals[0]] = vals[1];            }    return ret;}int main(int argc,char* argv[]){    string cmdline = "name1=value1 name2=value2 name3=value3";        optmap_t opts = parse_opts(cmdline);        optmap_t::iterator current = opts.begin();    optmap_t::iterator end = opts.end();    for(;current != end;++current)        cout << "Opt Name: " << current->first << ", Opt Value: " << current->second << '\n';        return 0;}
daerid@gmail.com

This topic is closed to new replies.

Advertisement