引用PEGJS 教程:
要生成解析器,请调用 PEG.buildParser 方法并将您的语法作为参数传递:
var parser = PEG.buildParser("start = ('a' / 'b')+");
我的语法有点复杂:
start
= additive
additive
= left:multiplicative "+" right:additive { return left + right; }
/ multiplicative
multiplicative
= left:primary "*" right:multiplicative { return left * right; }
/ primary
primary
= integer
/ "(" additive:additive ")" { return additive; }
integer "integer"
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
我应该传递给PEG.buildParser
我的语法的起始规则,即additive
,但我做错了。如果我写
PEG.buildParser(additive)
或PEG.buildParser(start)
,FireBug 说
SyntaxError: Expected "=" or string but end of input found
;如果我写PEG.buildParser(start = additive)
,我会得到GrammarError: Referenced rule "additive" does not exist
;通过我的语法的正确方法是什么?