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

Andi Clemens andi.clemens at gmx.net
Wed Mar 31 13:40:08 PDT 2010


Hi,

thank you... I will take a look at your example, maybe I can use this.

Andi

On Wednesday 31 March 2010 16:56:05 Jim Idle wrote:
> No it isn't in case you want to encapsulate it elsewhere. However for
> context level elements you want to use the mechanism I established and not
> hijack super really (though super is meant for your own stuff).
> 
> So:
> 
> 
> @parser::context
> {
> // A pointer to the blah that we blargle into a gargle
> //
> pBLAH                   papi;
> }
> 
> @parser::apifuncs
> {
> 	ctx->pBLAH = NULL;
> }
> 
> @parser::includes
> {
> // Include the BLAH interface specifications, so we know how to call
> // an implementation to have it do work for us.
> //
> #include <blah.h>
> 
> // Create a macro to make references to the BLAH pointer easier
> //
> #define BLAH CTX->pBLAH
> }
> 
> 
> Now in your actions you can use:
> 
> {
>    BLAH->number = 666;
>    BLAH->myCall(BLAH, 666);
> 
>    // And so on...
> 
> }
> 
> 
> You use the context pointers because 'global' data is exactly what you
> don't want as you instantly destroy the free threading, which is built
> into the code generation and the runtime. Here you get one pBLAH per
> thread:
> 
> psr	    = MyParserNew(tstream);
> 
>     if (psr == NULL) ...
> 
>     // Install a BLAH interface
>     //
>     psr->blah   = getBlah();
>     if  (psr->blah == NULL) ...
> 
>     if  ((retCode = psr->blah->blahInitialize(psr->blah)) != 0)...
> 
> 
> Jim
> 
> > -----Original Message-----
> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> > bounces at antlr.org] On Behalf Of Christopher L Conway
> > Sent: Wednesday, March 31, 2010 7:36 AM
> > To: antlr-interest at antlr.org
> > Subject: Re: [antlr-interest] Adding custom functions to the parser in
> > a C target
> > 
> > Andi,
> > 
> > I think you'll find the void* field super in ANTLR3_PARSER_struct,
> > accessible in the grammar via PARSER->super, is a good place to stick
> > implementation data. AFAICT, it is never set by the generated code.
> > 
> > -Chris
> > 
> > On Wed, Mar 31, 2010 at 1:51 AM, Andi Clemens <andi.clemens at gmx.net>
> > 
> > wrote:
> > > 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
> > > 
> > > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > > Unsubscribe: http://www.antlr.org/mailman/options/antlr-
> > 
> > interest/your-email-address
> > 
> > 
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> > email-address
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list