[antlr-interest] Problem with multiple treeparser ...

LdK ^^ elldekaa at gmail.com
Wed Oct 15 13:51:11 PDT 2008


 I have a problem when i use multiple treeparser,
>> the first is in same file that the parser
>>
>
> You can't make a tree parser in the same grammar file as your lexer/parser.


i don't know if there are some difference but i work with the version 2.7.6
of antlr (my school version)
and i'm sur that i can put the parser and treeparser in the same grammar
file


>  but the second is in a other file and i always get an "" when i make a
>> get.text() in this tree parser
>>
>
> When you construct the tree parser (in your driver code), you need to pass
> the AST node stream from the parser into it and you need to give it access
> to the underlying token stream as well.
>
>
yeah i do that


that's my code (the two tree parser make the same thing, that's just to
debbug) :

PARSER.G

class XHTMLParser extends Parser;
options {
    importVocab=XHTML;
    k = 1;
    buildAST = true;
    defaultErrorHandler=false;
}

//TODO

table        : TAB^ (milieu)+ TABF! EOF!;
milieu        : TRD^ (ligne)+ TRF!;
ligne        : TDD! STR TDF!;

///===========================================================================

{import java.util.*;}
class XHTMLTreeParser   extends  TreeParser;

options    {
        k=1;
        buildAST = true;
    }


{
int n;
String S="";
}

table returns [String chaine=new String()]
            : #(TAB (milieu)*) {chaine=S;}
            ;

milieu            : #(tr:TRD{
            n=tr.getNumberOfChildren();
            } (ligne)*){
            S=S.concat(System.getProperty("line.separator"));
            }
            ;

ligne            : e:STR {
            S=S.concat("\""+e.getText());
            n--;
            if(n!=0) S=S.concat(",");
            }
            ;



//TODO


NEXT FILE :

{import java.util.*;}
class XHTMLNewTreeParser extends TreeParser;

options    {
        buildAST = true;

    }
{
int n;
String S="";
}


table returns [String chaine=new String()]
            : #(TAB (milieu)*) {chaine=S;}
            ;

milieu            : #(tr:TRD{
            n=tr.getNumberOfChildren();
            } (ligne)*){
            S=S.concat(System.getProperty("line.separator"));
            }
            ;

ligne            : e:STR {
            S=S.concat("\""+e.getText());
            n--;
            if(n!=0) S=S.concat(",");
            }
            ;


AND MAIN.JAVA :


import java.io.*;
import java.util.*;
import antlr.collections.AST;
import antlr.debug.misc.ASTFrame;
public class Main {
   public static void main(String args[]) {
      if(args.length==0) { error(); }

      FileInputStream fileInput = null;
      try {
         fileInput = new FileInputStream(args[0]);
      } catch(Exception e) { error(); }

      try {
         DataInputStream input = new DataInputStream(fileInput);

         XHTMLLexer xhtmlLexer =   new XHTMLLexer(input);
         XHTMLParser xhtmlParser = new XHTMLParser(xhtmlLexer);
         xhtmlParser.table();

         AST t = (AST)xhtmlParser.getAST();

            // Print the resulting tree out in LISP notation
            //System.out.println(t.toStringTree()+"\n\n");
                    //Show the resulting tree out in a frame
            ASTFrame frame = new ASTFrame("AST", t);
            frame.setVisible(true);

                    //-----------------------------------------------

                    //Tree Parser
            XHTMLNewTreeParser newtparser = new XHTMLNewTreeParser();
             String space = newtparser.table(t);
            System.out.println(space);
            //System.out.println(space[0]+","+space[0]);
            XHTMLTreeParser tparser = new XHTMLTreeParser();
            String s = tparser.table(t);
            System.out.println(s);
            FileWriter fw = new FileWriter ("ddd.txt");
            fw.write(s);
            fw.close();

      } catch(Exception e) { System.err.println(e.getMessage()); }
   }

   private static void error() {
      System.out.println("*----------------------------------*");
      System.out.println("| USAGE:                           |");
      System.out.println("|   java Main inputfile              |");
      System.out.println("*----------------------------------*");
      System.exit(0);
   }
}


Thx a lot to your help and sorry for my poor englsih ...
Herve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081015/b8d9fb47/attachment.html 


More information about the antlr-interest mailing list