Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #874

Re: parsing bibtex file with flex/bison

From Rudra Banerjee <bnrj.rudra@gmail.com>
Newsgroups comp.compilers
Subject Re: parsing bibtex file with flex/bison
Date 2013-03-17 00:18 +0000
Organization A noiseless patient Spider
Message-ID <13-03-013@comp.compilers> (permalink)
References <13-03-003@comp.compilers> <13-03-004@comp.compilers> <13-03-006@comp.compilers>

Show all headers | View raw


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]

Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

parsing bibtex file with flex/bison bnrj.rudra@gmail.com - 2013-03-04 15:48 -0800
  Re: parsing bibtex file with flex/bison Evangelos Drikos <drikosev@otenet.gr> - 2013-03-06 13:45 +0200
    Re: parsing bibtex file with flex/bison Rudra Banerjee <bnrj.rudra@gmail.com> - 2013-03-07 11:20 +0000
      Re: parsing bibtex file with flex/bison Rudra Banerjee <bnrj.rudra@gmail.com> - 2013-03-17 00:18 +0000
        Re: parsing bibtex file with flex/bison Torsten Eichstädt <torsten.eichstaedt@FernUni-Hagen.de> - 2013-03-25 13:41 +0100
  Re: parsing bibtex file with flex/bison glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-03-08 03:59 +0000

csiph-web