【问题标题】:Grammar is matching function rule more than required number of times语法匹配函数规则的次数超过了要求的次数
【发布时间】:2016-07-15 06:18:34
【问题描述】:

这是我使用的语法。匹配规则 'Func' 后的动作对输入字符串 if(diff("col")) 调用了 12 次,而不是 2 次。

调试我意识到在解析 Eq 规则时变量 peg$currPos 被设置了。我无法理解确切的行为

 Main = Eq
 Eq = (Ar (('equal'i/'>='/'<='/'<>'/'!='/'<'/'>'/'=') Ar))
      /Ar ('equal'i/'>='/'<='/'<>'/'!='/'<'/'>'/'=')
      /Ar 

 Ar = (Mul (('+'/'-') Mul )*) ('+'/'-')?

 Mul = (Ex (('*'/'/'/'%'/'^') Ex )*) ('*'/'/'/'%'/'^')?

 Ex = __ ('-'__ Main/'(' __ Main __ ')'/Func/Lit) __ /'-'

 Func = __ id (('('__ Main __')')/'('__')') __ {
      console.log(text());
 }

 Lit = (col/charLit/numLit)
       / id:id

 charLit "character" = string:$("'"[^']*"'")

 numLit "number" = $([0-9.]+)

 col "column" = col:$('"'[^"]*'"') 

 id "identifier" = $([a-zA-Z][a-zA-Z0-9_]*)

 __ "space" = [ \t\n]*              

【问题讨论】:

    标签: javascript pegjs


    【解决方案1】:

    您遇到这种情况的原因是因为Func 规则匹配了很多次。不幸的是,其中一些匹配是失败的较大表达式(在Eq 规则中)的一部分。这就是为什么您会看到 peg$currPos 被退回的原因。

    不妨试试:

    Eq = Ar (('equal'i/'>='/'<='/'<>'/'!='/'<'/'>'/'=') Ar?)?
    

    【讨论】:

    • 是的,我想通了。谢谢
    猜你喜欢
    • 2023-01-31
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多