2

I want to get all tokens from my parser and then I want to filter the output, getting a List of AST(myAST):

    ANTLRStringStream stream = new ANTLRStringStream("P + 1 + F(A + 3)");
    MyLexer lexer = new MyLexer (stream);
    MyParser parser = new MyParser(new CommonTokenStream(lexer));
    LanguajeTreeAdaptor treeAdaptor = new LanguajeTreeAdaptor();
    parser.setTreeAdaptor(treeAdaptor);

I have a TreeAdaptor for my languaje:

public class LanguajeTreeAdaptor extends CommonTreeAdaptor{
       public LanguajeTreeAdaptor(){
         super();
       }

  @Override
  public Object create(Token payload) {
      if(payload == null)
         return super.create(payload);

      switch(payload.getType()){
          case EtesGrammarParser.ID:
             return new IdAST(payload);
       ..........
      }
      return super.create(payload);
 }

Here are some rules of my grammar:

program
:   expression EOF!
;

expression
:   exprFinal   
;

exprFinal:  exprFuncCall  |  ID  |  INT  |  DOUBLE  |  STRING  |  exprParenthesis;

exprFuncCall  :  ID LPARENT exprList RPARENT -> ^(FUNC_CALL ID exprList); 

So P, F, and A are Id in my rules, I am trying with this code:

    TokenStream input = parser.getTokenStream();
    TokenSource tSource = input.getTokenSource();
    Token currentToken = tSource.nextToken();

    while(currentToken != null){
        System.out.println(currentToken.getText());
        currentToken = tSource.nextToken();
    }

but I want to process only IdAST(my idea),

    while(currentToken != null){
        if(currentToken instanceof IdAST){
            //call to another method to process the Id
        }
        System.out.println(currentToken.getText());
        currentToken = tSource.nextToken();
    }

I can't do this because currentToken is CommonToken, how can I get those Id only?. I am working with antlr 3 Regards Zinov

Zinov
  • 3,817
  • 5
  • 36
  • 70
  • 1
    No I can't cast to IdAST because IdAST is a CommonTree not a CommonToken :( – Zinov Feb 03 '12 at 20:05
  • 1
    @Bart I have an application where I can edit my Ids, for example P = A (A = 5) where P and A are Ids, so if in my editor I go to A and change the value 5 to P, I get a recursive dependence of Ids, I need to check that this problem will appear when you change my Id, that is the reason to get per line all my Id dependencies. P + 1 + F(A + 3) is the right side of an existence Id – Zinov Feb 03 '12 at 20:15
  • 1
    Sorry, no, I have no idea: I asked a couple of question which you *did* answer, but either I couldn't indicate what information I needed, or you were unable communicate the problem properly (or a bit of both). Regardless of it all, I simply don't know what your problem is exactly (or what you're trying solve). Just wanted to let you know I wasn't looking into it, in case you were waiting for an answer from me. Best of luck. – Bart Kiers Feb 09 '12 at 19:53

0 Answers0