我需要解析像对象{"f": 1, "i": 2, "g": 5, ...}
这样的JSON,但与常规JSON对象不同,输入可以在对象中出现零次或一次......
所以,这是错误的对象{"f": 1, "f": 1, "i": 2, ...}
,因为它有两次" f两次"。
并且,这个对象很好{"i": 2}
,因为它只有关键字" i"并且它不会发生多次。
这是我尝试过的。我知道它不起作用,但我不知道如何将它设置为正确。
RuleMemberList
: RuleMember
{{$$ = {}; $$[$1[0]] = $1[1];}}
| RuleMemberList ',' RuleMember
{$$ = $1; $1[$3[0]] = $3[1];}
;
RuleMember
: I ':' RuleString
{$$ = [$1, $3];}
| G ':' RuleString
{$$ = [$1, $3];}
| F ':' RuleFinder
{$$ = [$1, $3];}
| A ':' RuleAction
{$$ = [$1, $3];}
| T ':' RuleTarget
{$$ = [$1, $3];}
| P ':' RuleNumber
{$$ = [$1, $3];}
| C ':' RuleChance
{$$ = [$1, $3];}
| L ':' RuleLayers
{$$ = [$1, $3];}
| R ':' RuleString
{$$ = [$1, $3];}
| E ':' RuleEvents
{$$ = [$1, $3];}
| B ':' RuleBinds
{$$ = [$1, $3];}
;
我能以某种方式将其定义为零或一次出现吗?
答案 0 :(得分:1)
向地图添加元素时检查重复项。类似的东西:
| RuleMemberList ',' RuleMember
{ $$ = $1;
if ($3[0] in $1)
error("duplicate key ", $3[0], " in map");
else
$1[$3[0]] = $3[1];}
;