[antlr-interest] Re: Beginner Question: What are the steps to get a rules's corresponding text in the parser ?

micheal_jor <open.zone at virgin.net> open.zone at virgin.net
Thu Jan 30 09:32:59 PST 2003


> I did not see how does tree parsers mecanism work.

These should help:
http://www.antlr.org/doc/sor.html#_bb1
http://www.jguru.com/faq/view.jsp?EID=818959

> I made a lexer and a parser, it compiles well and it seems to work.
>  
> I know to have a token's corresponding text in the lexer we use the
> .getText() method.
>  
> What are the steps to get a rules's corresponding text in the 
parser ?
> Is "buildAST = true" option needed ?

"buildAST = true" must be set in the global options section for a 
Parser if you want ANTLR to build an AST for you. If it is false (or 
missing), no AST/tree is built.

You can tell ANTLR waht sort of tree to build by annotating your 
grammar elements appropriately. See:
http://www.antlr.org/doc/trees.html#_bb1

To get an AST node's corresponding text in the Parser/TreeParser, use 
getText() on the AST node object.

AFAIK, there isn't a facility to print the text of a rule, you can 
use a basic TreeParser that walks your tree and calls getText() on 
each node in your desired order if you really want to do this.


Cheers!,

Micheal


PS   The C# codegen's ToStringTree() method does print a tree-like 
representation of your AST. You use that if you can't or don't want 
to use an ASTFrame to visualize your AST. See 'java' sample for 
example of using ASTFrame. Also see:

http://www.antlr.org/doc/sor.html#Examining_Debugging%20ASTs




 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list