Bot Tiger wrote:
I just found out that pANTLR3_BASE_TREE->getChild() returns the same node in memory, but pANTLR3_BASE_TREE->getText() creates an entirely new pANTLR3_STRING even when called from the same node.

Does anyone have any suggestions for making this function return the same string? I thought it would've returned a single string from a lexer structure.
It does not do anything in the lexer but store the start and end points. There is no point in allocatinog memory and copying the strings and so on if you are not going to use the text, which is the case for 95% of the tokens you will parse. As explained in the docs and the examples, it always creates a new copy because I cannot know what you intend to do with it. If you take the string and then use the append functions and so on, you will probaly later require a new copy from the input stream, not the one you last played with.

However, if you really want to use the runtime strings (and you do not have to), because perhaps performance isn't that important, then you can pin the pANTLR3_STRING pointer in the token like this in the parser:

...
s=STRING_LITERAL { $s.setText($s, $s.text); }

The token will then always return the pointer you give it and not make another copy from the input stream. However, if you ask for the text of a span of tokens in a tree, then you probably will get a new string, so when you know your node has just the one token, call getText() on that token directly.

Jim



On Fri, Jan 23, 2009 at 7:22 PM, Bot Tiger <[email protected]> wrote:
Hello, I am using the C runtime and experiencing heavy memory usage.

I am interpreting an AST tree recursively.

In each interpret call, I am checking the token text by using pANTLR3_BASE_TREE->getText()->chars.
Am I suppose to manually free this when I am done with it?

Also, I am recursively calling the children nodes with pANTLR3_BASE_TREE->getChild().
Am I also supposed to free these as well? I was assuming that they returned only 1 copy of the node.

Thank you.


List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---



List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to