Re: Why am I so stupid?
On Jun 5, 7:26 am, none <n...@none.none> wrote:
James Kanze wrote:
The answer to your problem, of course, is to use istream for all
of your parsing. After extracting the float, continue
extracting using the same istream (istringstream, etc.).
Ok, you (and others) have sold me on the concept of using the
istringstream for parsing, instead of working directly with
the string.
For the type of parsing you've been describing (or seem to have
been describing). For most of my own parsing, I use regular
expressions, or lex and yacc.
But istringstream makes the assumption that everything is
separated by whitespace, at least in the case of the >>
operator.
Partially. If you're reading an int, it will stop at the first
non-numeric character.
A typical lexer/parser only knows about "tokens." So I might
have this list of tokens:
"(", ")", "+", "-", "*", "/", "sqrt", "sin", "cos", ...
and I might wand to parse an input stream that looks like this:
"(1.2 * -sqrt(7.5e3))"
In other words, I can't rely on tokens to be separated by
whitespace. I need to "peek" before I actually extract, and I
might need to peek at more than one character -- for example,
to match the "sqrt" token.
So define a type Token, and write an >> operator for it, which
does what you want. (But I'd still just use lex, and be done
with it.)
I understand the theory of parsing an input sequence using a
grammar, and I have one that works beautifully in C using a
recursive-descent approach. I'd like to bring it up to date
by using an istringstream, or whatever STL construct is
appropriate. I'm finding the low-level nuts and bolts, like
tokenization, very difficult.
What are you using in C?
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34