[antlr-interest] debugging and testing

ronald.petty at milliman.com ronald.petty at milliman.com
Mon Apr 12 13:37:42 PDT 2004


I am trying to follow along 
http://www.antlr.org/article/parse.trees/index.tml

Do you need to do something with the classpath to get this to work?  Under 
cygwin is

$ echo $CLASSPATH
C:/antlr-2.7.3/antlr.jar;C:/antlr-2.7.3;C:/junit3.8.1/junit.jar;C:/junit3.8.1;.

I am using Sun's java, not the one from Cygwin

so in my .bashrc the CLASSPATH is really

export ANT_HOME="/cygdrive/c/apache-ant-1.6.1"
export JAVA_HOME="/cygdrive/c/j2sdk1.5.0"
export PATH=/cygdrive/c/j2sdk1.5.0/bin:${ANT_HOME}/bin:${PATH}
export CVSROOT=/cygdrive/c/repository
export 
CLASSPATH="C:/antlr-2.7.3/antlr.jar;C:/antlr-2.7.3;${JUNIT_HOME}${JUNIT_JAR};."

I have a directory structure of
pwd/source
pwd/grammars

When I build I do (in pwd)
alias build='mkdir ./build; java antlr.Tool -o ./build 
grammars/VB6Lexer.g; java antlr.Tool -o ./build grammars/VB6Parser.g; 
javac -d ./build ./build/*.java'

Before I tried in do this debugging/testing it worked, however when I add 
the code from the example (pasted) I get.  (My grammar is below the 
errors).

ANTLR Parser Generator   Version 2.7.3   1989-2004 jGuru.com
ANTLR Parser Generator   Version 2.7.3   1989-2004 jGuru.com
./build/VB6Parser.java:41: cannot find symbol
symbol  : class Stack
location: class VB6Parser
        protected Stack currentParseTreeRoot = new Stack();
                  ^
./build/VB6Parser.java:46: cannot find symbol
symbol  : class ParseTreeRule
location: class VB6Parser
        protected ParseTreeRule mostRecentParseTreeRoot = null;
                  ^
./build/VB6Parser.java:51: cannot find symbol
symbol  : class ParseTree
location: class VB6Parser
        public ParseTree getParseTree() {
               ^
./build/VB6Parser.java:26: cannot find symbol
symbol  : class ParseTree
location: class Main
ParseTree tree = parser.getParseTree();
^
./build/VB6Parser.java:41: cannot find symbol
symbol  : class Stack
location: class VB6Parser
        protected Stack currentParseTreeRoot = new Stack();
                                                   ^
./build/VB6Parser.java:85: cannot find symbol
symbol  : class ParseTreeRule
location: class VB6Parser
                ParseTreeRule root = 
(ParseTreeRule)currentParseTreeRoot.peek();

                ^
./build/VB6Parser.java:85: cannot find symbol
symbol  : class ParseTreeRule
location: class VB6Parser
                ParseTreeRule root = 
(ParseTreeRule)currentParseTreeRoot.peek();

                                      ^
./build/VB6Parser.java:86: cannot find symbol
symbol  : class ParseTreeToken
location: class VB6Parser
                ParseTreeToken tokenNode = null;
                ^
./build/VB6Parser.java:88: cannot find symbol
symbol  : class ParseTreeToken
location: class VB6Parser
                        tokenNode = new ParseTreeToken(new 
antlr.CommonToken("EO
F"));
.....  (more errors follow)


options {
}

{
import java.io.*;

class Main {
        public static void main(String[] args) {
                try {
                        VB6Lexer lexer = new VB6Lexer(System.in);
                        VB6Parser parser = new VB6Parser(lexer);
                        parser.program();

ParseTree tree = parser.getParseTree();
System.out.println("parse tree:"+tree.toStringTree());

                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
}
}

class VB6Parser extends Parser("antlr.debug.ParseTreeDebugParser");

options {
        importVocab=VB6;
}


{
        /** Each new rule invocation must have it's own subtree.  Tokens
         *  are added to the current root so we must have a stack of 
subtree roo
ts.
         */
        protected Stack currentParseTreeRoot = new Stack();

        /** Track most recently created parse subtree so that when parsing
         *  is finished, we can get to the root.
         */
        protected ParseTreeRule mostRecentParseTreeRoot = null;

        /** For every rule replacement with a production, we bump up 
count. */
        protected int numberOfDerivationSteps = 1; // n replacements plus 
step 0

        public ParseTree getParseTree() {
                return mostRecentParseTreeRoot;
        }

        public int getNumberOfDerivationSteps() {
                return numberOfDerivationSteps;
        }

        public void match(int i) throws MismatchedTokenException, 
TokenStreamExc
eption {
                addCurrentTokenToParseTree();
                super.match(i);
        }

        public void match(BitSet bitSet) throws MismatchedTokenException, 
TokenS
treamException {
                addCurrentTokenToParseTree();
                super.match(bitSet);
        }

        public void matchNot(int i) throws MismatchedTokenException, 
TokenStream
Exception {
                addCurrentTokenToParseTree();
                super.matchNot(i);
        }

        /** This adds LT(1) to the current parse subtree.  Note that the 
match()
         *  routines add the node before checking for correct match.  This 
means
         *  that, upon mismatched token, there will a token node in the 
tree
         *  corresponding to where that token was expected.  For no viable
         *  alternative errors, no node will be in the tree as nothing was
         *  matched() (the lookahead failed to predict an alternative).
         */
        protected void addCurrentTokenToParseTree() throws 
TokenStreamException
{
                if (inputState.guessing>0) {
                        return;
                }
                ParseTreeRule root = 
(ParseTreeRule)currentParseTreeRoot.peek();
                ParseTreeToken tokenNode = null;
                if ( LA(1)==Token.EOF_TYPE ) {
                        tokenNode = new ParseTreeToken(new 
antlr.CommonToken("EO
F"));
                }
                else {
                        tokenNode = new ParseTreeToken(LT(1));
                }
                root.addChild(tokenNode);
        }

        /** Create a rule node, add to current tree, and make it current 
root */
        public void traceIn(String s) throws TokenStreamException {
                if (inputState.guessing>0) {
                        return;
                }
                ParseTreeRule subRoot = new ParseTreeRule(s);
                if ( currentParseTreeRoot.size()>0 ) {
                        ParseTreeRule oldRoot = 
(ParseTreeRule)currentParseTreeR
oot.peek();
                        oldRoot.addChild(subRoot);
                }
                currentParseTreeRoot.push(subRoot);
                numberOfDerivationSteps++;
        }

        /** Pop current root; back to adding to old root */
        public void traceOut(String s) throws TokenStreamException {
                if (inputState.guessing>0) {
                        return;
                }
                mostRecentParseTreeRoot = 
(ParseTreeRule)currentParseTreeRoot.po
p();
        }

program
        :       (declaration)+
        { System.out.println("Matched Something"); }
        ;

declaration
        :       SUB
        |       FUNCTION
        ;
}

//Lexer

class VB6Lexer extends Lexer;

options {
        exportVocab=VB6;
        charVocabulary='\3'..'\377';
        caseSensitive=false;
        caseSensitiveLiterals=false;
}

tokens {
        DIM="dim";
        SUB="sub";
        FUNCTION="function";
}

IDENT
options {
        testLiterals=true;
}
        :       ('a'..'z')('a'..'z'|'0'..'9'|'_')*
        { System.out.println("found an ident"); }
        ;

WS
        :       ' '
        |       '\t'
        { $setType(Token.SKIP); }
        ;

NL      :       '\r''\n'
        |       '\n'
        { $setType(Token.SKIP); }
        ;

Thanks for the help.
Ron

**************************************************************************************
This communication is intended solely for the addressee and is
confidential. If you are not the intended recipient, any disclosure, 
copying, distribution or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. Unless indicated
to the contrary: it does not constitute professional advice or 
opinions upon which reliance may be made by the addressee or any
other party, and it should be considered to be a work in progress.
**************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20040412/eef5cf45/attachment.html


More information about the antlr-interest mailing list