[antlr-interest] C target help

Ilyevsky, Leonid (Equity Trading) Leonid_Ilyevsky at ml.com
Tue Dec 16 16:27:38 PST 2008


 
Jim,

I found another issue. I am just following the example, parsing the
string, getting the tree and trying to print it out. The code is below.

The program dies at the line

cout << "Tree: " << ast.tree->toStringTree(ast.tree)->chars << endl;

As I put some debugging in the antlr core code, I see that the problem
is corrupted token object.
Here is my test output:

-----------------------------------------------
QUERY: select from TestCache where X = 123
Init parser
About to parse
Parsing done
Parser succeeded
src/antlr3basetree.c:255
src/antlr3basetree.c:269
src/antlr3commontree.c:423
src/antlr3commontree.c:434
src/antlr3commontoken.c:346
src/antlr3commontoken.c:367
Token: Tz Start: -1612541768 End: -1612541760
Process died unexpectedly .....
-----------------------------------------------

I am not sure what should be the start and stop number in the token, but
those huge negative numbers do not look right.





==========================================

/* 
 * File:   QueryProcessor.h
 * Author: lilyevsk
 *
 * Created on December 16, 2008, 9:33 AM
 */

#ifndef _QUERYPROCESSOR_H
#define	_QUERYPROCESSOR_H

#include <c8queryLexer.h>
#include <c8queryParser.h>

class QueryProcessor {
public:

    void testQuery(const char * query) {
        cout << __FILE__ << ":" << __LINE__ << endl;
        pANTLR3_INPUT_STREAM input;
        pc8queryLexer lxr;
        pANTLR3_COMMON_TOKEN_STREAM qstream;
        pc8queryParser psr;

        try {
            cout << __FILE__ << ":" << __LINE__ << endl;

            //ANTLR3_STRING_FACTORY factory;

            cout << "QUERY: " << query << endl;

            //pANTLR3_STRING queryStr = factory.newStr8(&factory,
(pANTLR3_UINT8) query);

            input = antlr3NewAsciiStringInPlaceStream((pANTLR3_UINT8)
query, strlen(query), NULL);

            lxr = c8queryLexerNew(input);

            if (lxr == NULL) {
                cerr << "Unable to create the lexer due to malloc()
failure" << endl;
                return;
            }
            qstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT,
TOKENSOURCE(lxr));

            if (qstream == NULL) {
                cerr << "Out of memory trying to allocate token stream"
<< endl;
                return;
            }

            cout << "Init parser" << endl;

            psr = c8queryParserNew(qstream);

            cout << "About to parse" << endl;

            c8queryParser_statement_return ast = psr->statement(psr);

            cout << "Parsing done" << endl;

            if (psr->pParser->rec->state->errorCount > 0) {
                cerr << "The parser returned " <<
                        psr->pParser->rec->state->errorCount <<
                        " errors, tree walking aborted." << endl;
                return;

            } else {
                cout << "Parser succeeded" << endl;
            }

            cout << "Tree: " << ast.tree->toStringTree(ast.tree)->chars
<< endl;

            pANTLR3_COMMON_TREE_NODE_STREAM nodes =
                    antlr3CommonTreeNodeStreamNewTree(ast.tree,
ANTLR3_SIZE_HINT);

            cout << "Initialized nodes" << endl;
                       
            
            nodes ->free(nodes);
            nodes = NULL;
            

            // Cleanup
            psr ->free(psr);
            psr = NULL;
            qstream ->free(qstream);
            qstream = NULL;
            lxr ->free(lxr);
            lxr = NULL;
            input->reset(input);

            cout << __FILE__ << ":" << __LINE__ << endl;

        } catch (exception& e) {
            cerr << "exception: " << e.what() << endl;
        }

    }
};


#endif	/* _QUERYPROCESSOR_H */

==========================================

--------------------------------------------------------------------------
This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.
--------------------------------------------------------------------------
 


More information about the antlr-interest mailing list