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

Aaron Leiby aleiby.antlr at gmail.com
Tue Jan 11 19:43:15 PST 2011


What version of ANTLR are you using?

What options does your grammar specify?

'vectors' is defined for both parsers and tree walker grammars (though it
doesn't appear to exist in the generated lexer).  You should not have to
create the factory yourself.


On Tue, Jan 11, 2011 at 7:39 AM, Jeffrey Newman <jeffn at photorepo.com> wrote:

> 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