1
最近幾周,我試圖用flex和bison爲bibtex(http://www.bibtex.org/Format/)文件編寫解析器。用flex + bison解析bibtex:重溫
$ 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();
}
%% 樣本中文提供是:
@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,它表明它希望在重點領域中聲明(可能),
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".
.....
我會很感激,如果有人好心幫我在這。
嗨克里斯, 感謝您的幫助。它的工作,我已經更新了它,即使是一般情況下(儘管如此,不要認爲它是完整的)。 – BaRud 2013-03-10 11:27:26