I'm working as an assistant in a research project at the moment. I'm supposed to write some kind of simplistic testing system that takes a list of actions and checks if that sequence is possible (and how likely it is, but that's besides the point).
Basically, due to my employers using a variety of systems and compilers, I am restricted to C++03 and no third party libraries (yeah, no boost either).
Since I'm stuck with the standard library, what is the best way to parse lines in standard C++? So far it seems to me that just using std::string (and stringstream for datatype conversions) will get me the farthest - it seems to have most of what I need (not all I want, but oh well...), but I might be wrong.
Basically, getline does a great job of tokenizing a stream (you can specify a delimiter). A stringstream can be created from a string to enable this approach. That is the most straightforward way in C++03 that I have seen.
Things can get more difficult if you have to watch out for multiple delimiter characters at once or if the delimiters are more than 1 character. In that case, I think the strings find_* methods can do the trick.
I have to deal with optional parameters though, so getline isn't really an option. Thanks anyways, I'll keep that in mind (I'm hoping to make the optional parameters non-optional later on, because there isn't really any reason for them to be optional other than the fact that my employers wanted to see a prototype really quick cause they weren't sure I could do it).
Yeah, but that's just way overkill (I think) - there's really just one possible type of line with slight variations for now. It's not really that hard to write, I was just wondering because I don't know how much more they want (and they apparently don't either, at least not at the moment).
I think I'll go with what I have for now, and come back to it later should the need arise.
I also suggest looking into regular expressions. They can be a bit difficult to learn but can really expedite your coding process. Especially, if you have strings involved.
I.E. cout << "this is \"some\" string";
It can be more complicated but just as an example.
hanst99, that's part of parsing... If something matches a pattern, categorize it, else feed more input. If nothing matches a pattern, it's invalid input. Then using those categorized strings, you can define grammar. The grammar can define optional input quite easily and is very common.
I still think Flex/Bison would be a good bet here. I see people use it for general things like parsing the command line.
Of course I'm using getline to get the lines, but I don't see how I would use getline to extract data - all I can do is pass a delimiter that may or may not be there, which doesn't seem that terribly useful to me (am I missing something?).
I am going to look into flex, and maybe into bison too, I just didn't want to waste time learning how to use a new tool (two in this case) before starting to work on it - especially since this is a really simple grammar, and doesn't really warrant such effort. I just wanted to know if there's something I missed (like, there being other useful functions other than getline or the string methods).
on a note to flex... I am trying to find some material on it, this (http://flex.sourceforge.net/manual/Simple-Examples.html#Simple-Examples ) is the first I found - it seems to use what people used to think C looks like. Is that something flex-specific, or are you supposed to write a C program and that text is just really, really old?
Basically, due to my employers using a variety of systems and compilers, I am restricted to C++03 and no third party libraries (yeah, no boost either).
In that case, yes, I'd suggest flexcpp, which as the name implies generates C++ code. You may also want bison if you think you'll need it, but flex(cpp) should be enough. :)
it seems to use what people used to think C looks like. Is that something flex-specific, or are you supposed to write a C program and that text is just really, really old?
Eh, well. That example seems to have a few errors in it. Flex expects three sections, one containing the data that will be prepended to the generated C/C++ file, one containing the regular expressions and rules explaining what to do with them, and one that will be copied into the generated C/C++ file.
BisonC++'s generated code seems to be under GPL - I assume this applies to FlexC++ as well. I remember there being a discussion about this rather recently, but I don't remember the details anymore - how are you allowed to use GPL code? I'm pretty sure you can't statically link to programs that aren't "GPL compatible" (whatever that means), but what about dynamic linking?
I personally am not planning on creating proprietary software anytime soon, but I'm not sure how the rest of the project I'm working on is licensed - and I certainly don't want to be responsible for importing unnecessary or even harmful license restrictions.