[antlr-interest] .toStringTree()

Jim O'Connor Jim.OConnor at microfocus.com
Wed Mar 26 11:26:59 PST 2003

Hi Anthony,
  toStringTree() works nicely, if the tree is well formed.  toStringList()
will print out more if the tree is "flat". Viewing the flat output might
give you clues to how the tree can be better constructed.
  Why is (WS|nl) in the top level rule but not in the second level rule?
You might consider telling the lexer to skip these tokens altogether and
ease the strain on the parser.  They also might be throwing monkey in the
wrench of your AST.

  I'm not an expert antlr tree constructor but have struggle through a few
of them enough to make suggestions.  Take it for what it's worth.


-----Original Message-----
From: Anthony Youngman
To: 'antlr-interest at yahoogroups.com'
Sent: 3/26/03 11:11 AM
Subject: [antlr-interest] .toStringTree()

Okay - I've now got Antlr 2.7.2 running on Blackdown 1.3 :-) 

I've copied the general syntax from "XML Parsing The Easy Way". I would
have thought that .toStringTree() would print out a text representation
of the tree. So why is nothing happening? My top level class is:


// This is the wrapper file for the BASIC command 

import java.io.*; 
import antlr.*; 
import antlr.collections.*; 

class BASIC { 
        public static void main(String[] args) { 
                try { 
                        BASICLexer lexer = new BASICLexer(new
                        BASICParser parser = new BASICParser(lexer); 

                        System.out.println( "printing AST"); 
                        AST result = parser.getAST(); 

                } catch(Exception e) { 
                        System.err.println("exception: "+e); 


And the first two rules in the parser are 


entry : ( (WS|nl)* programst (WS|nl)* ) EOF ; 

programst : ( pr:"PROGRAM"^ id:IDENT EOL ( statement )+ "END"
{System.out.println(pr + " " + id+" program ");} ); 


Bearing in mind the parser is called via "entry", and the first thing
declared as a tree is one level down, "programst", is this the problem?
At the moment, all I'm trying to do is get a tree structure printed and
then I'm going to try to construct a tree walker. I've just printed out
the "Antlr Tree Construction" chapter so I will be reading that on the
way home tonight...

Note that my parser now seems to be behaving as expected :-) 


This transmission is intended for the named recipient only. It may
contain private and confidential information. If this has come to you in
error you must not act on anything disclosed in it, nor must you copy
it, modify it, disseminate it in any way, or show it to anyone. Please
e-mail the sender to inform us of the transmission error or telephone
ECA International immediately and delete the e-mail from your
information system.

Telephone numbers for ECA International offices are: Sydney +61 (0)2
9911 7799, Hong Kong + 852 2121 2388, London +44 (0)20 7351 5000 and New
York +1 212 582 2333.

Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> . 


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

More information about the antlr-interest mailing list