-1

I have a string like this:

{ "\\"name\\" => \\"{ 'a', 'b', 'c' }\\"**,**  \\"age\\" => \\"{6, 7, 8 }\\" " }

It's a hstore, and for example 'a' can be a hstore to. I want to parse this string by comma in C. when parsed the output must be like that

array(
    array('name' => {'a','b','c'}, 'age' => {6, 7, 8 }) , 
    array(  ),
    array(  )...
 )
  • can't makeo sense of this. no idea... – Philip Nov 29 '11 at 09:07
  • 1
    @KirilKirov - you might like that: http://meta.stackexchange.com/questions/19756/how-do-comments-work – MByD Nov 29 '11 at 09:08
  • @KirilKirov I think you meant http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list as the question is tagged C without the double-plus – Pete Kirkham Nov 29 '11 at 09:14
  • @MByD - for the links or for something else? If for the links - thanks, I wondered what was the right syntax. – Kiril Kirov Nov 29 '11 at 09:21
  • @PeteKirkham - uh, yep, thanks. I deleted my comment. – Kiril Kirov Nov 29 '11 at 09:22
  • Your sample output was not merely parsed by commas: it respects parenthesis and quotes, which means you need a stateful parser of some kind. Several of the short references in [Learning to write a compiler](http://stackoverflow.com/q/1669/2509) would help. I suggest the Crenshaw tutorial---you'll only need to read the first few chapters and that doesn't take long. – dmckee --- ex-moderator kitten Nov 29 '11 at 16:57

3 Answers3

3

It seems to be some nested JSON format. Did you consider using a JSON parsing library, like .e.g. Jansson

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

If you want to parse by commas, strtok() is one possible option. See http://www.daniweb.com/software-development/c/threads/184836. Honestly, I can't see how parsing by comma will make this data any more intelligible, but it can be done regardless.

Chris Parton
  • 1,052
  • 9
  • 16
  • While this is a legitimate answer to the request as written ( *"I want to parse this string by comma"* ), it fails to give the usual warnings about `strtok`, and doesn't answer the question the user appears to *mean* (as evidenced by sample output which evidently respects various quotes and brackets). *Ah, I see the sample output was not introduced until after this answer existed.* – dmckee --- ex-moderator kitten Nov 29 '11 at 16:55
  • @dmckee: Yeah, that sample output wasn't there. Obviously using `strtok()` to parse by commas won't be of much use in achieving that output. – Chris Parton Nov 29 '11 at 22:18
0

Use Ragel to generate a state machine and implementation in C: http://ragel.org/

Bit of a learning curve but well worth it. Being able to visualize the output in state machines helps with debugging.

I'm currently using it to produce yet another json library. Which as mentioned in the above answer, seems to be pretty similar. Feel free to learn from and copy bits of my code.

I have the ragel code split over 3 files:

Which produces these three c++ files (You can produce C just as easily if needed):

Also, in case it is interesting here are diagrams of the state machines the ragel produces: https://github.com/matiu2/yajp/tree/master/images

matiu
  • 7,469
  • 4
  • 44
  • 48