[antlr-interest] Need help getting char * string from an ID

Jim Idle jimi at temporal-wave.com
Fri Dec 17 10:12:12 PST 2010


Use antlr.markmail.org and search for how to do this, ot used the example
gramamrs as guides. Using $ID.text->chars is easy with the C target but it
will acculate all the memory you yse for the strings until you free the
parser object (and it also isn't that fast to do that), whereas you can uuse
the commontoken fields to gind the start and end of the text that represents
the token and copy/print/etc directly from the input text.

Again, download the examples and look at the C parser in there.

jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Jeffrey Newman
> Sent: Thursday, December 16, 2010 5:01 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Need help getting char * string from an ID
> 
> Hi,
> I'm still trying to bring up my first test parser with the C runtime.
> I'm using Qt, so I have translate from C to C++ and Qt's runtime.
> 
> I have most of working, except I have not been able to get the ID
> string.
> 
> Here is the grammar:
> 
> grammar T;
> 
> options {
> 	language = C;
> }
> 
> @header {
> 	#include "../parserBindings.h"
> }
> 
> @members {
> 	char buffer[256];
> }
> 
> r : 'call' ID ';' {
> 		sprintf(buffer, "Invoked with: \%s", $ID.text);
> 		debug(buffer);
> 	} ;
> 
> ID: 'a'..'z'+ ;
> WS: (' ' | '\n' | '\r')+ {$channel=HIDDEN;};
> 
> The generated code in TParser looks like.
> 
> sprintf(buffer, "Invoked with: %s", (ID1->getText(ID1)));
> debug(buffer);
> 
> where debug is a simple function in my C to C++ bindings.
> 
> So, how do I get a string from the grammar?
> Please direct me to an example in C.
> 
> 
> thanks
> 
> 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