我在解析 bib 文件方面取得了相当大的进展,但下一步对于我目前的理解水平来说相当困难。我已经创建了野牛和 flex 代码,可以正确解析上面的 bib 文件:
%{
#include <stdio.h>
%}
// Symbols.
%union
{
char *sval;
};
%token <sval> VALUE
%token <sval> KEY
%token OBRACE
%token EBRACE
%token QUOTE
%token SEMICOLON
%start Input
%%
Input:
/* empty */
| Input Entry ; /* input is zero or more entires */
Entry:
'@' KEY '{' KEY ','{ printf("===========\n%s : %s\n",$2, $4); }
KeyVals '}'
;
KeyVals:
/* empty */
| KeyVals KeyVal ; /* zero or more keyvals */
KeyVal:
KEY '=' VALUE ',' { printf("%s : %s\n",$1, $3); };
%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}
int main(void) {
yyparse();
}
和
%{
#include "bib.tab.h"
%}
%%
[A-Za-z][A-Za-z0-9]* { yylval.sval = strdup(yytext); return KEY; }
\"([^\"]|\\.)*\"|\{([^\"]|\\.)*\} { yylval.sval = strdup(yytext); return VALUE; }
[ \t\n] ; /* ignore whitespace */
[{}@=,] { return *yytext; }
. { fprintf(stderr, "Unrecognized character %c in input\n", *yytext); }
%%
我想将这些值放在容器中。在过去的几天里,我阅读了大量的 glib 文档,并提出了最适合我的情况的哈希容器。下面是一个基本的哈希码,一旦将值放入数组键和 vals 中,它就会正确地得到哈希值。
#include <glib.h>
#define slen 1024
int main(gint argc, gchar** argv)
{
char *keys[] = {"id", "type", "author", "year",NULL};
char *vals[] = {"one", "Book", "RB", "2013", NULL};
gint i;
GHashTable* table = g_hash_table_new(g_str_hash, g_str_equal);
GHashTableIter iter;
g_hash_table_iter_init (&iter, table);
for (i= 0; i<=3; i++)
{
g_hash_table_insert(table, keys[i],vals[i]);
g_printf("%d=>%s:%s\n",i,keys[i],g_hash_table_lookup(table,keys[i]));
}
}
问题是,我如何整合这两个代码,即在 C 代码中使用解析后的数据。任何帮助表示赞赏。
编辑:解释@UncleO 的回复:@UncleO,
谢谢你的评论。我不知道如何更好地解释它。这是一个尝试。我的代码(野牛)的最新状态是:
%{
#include <stdio.h>
#include <glib.h>
%}
// Symbols.
%union
{
char *sval;
};
%token <sval> VALUE
%token <sval> KEY
%token OBRACE
%token EBRACE
%token QUOTE
%token SEMICOLON
%start Input
%%
Input:
/* empty */
| Input Entry ; /* input is zero or more entires */
Entry:
'@' KEY '{' KEY ','{ printf("===========\n%s : %s\n",$2, $4); }
KeyVals '}'
;
KeyVals:
/* empty */
| KeyVals KeyVal ; /* zero or more keyvals */
KeyVal:
KEY '=' VALUE ',' { printf("%s : %s\n",$1, $3); };
%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}
int main(void) {
GHashTable* table = g_hash_table_new(g_str_hash, g_str_equal);
char *keys[] = {"id", "type", "author", "year",NULL};
char *vals[] = {"one", "Book", "RB", "2013", NULL};
gint i;
yyparse();
GHashTableIter iter;
g_hash_table_iter_init (&iter, table);
for (i= 0; i<=3; i++)
{
g_hash_table_insert(table, keys[i],vals[i]);
g_printf("%d=>%s:%s\n",i,keys[i],g_hash_table_lookup(table,keys[i]));
}
}
lex 文件不变。数组键和值的元素用于测试目的。输入文件的一个例子是
@Booklet{ab19,
Author="Rudra Banerjee and A. Mookerjee",
Editor="sm1",
Title="sm2",
Publisher="sm3",
Volume="sm4",
Issue="sm5",
Page="sm6",
Month="sm8",
Note="sm9",
Key="sm10",
Year="1980",
Add="osm1",
Edition="osm2",
}
因此,在解析时,代码会正确解析该值。我想使用解析输入中的这些值插入到哈希表中,这对于每个输入都是不同的。所以,我的最终目标是从代码中删除数组键和值;和线
g_hash_table_insert(table, keys[i],vals[i]);
应该用类似的东西代替:
g_hash_table_insert(table, <$1 from bison>,<$3 from bison>);
这有意义吗?
编辑:=======================================
@Uncle0:这是更新的代码;可能我的意图很明确。我正在努力解决这个问题,但是虽然从野牛行打印按预期打印东西,但从哈希表打印时并非如此(代码的最后一行)
%{
#include <stdio.h>
#include <glib.h>
#define slen 1024
GHashTable* table;
%}
// Symbols.
%union
{
char *sval;
};
%token <sval> VALUE
%token <sval> KEY
%token OBRACE
%token EBRACE
%token QUOTE
%token SEMICOLON
%start Input
%%
Input:
/* empty */
| Input Entry ; /* input is zero or more entires */
Entry:
'@' KEY '{' KEY ','{ g_hash_table_insert(table, "TYPE", $2);
g_hash_table_insert(table, "ID", $4);
g_printf("%s: %s\n", $2, $4);
}
KeyVals '}'
;
KeyVals:
/* empty */
| KeyVals KeyVal ; /* zero or more keyvals */
KeyVal:
KEY '=' VALUE ',' { g_hash_table_insert(table, $1, $3);
g_printf("%s: %s\n", $1, $3); };
%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}
int main(void) {
table = g_hash_table_new(g_str_hash, g_str_equal);
gint i;
do{
g_hash_table_remove_all (table);
yyparse();
parse_entry (table);
// g_printf("%s:%s\n","Author=>",g_hash_table_lookup(table,"Author"));
// g_printf("%s:%s\n","KEY=>",g_hash_table_lookup(table,"KEY"));
}
while(!EOF);
}
void parse_entry (GHashTable *table)
{
GHashTableIter iter;
gchar *key, *val;
char *keys[] = {"id", "type", "author", "year", "title", "publisher", "editor",
"volume", "number", "pages", "month", "note", "address", "edition", "journal",
"series", "book", "chapter", "organization", NULL};
char *vals[] = {NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL};
gchar **kiter;
gint i;
g_hash_table_iter_init (&iter, table);
while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
{
for (kiter = keys, i = 0; *kiter; kiter++, i++)
{
if (!g_ascii_strcasecmp(*kiter, key))
{
vals[i] = g_strndup(val,slen);
break;
}
g_printf("%d=>%s:%s\n",i,keys[i],vals[i]);
}
}
}