JavaCC How to limit one-time occurrence clause

来源:互联网 发布:java常用的类选择器 编辑:程序博客网 时间:2024/06/02 04:18



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;

}

}


0 0
原创粉丝点击