For last few weeks, I am trying to write a parser for bibtex (http://www.bibtex.org/Format/) file using flex and bison.
$ cat raw.l
%{
#include "raw.tab.h"
%}
value [\"\{][a-zA-Z0-9 .\t\{\} \"\\]*[\"\}]
%%
[a-zA-Z]* return(KEY);
\" return(QUOTE);
\{ return(OBRACE);
\} return(EBRACE);
; return(SEMICOLON);
[ \t]+ /* ignore whitespace */;
{value} {
yylval.sval = malloc(strlen(yytext));
strncpy(yylval.sval, yytext, strlen(yytext));
return(VALUE);
}
$ cat raw.y
%{
#include <stdio.h>
%}
//Symbols.
%union
{
char *sval;
};
%token <sval> VALUE
%token KEY
%token OBRACE
%token EBRACE
%token QUOTE
%token SEMICOLON
%start Entry
%%
Entry:
'@'KEY OBRACE VALUE ','
KeyVal
EBRACE
;
KeyVal:
/* empty */
| KeyVal '=' VALUE ','
| KeyVal '=' VALUE
;
%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}
int main(void) {
yyparse();
}
%% A sample bibtex is:
@Book{a1,
author = "a {\"m}ook, Rudra Banerjee",
Title="ASR",
Publisher="oxf",
Year="2010",
Add="UK",
Edition="1",
}
@Article{a2,
Author="Rudra Banerjee",
Title="Fe{\"Ni}Mo",
Publisher={P{\"R}B},
Issue="12",
Page="36690",
Year="2011",
Add="UK",
Edition="1",
}
When I am trying to parse it, its giving syntax error. with GDB, it shows it expect fields in KEY to be declared(probably),
Reading symbols from /home/rudra/Programs/lex/Parsing/a.out...done.
(gdb) Undefined command: "". Try "help".
(gdb) Undefined command: "Author". Try "help".
(gdb) Undefined command: "Editor". Try "help".
(gdb) Undefined command: "Title". Try "help".
.....
I will be grateful if someone kindly help me on this.