JavaCC SPECIAL_TOKEN option



SPECIAL_TOKEN are special tokens, that are bound to a token; they are referred from token.specialToken fields.


For example:


Grammar define comments as SPECIAL_TOKEN.


SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
}

/* COMMENTS */
SPECIAL_TOKEN :
{
  <SINGLE_LINE_COMMENT: "--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
| <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" ("*" | (~["*","/"] (~["*"])* "*"))* "/">
}


SimpleNode Input() :
{}
{
 P()
 <EOF>
 { return jjtThis; }
}

void P() :
{}
{
  ( A() | B() | C() )+
}





So for this input:

-- line comment 1
A /* field comment 1 */ B
-- line comment 2
/* field comment 2 */
C
-- line comment 3


These inputs will generate 3 TOKEN, A, B, C

A.next = B;

B.next = C;

C.next = null;

And 5 comment special tokens; the comment tokens are bound to A, B, or C

A.specialToken = "--line comment 1"

  "--line comment 1".next = A

B.specialToken = "/* field comment 1 */"

  "/* field comment 1 */".next = B

C.specialToken = "/* field comment 2 */"

  "/* field comment 2 */".next = C

  "/* field comment 2 */".specialToken = "--line comment 2"

  "--line comment 2".next = "/* field comment 2 */"


But the problem is where is "--line comment 3", there is no Token after it ?


Fortunately, there is a <EOF> token at the end, <EOF>.next = "--line comment 3".


So the final token train is:


lc = line comment

fc = field comment






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值