I would suggest (and this is more or less true from compiler tutorials) that you approach the problem in stages. This destroys the situation, so the problem at each stage is much more manageable.
Focus on the lexer phase first. Your lexical phase should take the source text and give you a sequence of tokens, such as words and special characters. The lexer phase can take care of the continuation of the line and process spaces or comments as necessary. By handling spaces, a lexer can simplify the parser’s task: you can write a lexer to global{ , global { and even
global
{
all will issue two tokens: one represents global and one represents { .
Also note that the lexer can bind row and column numbers to tokens for later use if you click errors.
Once you have a good stream of tokens, work on your parsing phase. The parser should take this sequence of tokens and build an abstract syntax tree that models the syntactic structures of your document. At the moment, you should not worry about ifstream and operator>> , since the lexer should have done everything that it read for you.
You indicated an interest in the recursive call of the parsing function as soon as you see the scope. This is definitely one way. As you will see, the design decision that you will have to execute many times is whether you want to literally invoke the same parsing function recursively (for constructs such as global { global { ... } } that you can deny syntactically) or want to define a few (or even significantly) different set of syntax rules that apply within the scope.
Once you find that you need to change the rules: the key is to reuse, by refactoring in the function, as much as you can reuse between the different syntax options. If you continue to move in this direction - using separate functions that represent different pieces of syntax that you want to deal with and have them call each other (possibly recursively) where necessary - you will end up with what we call the recursive descent parser. The Wikipedia entry has a simple example: see http://en.wikipedia.org/wiki/Recursive_descent_parser .
If you really want to delve into the theory and practice of lexers and parsers, I recommend that you get a good reliable tutorial for compilers to help you. In the "Stack Overflow" section mentioned in the comments above, you will begin: Learning how to write a compiler
source share