在过去的几周里,我正在尝试使用 flex 和 bison为 bibtex ( http://www.bibtex.org/Format/ ) 文件编写一个解析器。
$ 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();
}
%% 一个示例 bibtex 是:
@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",
}
当我试图解析它时,它给出了语法错误。使用 GDB,它显示它期望声明 KEY 中的字段(可能),
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".
.....
如果有人在这方面帮助我,我将不胜感激。