Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!news.ripco.com!rahul.net!wasp.rahul.net!rahul.net!news.misty.com!news.iecc.com!.POSTED!nerds-end From: Rudra Banerjee Newsgroups: comp.compilers Subject: Re: parsing bibtex file with flex/bison Date: Sun, 17 Mar 2013 00:18:50 +0000 Organization: A noiseless patient Spider Lines: 96 Sender: johnl@iecc.com Approved: comp.compilers@iecc.com Message-ID: <13-03-013@comp.compilers> References: <13-03-003@comp.compilers> <13-03-004@comp.compilers> <13-03-006@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Trace: leila.iecc.com 1363701391 71813 64.57.183.58 (19 Mar 2013 13:56:31 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Tue, 19 Mar 2013 13:56:31 +0000 (UTC) Keywords: parse, question, comment Posted-Date: 19 Mar 2013 09:56:31 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:874 I have managed considerable progress in parsing the bib file, but the next step is quite tough for my present level of understanding. I have created bison and flex code, that parses the bib file correctly (some exceptions may still be there): $cat bib.y %{ #include %} // Symbols. %union { char *sval; }; %token VALUE %token KEY %token OBRACE %token EBRACE %token QUOTE %token SEMICOLON %start Input %% Input: /* empty */ | Input Entry ; /* input is zero or more entires */ Entry: '@' KEY '{' KEY ','{ printf("===========\n%s : %s\n",$2, $4); } KeyVals '}' ; KeyVals: /* empty */ | KeyVals KeyVal ; /* zero or more keyvals */ KeyVal: KEY '=' VALUE ',' { printf("%s : %s\n",$1, $3); }; %% int yyerror(char *s) { printf("yyerror : %s\n",s); } int main(void) { yyparse(); } and $cat bib.l %{ #include "bib.tab.h" %} %% [A-Za-z][A-Za-z0-9]* { yylval.sval = strdup(yytext); return KEY; } \"([^\"]|\\.)*\"|\{([^\"]|\\.)*\} { yylval.sval = strdup(yytext); return VALUE; } [ \t\n] ; /* ignore whitespace */ [{}@=,] { return *yytext; } . { fprintf(stderr, "Unrecognized character %c in input\n", *yytext); } %% I want to have those values in a container. For last few days, I read the vast documentation of glib and came out with hash container as most suitable for my case. Below is a basic hash code, where it have the hashes correctly, once the values are put in the array keys and vals. #include #define slen 1024 int main(gint argc, gchar** argv) { char *keys[] = {"id", "type", "author", "year",NULL}; char *vals[] = {"one", "Book", "RB", "2013", NULL}; gint i; GHashTable* table = g_hash_table_new(g_str_hash, g_str_equal); GHashTableIter iter; g_hash_table_iter_init (&iter, table); for (i= 0; i<=3; i++) { g_hash_table_insert(table, keys[i],vals[i]); g_printf("%d=>%s:%s \n",i,keys[i],g_hash_table_lookup(table,keys[i])); } } The problem is, how I integrate this two code, i.e. put the $1, $3 of " KEY '=' VALUE ',' { printf("%s : %s\n",$1, $3); };" in the hash table. Any kind help is appreciated. [If you want to put it in the hash table, you do so, something like g_hash_table_insert(table, $1, $3) -John]