JavaCC How to limit one-time occurrence clause

本文探讨了如何使用JavaCC定义一种特殊的文法,该文法支持一组子句,这些子句可以无序出现但每个只能被指定一次。通过具体示例说明了如何利用用户行为来实现这一限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



How to support a set of clause whose orderis meaningless, but each can only be specified once. For example, non-terminalP() may contain 3 sub-clause( A(), B(), C()), but each can only occur one time, so followingare valid input:

  • C
  • B C
  • A B C
  • C A B



And following are invalue

  • A A                    //A is specified twice
  • C B C                  //C is specified twice




JavaCC grammar

P() : {}

{

    (A() B() C() )?

}

This grammar will support only one clause A(),B(), or C(), but will not their combination.



P() : {}

{

    (A() B() C() )+

}

This grammar will support any combinationof A(), B(), or C(), no onetime only limitation.




It seems grammar itself cannot have this limitation;however, we can use user-action to support this limitation; following grammarhas been reconstructed:


void P() :

{

         SimpleNode[]ABCClause = new SimpleNode[3];

}

{

A()

{

if (ABCClause[0] != null)

throw new Exception("A() specified more than one");

ABCClause[0] = value;

}

|       B()

{

if (ABCClause[1] != null)

throw new Exception("B() specified more than one");

ABCClause[1] = value;

}

|       C()

         {

if (ABCClause[2] != null)

throw new Exception("C() specified more than one");

ABCClause[2] = value;

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值