[antlr-interest] Threadsafe C parser

Jim Idle jimi at temporal-wave.com
Mon Jan 7 09:55:36 PST 2008


There are no static allocations and no threading issues with the C
stuff. @members I meant for additional functions and so on, not for
variable declarations.

 

You will find that your @context declarations appear in the per instance
structure for the compiler and not in any static area. However, the
userp variable is also another way to do it if the things you want to
instantiate are more complicated than the odd int/Boolean etc.

 

All rules and elements in the generated code have access to the ctx->
(context pointer) for the parser/tree parser, etc. 

 

See past posts for details of the @area in the C target (which should
become a wiki article really), but in short:

 

@lexer::context

{

       pMVB_PARSE_SESSION         ps;                        // MVB
compiler session context for lexer

       ANTLR3_BOOLEAN                    haveHeader;          // Set to
TRUE once we have seen a PROGRAM or SUBROUTINE

}

 

@parser::context

{

       pMVB_PARSE_SESSION  ps;                  // MVB compiler session
context for parser

}

 

In your rules you are then guaranteed to have ctx-> and can do this in
the action:

 

ctx->haveHeader = true.

 

If you use the userp element you can divorce the specifics of your
additional ‘thing’ from the grammar somewhat. You would use this when
the grammar rules and so on don’t actually care anything about what is
there. For instance maybe there is a structure you carry around for
error reporting and you need it available from the parser instance you
will be passed in the error routine. But it was just put there to give
people a choice and in general I would use the @context stuff which
works very neatly.

 

Jim

 

 

  _____  

From: John Gunnarsson [mailto:mailing.list.recipient at gmail.com] 
Sent: Sunday, January 06, 2008 2:27 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Threadsafe C parser

 

Since C isn't object oriented, declarations in the grammar's member area
is static this is not a viable option to store data and build a thread
safe parser.
Could I store data per parser somehow?
Is this the place:
http://www.antlr.org/api/C/struct_a_n_t_l_r3___b_a_s_e___r_e_c_o_g_n_i_z
_e_r__struct.html#d2f3309b604f5433e1480337b764c2da 
ANTLR3_BASE_RECOGNIZER_struct::userp ?

If so, how can I reach it from within a grammar action?

//John




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080107/4d06b3c9/attachment.html 


More information about the antlr-interest mailing list