[antlr-interest] Need help vector in C generated code

Jim Idle jimi at temporal-wave.com
Wed Jan 12 09:19:29 PST 2011


Ah, I see your problem now.

You are trying to use the += operator but you are not producing an AST
(output=AST; option). These operators are ONLY for producing ASTs (which
you should really be doing anyway if you are doing anything other than
something very trivial). So, if you do not want to produce an AST and want
to accumulate things like that, then you need to write your own code
(though you could use the vectors, hashtables of course). Then general
rule us to take all suck code out of actions so that you call an API that
builds lists - this means your action code is just the C/C++ code that is
required to pass the structures of interest to your library routines.

You could add the output=AST option and ignore the resulting AST too, but
this would be pretty wasteful of course.

Also, if you are still learning ANTLR, then unless you don't know Java or
C# at all, I would start with one of those targets as the C target has the
steepest learning curve. This is not to cast aspersions on your C
programming skills, it is just that it is better in general to learn about
the ANTLR 'stuff' then move to the C target.

Hope that helps,

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Jeffrey Newman
> Sent: Tuesday, January 11, 2011 7:40 AM
> To: Stanley Steel
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Need help vector in C generated code
>
> First of all thanks for the code sample.
> This is a help, although I need to figure out where to put it, and how
> to call.
>
> As per your and Jim's comment that my question was not clear, I will
> try to clarify it now.
>
> (As to Jim's comment that I don't know what I'm doing.
> Well.... If I did I would not be asking for help.)
>
> On page 132 of Ter's book (pdf version)
>
> When a rule matches elements repeatedly, translators commonly need to
> build a list of these elements. As a convenience, ANTLR provides the
> += label operator that automatically adds all associated elements to an
> New in v3.
> ArrayList, whereas the = label operator always refers to the last
> element matched. The following variation of rule decl captures all
> identifiers into a list called ids for use by actions:
>
> decl: type ids+=ID (',' ids+=ID)* ';' ; // ids is list of ID tokens
>
> my rule is a little different (and simplitied)
>
> type_name
>     : name+=ID+ (LPAREN size1=signed_number (COMMA
> size2=signed_number)? RPAREN)?
>       {
>
>       }
>     ;
>
>
> The key point here being name += ID+
>
> The relevant generated code looks like:
>
>
>             	        //
> /home/jeffn/Development/antlr/trunk/edu/sqliteGui/SQLite.g:207:11:
> name+= ID
>             	        {
>             	            name = (pANTLR3_COMMON_TOKEN) MATCHT(ID,
> &FOLLOW_ID_in_type_name1275);
>             	            if  (HASEXCEPTION())
>             	            {
>             	                goto ruletype_nameEx;
>             	            }
>
>             	            if (list_name == NULL)
>             	            {
>             	                list_name=ctx->vectors->newVector(ctx-
> >vectors);                     <<<<<<------- Key point.
>             	            }
>             	            list_name->add(list_name, name, NULL);
>
>             	        }
>
> The key point here, in the line below (and marked above). is the ctx-
> >vectors.
>
>  list_name=ctx->vectors->newVector(ctx->vectors)
>
>
> There is no "vectors" element in the ctx structure.
>
> SO, MY FOLLOW UP QUESTION ARE:
> HOW DO I PROPERLY ADD THE vectors ELEMENT TO THE THE ctx STRUCTURE?
> HOW TO I INIT THE ctx ->vectors TO POINT TO MY NEWLY MINTED vectors
> function.
> AND HOW TO USE IT IN SUBSEQUENT RULES.
> (Or more precisely in my case. Since I can simply build a string and
> pass it back to the calling rule.
>   How do I access the vector's element (eg the individual string of the
> compound type name (ie var char)
>   to build a composite (concatenated) string.)
>
> --------
>
> One of my questions is why did the code generator generate code for
> functions it did not create?
>
> --------
>
> Some things that I have tried.
>
> I have looked and read, and reread the documentation.
> I have grepped and prayed over the example code and grammars.
> I have searched the email archives and tried to find some info in the
> wiki.
> I have googled everything I can think of.
> All to no avail.
>
> I noticed a lot of chatter about code hoisting and C# and global init.
> But I have to admit  I'm enough of a language guy to intricacies,
> nuances, and inter-language complications that I read about.
>
> I created a rule;
>
> vectors: ;
>
> This indeed put a vectors element in the ctx structure (I thought I was
> home free, all I would have to do was initialize with the newly minted
> vectors function that Stanley gave me.)
>
> So I added an
> @members {
> 	myVectors() {}
> }
>
> @init {
>  ctx->vectors = myVectorr;
> }
>
> And I found that the generated code did indeed contain the myVectors
> routine.
> And nothing was generated to update the ctx->vectors element.
>
> Clearly the my idea of an @init outside of a rule being executed was
> wrong.
>
> So, now I am back asking for more help.
> I hope this is more clear.
>
> Thanks in advance (or should I say part way through)
>
> Jeffrey
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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