Parsing Bison语法文件中出现零次或一次
我需要解析一个类似JSON的对象Parsing Bison语法文件中出现零次或一次,parsing,compiler-construction,bison,Parsing,Compiler Construction,Bison,我需要解析一个类似JSON的对象{“f”:1,“I”:2,“g”:5,…},但与常规JSON对象不同,输入可以在对象中出现零次或一次 因此,这是错误的对象,因为它有两个键“f” 而且,这个对象很好,因为它只有键“i”,并且不会出现多次 这是我试过的。我知道它不起作用,但我不知道如何设置它 RuleMemberList : RuleMember {{$$ = {}; $$[$1[0]] = $1[1];}} | RuleMemberList ',' RuleMemb
{“f”:1,“I”:2,“g”:5,…}
,但与常规JSON对象不同,输入可以在对象中出现零次或一次
因此,这是错误的对象,因为它有两个键“f”
而且,这个对象很好,因为它只有键“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];}
;
我可以将其定义为零或一次出现吗?在向地图添加元素时检查重复项。比如:
| RuleMemberList ',' RuleMember
{ $$ = $1;
if ($3[0] in $1)
error("duplicate key ", $3[0], " in map");
else
$1[$3[0]] = $3[1];}
;
它看起来不错,但没有名为“error”的函数。您熟悉我可以用来返回错误的另一个函数吗?找到解决方案:抛出“message”可以正常工作。使用JISON()