Re: parsing bibtex file with flex/bison

Rudra Banerjee <bnrj.rudra@gmail.com>
Sun, 17 Mar 2013 00:18:50 +0000

          From comp.compilers

Related articles
parsing bibtex file with flex/bison bnrj.rudra@gmail.com (2013-03-04)
Re: parsing bibtex file with flex/bison drikosev@otenet.gr (Evangelos Drikos) (2013-03-06)
Re: parsing bibtex file with flex/bison bnrj.rudra@gmail.com (Rudra Banerjee) (2013-03-07)
Re: parsing bibtex file with flex/bison gah@ugcs.caltech.edu (glen herrmannsfeldt) (2013-03-08)
Re: parsing bibtex file with flex/bison bnrj.rudra@gmail.com (Rudra Banerjee) (2013-03-17)
Re: parsing bibtex file with flex/bison torsten.eichstaedt@FernUni-Hagen.de (Torsten =?UTF-8?B?RWljaHN0w6RkdA==?=) (2013-03-25)
| List of all articles for this month |

From: Rudra Banerjee <bnrj.rudra@gmail.com>
Newsgroups: comp.compilers
Date: Sun, 17 Mar 2013 00:18:50 +0000
Organization: A noiseless patient Spider
References: 13-03-003 13-03-004 13-03-006
Keywords: parse, question, comment
Posted-Date: 19 Mar 2013 09:56:31 EDT

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 <stdio.h>
%}


// Symbols.
%union
{
char *sval;
};
%token <sval> VALUE
%token <sval> 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 <glib.h>
#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]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.