Help needed for parsing algorithm

Started by
3 comments, last by Zahlman 14 years ago
I'm trying to find a clever way to parse the following text file (well, a part of it) so that it would be organized in nodes (i'd like to end up with a c# treeview).

version // NODE
        {
        number 8.2
	}
morphBinaryFile grizzly.pmd // NODE
figureResFile Grizzly.obj  // NODE
actor BODY:3  // NODE
	{

	}
actor BODY:3 // NODE
        {
	name    BODY
	on
	bend 1
	dynamicsLock		0
	hidden		0
	addToMenu	1
	castsShadow		1
	includeInDepthCue		1
	useZBuffer		1
	parent UNIVERSE
	creaseAngle 80
	channels // NODE
		{
		groups // NODE
			{
			groupNode Transform  // NODE
				{
				parmNode Scale
				parmNode xScale
				parmNode yScale
				parmNode zScale
				parmNode Twist
				parmNode Bend
				parmNode Side-Side
				parmNode xTran
				parmNode yTran
				parmNode zTran
				}
			groupNode Other  // NODE
				{
				parmNode Roar
				}
			}
                 }
        }

As you can see each node section is included in a {} block. But since the file structure expands to several levels of recursion, I failed to find a way to build some code that would, for example, create an array where, for each text line, would store the parent node. I'm really stuck so any help is greatly appreciated.
Advertisement
Have a look at boost spirit which great for parsing files.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Thanks but too complicate.
Recursion is one easy way of solving your problem...
void Node::read(){  string line = readline().trim();  if( line == "}" ) return;  if( line == "{" )  {    Node node = new Node(this); // set parent    node.read(); // recursive call    add(node);  }  else  {    add(new Attribute(line));  }}


[Edited by - sirGustav on April 14, 2010 12:03:43 AM]
What language are you using? (And why?)

This topic is closed to new replies.

Advertisement