我正在玩PEG.js和阅读,在Nathans 大学我找到了一个很好的“解释”如何构建我自己的语言,但我坚持这一步
我不明白primary
请给我解释一下
start =
comma
comma =
left: additive "," right:comma
{ return {tag: ",", left:left, right:right}; }
/ additive
additive =
left:multiplicative "+" right:additive
{ return {tag: "+", left:left, right:right}; }
/ multiplicative
multiplicative =
left:primary "*" right:multiplicative
{ return {tag: "*", left:left, right:right}; }
/ primary
primary =
integer
/ "(" comma:comma ")" # Can explain me why is necesary "(" and ")"
{ return comma; }
integer =
digits:[0-9]+
{ return parseInt(digits.join(""), 10); }
测试
var parse = wrapExceptions(PEG.buildParser(answer).parse);
assert_eq(parse("1+2"),
{tag:"+", left:1, right:2},
"parse 1+2");
assert_eq(parse("1+2*3"),
{tag:"+", left:1, right:{tag:"*", left:2, right:3}},
"parse 1+2*3");
assert_eq(parse("1,2"),
{tag:",", left:1, right:2},
"parse 1,2");
assert_eq(parse("1,2+3"),
{tag:",", left:1, right:{tag:"+", left:2, right:3}},
"parse 1,2+3");
assert_eq(parse("1*2,3"),
{tag:",", left:{tag:"*", left:1, right:2}, right:3},
"parse 1*2,3");
我的问题是为什么需要/ "(" comma:comma ")"
如果条目没有括号,如果删除该行最后一个测试失败