www.delorie.com/gnu/docs/gperf/gperf_9.html   search  
 
Buy GNU books!


Perfect Hash Function Generator

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.1 struct Declarations and C Code Inclusion

The keyword input file optionally contains a section for including arbitrary C declarations and definitions, as well as provisions for providing a user-supplied struct. If the `-t' option is enabled, you must provide a C struct as the last component in the declaration section from the keyfile file. The first field in this struct must be a char * or const char * identifier called `name', although it is possible to modify this field's name with the `-K' option described below.

Here is a simple example, using months of the year and their attributes as input:

 
struct months { char *name; int number; int days; int leap_days; };
%%
january,   1, 31, 31
february,  2, 28, 29
march,     3, 31, 31
april,     4, 30, 30
may,       5, 31, 31
june,      6, 30, 30
july,      7, 31, 31
august,    8, 31, 31
september, 9, 30, 30
october,  10, 31, 31
november, 11, 30, 30
december, 12, 31, 31

Separating the struct declaration from the list of keywords and other fields are a pair of consecutive percent signs, `%%', appearing left justified in the first column, as in the UNIX utility lex.

Using a syntax similar to GNU utilities flex and bison, it is possible to directly include C source text and comments verbatim into the generated output file. This is accomplished by enclosing the region inside left-justified surrounding `%{', `%}' pairs. Here is an input fragment based on the previous example that illustrates this feature:

 
%{
#include <assert.h>
/* This section of code is inserted directly into the output. */
int return_month_days (struct months *months, int is_leap_year);
%}
struct months { char *name; int number; int days; int leap_days; };
%%
january,   1, 31, 31
february,  2, 28, 29
march,     3, 31, 31
...

It is possible to omit the declaration section entirely. In this case the keyfile begins directly with the first keyword line, e.g.:

 
january,   1, 31, 31
february,  2, 28, 29
march,     3, 31, 31
april,     4, 30, 30
...


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003