Dear friends,
Bibtex file is a citation format, used largely with latex. A sample
bibtex entry (copied from
bibtex.org) looks like:
@article{mrx05,
Author = "Mr. X",
Title = {Something Great},
Publisher = "nob" # "ody"},
Year = 2005,
}
generally a bibtex file contains hundreds of such entry.
I am trying to parse these file and use the value of each token in a C programme.
To my knowledge, flex is the way of doing this, and I trying to do this.
Till now, I managed to write a code:
$ cat pbib.l
%{
#include <stdio.h>
#include <stdlib.h>
%}
%{
char yylval;
int YEAR;
%}
%x author
%x title
%x pub
%x year
%%
@[a-zA-Z][a-zA-Z0-9]* { printf("%s",yytext);}
[Aa]uthor= {BEGIN(author);}
<author>\"[a-zA-Z\/.]+\" { printf("%s",yytext);
BEGIN(INITIAL);}
[Yy]ear= {BEGIN(year);}
<year>\"[0-9]+\" {printf("%s",yytext);
BEGIN(INITIAL);}
[Tt]itle= {BEGIN(title);}
<title>\"[a-zA-Z\/.]+\" { printf("%s",yytext);
BEGIN(INITIAL);}
[Pp]ublisher= {BEGIN(pub);}
<pub>\"[a-zA-Z\/.]+\" { printf("%s",yytext);
BEGIN(INITIAL);}
[a-zA-Z0-9\/.-]+= printf("ENTRY TYPE ");
\" printf("QUOTE ");
\{ printf(" ");
\} printf(" ");
; printf("SEMICOLON ");
\n printf("\n");
[\,\n\}][\,\}] printf("\nNEWENTRY");
%%
Which is working fine, if I print in stdout only. Since I want to use the output of lex.yy.c,
I need have each yytext from all entry stored in array(probably). So, all yytext
for token Author should be stored in array_author[i], all from year in array_year[i] and so on.
But I have not managed to do this.
What I am currently doing is using shell script to write each entry in different file,
an again pushing C read those file...which is clearly very BAD way of doing things.
I am using flex only because I dont know any better tool, so kindly advice me if any better
way available, but for C (hence, boost is not probably a solution).
Can someone here kindly show me how can I extend my flex code so that I have those data
stored?