ANTLR 3: passing value to Lexer (C target)

71 views Asked by At

Is there a mechanism to pass a value to a lexer? (I'm working with C target in ANTLR 3)

Some other search results had suggested putting a function and var into the member area:

@members
{
    bool read_flag;
    void set_flag(bool b) {read_flag = b;}
}

however, that does not seem to work. The set_flag() is a global for the lexer, but not able to be called from outside

I want to be able to do something like this in the calling code:

//some input stream
pANTLR3_INPUT_STREAM input =
    antlr3NewAsciiStringInPlaceStream((pANTLR3_UINT8)buf, len, NULL);

pmyLexer lxr = myLexerNew(input);
lxr->set_flag(true);
1

There are 1 answers

0
Mike Lischke On

You can use the user pointer for that, which has been added exactly for this purpose:

lexer->pLexer->rec->state->userp = &context;

In my lexer I use this to store a reference to my RecognitionContext structure, which I then access via macros in my grammar:

#define PAYLOAD ((RecognitionContext*)RECOGNIZER->state->userp)->payload
#define SERVER_VERSION ((RecognitionContext*)RECOGNIZER->state->userp)->version

The structure is defined like this:

typedef struct {
    long version;
    void *payload;
    ...
} RecognitionContext;