More C fun!

Tue, 12 October 2010

A while ago, someone on IRC asked for help with C code to parse an expression of the following form:-

1 (1 (5 (1 (5 6) 3 78) ((7 9 4)) 6) 3) 6 (78 (7 (2 4) 9 4)) 3

The parser was required to create a tree structure, with each node having child nodes and a list of numbers. I decided to try writing one as it would be a fun exercise in C.

This is what I came up with. It generates the following output for the string given above:-

O - 1 (1 (5 (1 (5 6) 3 78) ((7 9 4)) 6) 3) 6 (78 (7 (2 4) 9 4)) 3 -> 3 6 1 
|  
+--O - 78 (7 (2 4) 9 4) -> 78 
|  |  
|  +--O - 7 (2 4) 9 4 -> 4 9 7 
|  |  |  
|  |  +--O - 2 4 -> 4 2 
|  |  |  |  
|  |  |  *
|  |  *
|  *
+--O - 1 (5 (1 (5 6) 3 78) ((7 9 4)) 6) 3 -> 3 1 
|  |  
|  +--O - 5 (1 (5 6) 3 78) ((7 9 4)) 6 -> 6 5 
|  |  |  
|  |  +--O - (7 9 4) -> 
|  |  |  |  
|  |  |  +--O - 7 9 4 -> 4 9 7 
|  |  |  |  |  
|  |  |  |  *
|  |  |  *
|  |  +--O - 1 (5 6) 3 78 -> 78 3 1 
|  |  |  |  
|  |  |  +--O - 5 6 -> 6 5 
|  |  |  |  |  
|  |  |  |  *
|  |  |  *
|  |  *
|  *
*

The output shows the tree structure that the parser generates. For each node, the actual substring of the original string and the list of numbers corresponding to that node are printed. This is repeated for each child node.

Back to posts

Menu

Home
Log
Biography
Projects
Music
Art

About
Contact
GitHub

Links

Ogre
Blender
Bullet
Arch

Unix is user-friendly. It's just very selective about who its friends are.

- Anonymous