how to get all tokens from parser in antlr

2.5k Views Asked by At

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

0

There are 0 best solutions below