[antlr-interest] Does ANTLR use Adaptor to walk the tree?

Yau Kwong K (Tony) yau at LGSInnovations.com
Tue Apr 22 08:53:54 PDT 2008


Hi all,

 

I currently have a tree parser that builds the AST and a tree walker
which walks the AST (under C Target)

I also have a treeAdaptor which I would like to experiment to see how I
can use to talk to the tree walker. 

 

I've noticed when it builds the tree, the treeAdaptor is used heavily.
However, when I walk the tree (with tree grammar), the adaptor is not
used at all (only adaptorNew is called).  If this is true, how do I
extend the tree walker so that it knows how to walk my own tree with
custom process I would like to define?  In my application, I may not
have the tree builder to start with. 

 

Thanks

Tony

 

 

#include <ASTBuildParser.h>

#include <ASTBuildLexer.h>

#include <TreeWalk.h>

#include <Mytreeadaptor.h>

 

// Main entry point for this example

//

int ANTLR3_CDECL

main     (int argc, char *argv[])

{

            pANTLR3_UINT8
fName;

            pANTLR3_INPUT_STREAM                                input;

            pASTBuildLexer
lxr;

            pANTLR3_COMMON_TOKEN_STREAM             tstream;

            pASTBuildParser
psr;

            pANTLR3_BASE_TREE_ADAPTOR                    myAdaptor;

            ASTBuildParser_prog_return
parser_ast;

            pANTLR3_COMMON_TREE_NODE_STREAM nodes;

            pTreeWalk
tw;

 

printf("--- TREE BUILDING ---\n");

     

            if (argc < 2 || argv[1] == NULL)

                        fName = (pANTLR3_UINT8) "./input";

            else

                        fName  = (pANTLR3_UINT8)argv[1];

            

            input     = antlr3AsciiFileStreamNew(fName);

 

            if ( input == NULL )

            {

printf("Unable to open file %s due to malloc() failure1\n", (char
*)fName);

            }

 

            lxr             = ASTBuildLexerNew(input);       

 

            if ( lxr == NULL )

            {

                        printf("Unable to create the lexer due to
malloc() failure1\n");

                        exit(ANTLR3_ERR_NOMEM);

            }

 

            tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT,
lxr->pLexer->tokSource);

if (tstream == NULL)

{

printf("Out of memory trying to allocate token stream\n");

            exit(ANTLR3_ERR_NOMEM);

 }

 

            psr = ASTBuildParserNew(tstream);  

 

            if (psr == NULL)

            {

                        printf("Out of memory trying to allocate
parser\n");

                        exit(ANTLR3_ERR_NOMEM);

            }

            myAdaptor =
(pANTLR3_BASE_TREE_ADAPTOR)MY_TREE_ADAPTORNew(tstream->tstream->tokenSou
rce->strFactory);

            psr->adaptor = myAdaptor; 

            parser_ast = psr->prog(psr);

 

 

            // Tree Walker

 

            printf("\n--- TREE WALKINGING ---\n\n");

            nodes = antlr3CommonTreeNodeStreamNewTree(parser_ast.tree,
ANTLR3_SIZE_HINT);

            myAdaptor =
(pANTLR3_BASE_TREE_ADAPTOR)MY_TREE_ADAPTORNew(tstream->tstream->tokenSou
rce->strFactory);

            psr->adaptor = myAdaptor; 

            tw = TreeWalkNew(nodes);

            tw->twProg(tw);

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080422/38a35d53/attachment-0001.html 


More information about the antlr-interest mailing list