c++cgperf

Can gperf accept keywords defined by macros?


I would like something like the following gperf input file:

%{
#include <keywords.h>
// the contents of which contain
// #define KEYWORD1_MACRO "keyword1"
// #define KEYWORD2_MACRO "keyword2"
%}
%%
KEYWORD1_MACRO
KEYWORD2_MACRO
%%

Unfortunately, gperf will interpret those as the stings "KEYWORD1_MACRO", etc.

The reason for this is that I have a protocol spec provided by another party as a header file, containing such #defines. So I don't have control over how they are defined, and I'd rather not have to write another preprocessing tool to #include the header and output the expansion of the macros as quoted strings, only then for use as a gperf input file.


Solution

  • I made the following experiment which uses gcc -E to deal with those #includes.

    keywords.h:

    #define KEYWORD1_MACRO "keyword1"
    #define KEYWORD2_MACRO "keyword2"
    

    test.c:

    %{
    #include "keywords.h"
    %}
    %%
    KEYWORD1_MACRO
    KEYWORD2_MACRO
    %%
    

    Command: gcc -E -o test.out.txt test.c.
    Then, the content in test.out.txt:

    # 1 "test.c"
    # 1 "<built-in>"
    # 1 "<command line>"
    # 1 "test.c"
    %{
    # 1 "keywords.h" 1
    # 3 "test.c" 2
    %}
    %%
    "keyword1"
    "keyword2"
    %%
    

    The #includes are handled automatically. You can then do some text processing and feed into gperf.