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


Groups > comp.compilers > #864

parsing bibtex file with flex/bison

From bnrj.rudra@gmail.com
Newsgroups comp.compilers
Subject parsing bibtex file with flex/bison
Date 2013-03-04 15:48 -0800
Organization Compilers Central
Message-ID <13-03-003@comp.compilers> (permalink)

Show all headers | View raw


I want to parse bibtex file using flex/bison. A sample bibtex is:
@Book{a1,
author="amook",
Title="ASR",
Publisher="oxf",
Year="2010",
Add="UK",
Edition="1",
}
@Article{a2,
Author="Rudra Banerjee",
Title={FeNiMo},
Publisher={P{\"R}B},
Issue="12",
Page="36690",
Year="2011",
Add="UK",
Edition="1",
}
(A new key may start in same line)
Now, I have written a flex code:

%{
#include <stdio.h>
#include <stdlib.h>
%}

%{
char yylval;
int YEAR,i;
//char array_author[1000];
%}
%x author
%x title
%x pub
%x year
%%
@          			printf("\nNEWENTRY\n");
[a-zA-Z][a-zA-Z0-9]*   		{printf("%s",yytext);
					BEGIN(INITIAL);}
author=                 	{BEGIN(author);}
<author>\"[a-zA-Z\/.]+\"  	{printf("%s",yytext);
                           		BEGIN(INITIAL);}
title=                 		{BEGIN(title);}
<title>\"[a-zA-Z\/.]+\"  	{printf("%s",yytext);
                           		BEGIN(INITIAL);}
publisher=                 	{BEGIN(pub);}
<pub>\"[a-zA-Z\/.]+\"  		{printf("%s",yytext);
                           		BEGIN(INITIAL);}
[a-zA-Z0-9\/.-]+=        printf("ENTRY TYPE ");
\"                      printf("QUOTE ");
\{                      printf("LCB ");
\}                      printf(" RCB");
;                       printf("SEMICOLON ");
\n                      printf("\n");
%%

int main(){
  yylex();
//char array_author[1000];
//printf("%d%s",&i,array_author[i]);
i++;
return 0;
}

while this is peeking up the few things, not all.
Can anyone kindly help me with this?
[My suggestion would be to do less in the lexer and more in the parser.  In the
lexer, responable tokens might be '@' '{' '}' '=' ',' word
qstring (quoted string)

Then you could write bison rules like this:

clause: '@' word '{' word ',' attrlist '}' ;

attrlist: attr | attr ',' attrlist ;

attr: name '=' value ;

value: word | qstring | nestlist :

nestlist: '{' list '}' ;

list: listitem | list listitem ;

listitem: word | qstring | nestlist :

And so forth.  This isn't exactly right, but it should get you going
in the right direction. The parser will recognize some invalid bibtex,
e.g., words that aren't attribute names, which it's easier to check in
semantic code rather than trying to stick laundry lists of keywords
into the parser. -John]

Back to comp.compilers | Previous | Next — 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