String parsing ideas

Afternoon everybody! It's been quite a while since I visited this forum. I kind of lost interest in coding in C++ for a while, ran out of interesting things to do. But long behold, I'm back. I was daydreaming in class and came up with this idea to build a virtual test-maker. Sort of like an online test, where I can make my own and take them over again to help myself study. I thought it was a rather cool idea.
Anyway, I can't decide on an approach to take in parsing the text file that stores text information. I've already came up with the syntax that it will be stored as, an example would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
description {
    text=This would be the test description;
}

question {
    text=This is question number 1;
    tip=This is the question tip;
    type=multiple_choice;

    ac {  //ac stands for answer choice
        isCorrect=true;
        text=answer choice 1;
    }
    ac {
        isCorrect=false;
        text=answer choice 2;
    }   
}


It turned out sort of like a little scripting language.

But back to my point, I'm at a halt at approaches on how to parse it and extract question data effectively.

I can't use string.find("question") because the word 'question' might appear in the text of something.

And how to find the closing bracket of a tag, because there's other closing brackets inside it.
Maybe keep a counter, and go through the string character by character, and every open bracket increment the counter, and every closing bracket decrement the counter, and when the counter is 0 that means i've come across the closing bracket.

I'm probably over thinking this, and its actually a lot simpler than i'm seeing it.

Any input would be appreciated!
Thanks a bunch~
DESERTER!

I would actually recommend using PERL for this, but if you want to use C++, then... how comfortable would you be with using regular expressions, either the Boost or C++0x variants? For the example you gave, you could use a regular expression like:
question\s*\{
...to isolate the start of your question block (assuming proper syntax). :)

-Albatross
Last edited on
I'M SORRY!
I assure you now, I'm back, Albatross! (:

I probably should, i don't know PERL, though. C++ is the only language I'm fluent in.
Here's another idea, i could change it to work like HTML tags. Though, the script won't look anywhere as cool :P
I'd just use an XML lib for this. There's probably XML for any programming language you'd want.
Topic archived. No new replies allowed.