开发者

ANTLR: Define new channel in grammar

I know it is possible to switch between the default and hidden token channels in an ANTLR grammar, but lets say I want a third channel. How can I define a new token channel in the gramar? For instance, lets say I want a c开发者_如何学编程hannel named ALTERNATIVE.


They're just final int's in the Token class , so you could simply introduce an extra int in your lexer like this:

grammar T;

@lexer::members {
  public static final int ALTERNATIVE = HIDDEN + 1;
}

// parser rules ...

FOO
  :  'foo' {$type=ALTERNATIVE;}
  ;

// other lexer rules ...

A related Q&A: How do I get an Antlr Parser rule to read from both default AND hidden channel


For the C target you can use

//This must be assigned somewhere
@lexer::context {
  ANTLR3_UINT32 defaultChannel;
}

TOKEN : 'blah' {$channel=defaultChannel;};

This gets reset after every rule so if you want a channel assignment to persist across rules you may have to override nextTokenStr().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜