Replace token in ANTLR

Posted by user1019710 on Stack Overflow See other posts from Stack Overflow or by user1019710
Published on 2012-03-26T19:35:37Z Indexed on 2012/03/27 17:29 UTC
Read the original article Hit count: 201

Filed under:
|

I want to replace a token using ANTLR.

I tried with TokenRewriteStream and replace, but it didn't work.

Any suggestions?

  ANTLRStringStream in = new ANTLRStringStream(source);
  MyLexer lexer = new MyLexer(in);
  TokenRewriteStream tokens = new TokenRewriteStream(lexer);
  for(Object obj : tokens.getTokens()) {
     CommonToken token = (CommonToken)obj;  
     tokens.replace(token, "replacement");
  }

The lexer finds all occurences of single-line comments, and i want to replace them in the original source too.

© Stack Overflow or respective owner

Related posts about antlr

Related posts about antlr3