[antlr-interest] Need help printing a tree from the C runtime.

Jeffrey Newman jeffn at photorepo.com
Sat Dec 18 06:12:06 PST 2010


Hi,

I'm trying to implement the sample code, in C (C++), on page 156 of Ter's book.

I've looked for the samples Jim referred to in a previous mail.
I found a lot of C grammar samples but nothing about how to use the C API.
And, found the doxygen documentation just about worthless.

Fortunately, the code is very well written, and it looks like Jim pretty
much followed the C++ to C convention of the object itself is the this pointer,
and the class is really a structure (with a few more semantics of course). So,
as far as I can tell, read the source is the documentation.

That being said I have gotten this far. The code below compiles, and runs up to
the trying to print the string. All I get an empty string. I think I am doing something
wrong trying to create the token (e.g. the cast of a string to a pANTRLR3_UINT8).

Can some one tell what I am doing wrong, and how to fix things so I can see the tree.

thanks

Jeffrey



pANTLR3_STRING_FACTORY stringFactory = antlr3StringFactoryNew();
pANTLR3_BASE_TREE_ADAPTOR adaptor    = ANTLR3_TREE_ADAPTORNew(stringFactory);
pANTLR3_BASE_TREE list               = (pANTLR3_BASE_TREE) adaptor->nilNode(adaptor);

pANTLR3_COMMON_TOKEN xToken = adaptor->createToken(adaptor, myID, (pANTLR3_UINT8)("x"));
list->addChild((pANTLR3_BASE_TREE) list, xToken);

pANTLR3_COMMON_TOKEN yToken = adaptor->createToken(adaptor, myID, (pANTLR3_UINT8)("y"));
list->addChild((pANTLR3_BASE_TREE) list, yToken);

pANTLR3_COMMON_TOKEN zToken = adaptor->createToken(adaptor, myID, (pANTLR3_UINT8)("z"));
list->addChild((pANTLR3_BASE_TREE) list, zToken);

pANTLR3_STRING pAntlrString = list->toStringTree((pANTLR3_BASE_TREE) list);
char *pStr = (char *) pAntlrString->chars;



More information about the antlr-interest mailing list