[antlr-interest] Adding custom functions to the parser in a C target

Andi Clemens andi.clemens at gmx.net
Tue Mar 30 22:51:06 PDT 2010


Hi,

I want to use the PLSQL grammar from antlr.org with some modifications to 
detect table names in statements. I want to check, whenever I hit the 
"table_spec" rule, if this table name is in a whitelist and perform further 
actions.

I have a problem now: How can I add global variables to the parser?
I need to give the parser a pointer to the whitelist in memory and some other 
variables for detecting valid / invalid table names.

Defining variables in @members doesn't help, I am not able to access this from 
outside of the parser code.
In Java it seems to be much easier, since you have a class where you can add 
public members, but in C I have no clue how to do it (yes, I need to use C, 
and I never programmed in that language, only in C++ and Python so far).

I want to do something like this.
In the parser rule "table_spec", I want to check the table name:
{
    char result[256];
    strcat(result, s.tree ? s.tree->getText(s.tree)->chars : "");
    strcat(result, s.tree ? "." : "");
    strcat(result, t.tree ? t.tree->getText(t.tree)->chars : "");
    isValid = checkForValidTable(result);
}

"isValid" and "checkForValidTable" are defined in @members, but the check 
function needs some statement handlers and other stuff coming from the 
outside. In the end, I wanted to have something like this in my main.cpp:

[...]
parser->setStmtHandle(some pointer);
parser->setWhiteListHandle(some pointer);
parser->sql_statement(parser); // this is the main function I would use from 
the PL/SQL grammar file
bool isValid = parser->isValid();
[...]


Is it possible to do something like that? If not, how can I fix this problem?
I need to get those handles inside of the parser somehow? 
Any ideas?

Andi


More information about the antlr-interest mailing list