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

998

被折叠的 条评论
为什么被折叠?



