Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i wonder how and what is the best method to create a data tree from particular string with specific pattern into TreeView Control in c#?

For example i have input string like this :

(program (statements (statement (var_declaration (primitive_type string) (identifier a) = (expression "hi") ;)) (statement (func_declaration (predefined_fun tampilkan) ( (identifier a) ) ;))) <EOF>)


into data tree like this but in TreeView Control?

Look at this image
Look at this image

i need an advice & reference.

What I have tried:

Tried to learn about tree data structures step by step on books.
Posted
Updated 13-Apr-16 23:04pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Apr-16 1:17am    
Make your input string XML or JSON; parsers already exist.
—SA
Gun Gun Febrianza 14-Apr-16 5:06am    
so i need to convert the input string format into xml file right?
and change the contents by following xml structure?

This example uses "(" and ")" - but that's a trivial change: c# - Tree structure as string - how to match nested braces? - Stack Overflow[^]
 
Share this answer
 
Comments
Gun Gun Febrianza 14-Apr-16 2:54am    
thanks for the links. it saved my time.
OriginalGriff 14-Apr-16 3:10am    
You're welcome!
Gun Gun Febrianza 14-Apr-16 5:08am    
is there any example for input string like this :

(program (statements (statement (var_declaration (primitive_type string) (identifier a) = (expression "hi") ;)) (statement (func_declaration (predefined_fun tampilkan) ( (identifier a) ) ;))) <eof>)

btw i was visit the link & it's hard to follow..
As Sergey suggested, the "big win" here would be to get whatever is producing your data formatted as string into JSON, or XML format; parsing those formats to a Tree data structure, or creating a populated TreeView Control using those formats, is straightforward.

If you are going to try and use data in the format you show here, need to get your data into "well formed" shape. Your current data:
C#
<program>
	<var_declaration>
		<primitive type="" bita=""> 
		<identifier variable=""> 
		= 
		<expression 25=""> 
		;
	> 
  <eof>
></eof></expression></identifier></primitive></var_declaration></program>
Is obviously not formatted systematically. There are many ways you could do that; for example:
C#
<program>
	<var_declaration>
		<primitive type="" bita=""> 
		<identifier variable="">
			<expression 25=""> 
		>
	> 
></expression></identifier></primitive></var_declaration></program>
Translating this to some Tree structure is not that difficult, but, to do so, one needs to know what types of objects (classes, structs, enums, etc.) are being created in the case of using some generic Tree (not a Control). If using a Control like a TreeView, then you have to decide what kind of naming scheme you want to use for the Nodes.

I've written several types of parsers from string to Tree/TreeView over the years, and if you show you have made an effort to implement your own here, I'll be happy to respond further ... as, I am sure, other people will also. You can also find examples on the net.
 
Share this answer
 
Comments
Gun Gun Febrianza 14-Apr-16 5:02am    
do you know ANTLR 4? i am interested on domain specific language. They have prebuilt feature to parse this string into treeview control in java.

So the real input is :

(program (statements (statement (var_declaration (primitive_type string) (identifier a) = (expression "hi") ;)) (statement (func_declaration (predefined_fun tampilkan) ( (identifier a) ) ;))) <eof>)

i am still curious how to parse this string into treeview control in c#, but with my current programming skill i need painkiller, good example, step by step.

i am interested with your parser, would be happy if you share and train me :)
BillWoodruff 14-Apr-16 23:15pm    
For what I think you are trying to do here, I think learning to use ANTLR would be great.
Gun Gun Febrianza 14-Apr-16 5:20am    
here is my code now but this is not efficient and complex

if (source.Contains("program"))
{
treeView1.BeginUpdate();
treeView1.Nodes.Add("program");
treeView1.EndUpdate();

if (source.Contains("statements"))
{
treeView1.BeginUpdate();
treeView1.Nodes[0].Nodes.Add("statements");
treeView1.EndUpdate();

if (source.Contains("statement"))
{
treeView1.BeginUpdate();
treeView1.Nodes[0].Nodes[0].Nodes.Add("statement");
treeView1.EndUpdate();

if (source.Contains("var_declaration"))
{
treeView1.BeginUpdate();
treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes.Add("var_declaration");
treeView1.EndUpdate();
}
}
}
}
else if (source.Contains(""))
{

}
else
{

}
BillWoodruff 14-Apr-16 6:35am    
Hi, best if you update your original post with your revised string input sample, and your code examples. Please format the code using the CP editor tools.

I suggest you start experimenting with an actual test project in WinForms, or WPF, or whatever. Focus on parsing string.

Also, I suggest you update your original post with information on where the string you need to parse is coming from: do you have any control over the format of that string ?

And, please tag your original post to show what .NET stack you are workig with: WinForms ?

"Pain-killers," I don't have, but vitamins for people who get busy writing code and studying: have ! :)

I'd suggest that you write three different kinds of parsers in order to prepare yourself for your future as a programmer:

1. non-recursive: write a tokenizer to decompose the string into a queue, and then iterate over the queue to generate your objects.

2. non-recursive: do all the work in one iteration of the string (char by char).

3. recursive: save for last

cheers, Bill
Gun Gun Febrianza 14-Apr-16 7:45am    
noted bill thanks for advice. i will learn no matter how long it takes :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900