Next: , Previous: Declarations, Up: Declarations


3.1.1.1 User-supplied struct

If the ‘-t’ option (or, equivalently, the ‘%struct-type’ declaration) is enabled, you must provide a C struct as the last component in the declaration section from the input file. The first field in this struct must be of type char * or const char * if the ‘-P’ option is not given, or of type int if the option ‘-P’ (or, equivalently, the ‘%pic’ declaration) is enabled. This first field must be called ‘name’, although it is possible to modify its name with the ‘-K’ option (or, equivalently, the ‘%define slot-name’ declaration) described below.

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

     struct month { 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.

If the struct has already been declared in an include file, it can be mentioned in an abbreviated form, like this:

     struct month;
     %%
     january,   1, 31, 31
     ...