Question

I have a couple of questions about C target of antlr. (I am using libantlr3c-3.4)

  1. Since there is no garbage collection, I have to clean up the memory myself, so I want to throw away all parser data after my data structures are populated and parsing is completed. Is there a simple way to delete the entire parser memory, instead of walking through each and every object and deleting it explicitly? (I noticed a comment in antlr3string.h that this is possible, but I could not find a code example of how to do it.)

  2. My parser is working fine when the input is in correct syntax. But when the input syntax is wrong, it reports an error and gives a segmentation fault. I guess this is because there is no catch-throw of exceptions in C (unlike java). How to make the exit graceful in such situations? (I saw an answer on this topic - 4751699 - but that was more than 2 years ago and an older version of antlr .. just wanted to confirm if that answer is still valid, or some other stuff has to be done.)

Was it helpful?

Solution

Cleanup after you are done is simple unless you are manually creating own structures. All what's needed is:

  pANTLR3_INPUT_STREAM _input;
  pMySQLLexer _lexer;
  pANTLR3_COMMON_TOKEN_STREAM _tokens;
  pMySQLParser _parser;
  MySQLParser_query_return _ast;

  _parser->free(_parser);
  _tokens ->free(_tokens);
  _lexer->free(_lexer);
  _input->close(_input);

No need to free the tree in the stored ast, since the nodes are from a pool that gets freed when you free the parser.

For the invalid input: there must be something wrong in your error handler. ANTLR doesn't throw an exception if the input is wrong. See where the exception comes from. You are probably accessing an element that you think exist but doesn't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top