ios - JSON parsing using Lemon (and Core Foundation) -
i'm trying write simple json parser using lemon , apple core foundation.
here's code far:
%include { #import <corefoundation/corefoundation.h> #import "state.h" // struct parserstate { cftyperef result; }; #import "tuple.h" // struct tuple { cftyperef one; cftyperef two; }; } %start_symbol json %token_type { cftyperef } %token_prefix t %extra_argument { parserstateref state } %type simple_value { cftyperef } %type fellow member { tupleref } %type members { cfmutabledictionaryref } %type object { cfmutabledictionaryref } %type array { cfmutablearrayref } simple_value(a) ::= string(b). { = b; } simple_value(a) ::= int(b). { a = b; } simple_value(a) ::= float(b). { = b; } simple_value(a) ::= false. { = kcfbooleanfalse; } simple_value(a) ::= true. { = kcfbooleantrue; } simple_value(a) ::= null. { = kcfnull; } member(a) ::= string(b) colon simple_value(c). { = tuplecreate(b,c); } fellow member ::= string colon object. fellow member ::= string colon array. members(a) ::= member(b). { = cfdictionarycreatemutable(kcfallocatordefault,0,&kcftypedictionarykeycallbacks,&kcftypedictionaryvaluecallbacks); cfdictionarysetvalue(a, b->first, b->second); cfrelease(b->first); cfrelease(b->second); tuplerelease(b); } members(a) ::= members(b) comma member(c). { cfdictionarysetvalue(b, c->first, c->second); cfrelease(c->first); cfrelease(c->second); tuplerelease(c); = b; } values ::= value. values ::= values comma value. object(a) ::= lcb rcb. { /* never gets called */ = cfdictionarycreatemutable(kcfallocatordefault,0,&kcftypedictionarykeycallbacks,&kcftypedictionaryvaluecallbacks); } object(a) ::= lcb members(b) rcb. { /* never gets called */ = b; } array ::= lsb rsb. array ::= lsb values rsb. value ::= array. value ::= object. value ::= simple_value. json ::= object(a). { state->result = a; } json ::= array.
with simple json this
{ \"hello\" : \"world\" }
i can't go past members rule (at point, dictionary set correctly).
the object rule never called, , json ::= object same!
am doing stupid?
any input appreciated!
you have phone call parser(...) function 0 value sec parameter when tokens stream run out.
information from: https://www.sqlite.org/src/doc/trunk/doc/lemon.html
01 parsetree *parsefile(const char *zfilename){ 02 tokenizer *ptokenizer; 03 void *pparser; 04 token stoken; 05 int htokenid; 06 parserstate sstate; 07 08 ptokenizer = tokenizercreate(zfilename); 09 pparser = parsealloc( malloc ); 10 initparserstate(&sstate); 11 while( getnexttoken(ptokenizer, &htokenid, &stoken) ){ 12 parse(pparser, htokenid, stoken, &sstate); 13 } 14 parse(pparser, 0, stoken, &sstate); 15 parsefree(pparser, free ); 16 tokenizerfree(ptokenizer); 17 homecoming sstate.treeroot; 18 }
see line 14. says parser should not expect more tokens , can perform remained rules.
basically, programme has utilize lemon-generated parser first create parser, send lots of tokens obtained tokenizing input source. when end of input reached, parse() routine should called 1 lastly time token type of 0. step necessary inform parser end of input has been reached. finally, reclaim memory used parser calling parsefree().
ios core-foundation lemon
No comments:
Post a Comment