Loading ...
Sorry, an error occurred while loading the content.

help for heterogeneous AST

Expand Messages
  • cormier
    I want to create objects with different classes. Each node is a class. I try to use heterogeneous AST. I fail. My tiny example : class MyParser extends Parser;
    Message 1 of 1 , Jan 2, 2001
    • 0 Attachment
      I want to create objects with different classes. Each node is a class.
      I try to use heterogeneous AST. I fail.
      My tiny example :

      class MyParser extends Parser;
      options {
               buildAST = true;
      }
      tokens {
                        TOKEN<AST=TOKENNode>;
      }
      program   :        BEGIN (token)* END EOF;
      token              :        TOKEN^ ;

      class MyLexer extends Lexer;
      tokens {
              TOKEN = "stuff" ;
      }
      BEGIN    :         '%'       ;
      END               :        '*'       ;
      WS                 :         (' '
                        |         '\t'
                        |         '\n'      {newline();}
                        |         '\r')
                        {        _ttype = Token.SKIP; }
                        ;
      IDENT
               :      ('a'..'z') ('a'..'z')*
                       ;
      -----------------------------------------------------
      import ...
      public abstract class MyAST extends antlr.BaseAST {
      }
      -----------------------------------------------------
      import ...
      /** A simple node to represent TOKEN*/
      public class TOKENNode extends MyAST {
               String nameNode;         
               int typeNode;
               public TOKENNode(Token tok) {
                        nameNode = tok.getText();
                        typeNode = tok.getType();        
                        System.out.println(nameNode + typeNode);
               }
          // satisfy abstract methods from BaseAST
          public void initialize(int t, String txt) {    }
          public void initialize(AST t) {    }
          public void initialize(Token tok) {    }
      }
      -----------------------------------------------------
      import ...
      class Main {
               public static void main(String[] args) {
                        try {
                               MyLexer lexer = new MyLexer(new DataInputStream(System.in));
                              MyParser parser = new MyParser(lexer);
                              parser.program();
                              MyAST t = (MyAST)parser.getAST(); // line 12
                        }
                               catch(Exception e) {
                                 System.err.println("exception: "+e);
                                 e.printStackTrace();
                        }
               }
      }
      ------------------------------------------------------
      MyGrammar.in
      %
       stuff
               stuff
      *
      ------------------------------------------------------
      \bureau\antlr\antlr-2.7.1; Main < MyGrammar.in
      stuff4            
      stuff4
      exception: java.lang.ClassCastException: antlr.CommonAST
      java.lang.ClassCastException: antlr.CommonAST
              at Main.main(Main.java:12)
       
      I certainly missed something, but what?


      Christian CORMIER
      cormier@...

    Your message has been successfully submitted and would be delivered to recipients shortly.