简体   繁体   English

用PEG.js解析此语法的意外行为

[英]Unexpected Behaviour with parsing this grammar with PEG.js

I'm new to the world of formal grammars. 我是形式语法世界的新手。 I tried to use ANTLR for JS but couldn't figure out how to run the parser. 我试图将ANTLR用于JS,但无法弄清楚如何运行解析器。 I'm trying to develop a grammar for a DSL on PEG.js and this is what I have ( http://peg.arcanis.fr/3T2PKD/ ): 我正在尝试为PEG.js上的DSL开发语法,这就是我所拥有的( http://peg.arcanis.fr/3T2PKD/ ):

start 
  = expr

operator
  = "show" lparen st:string ws rparen { console.log(7)}
  / "show" lparen ex:expr rparen      { console.log(8)}

expr 
 = op: operator ws ex: expr  {console.log (1)}
 / st:string ws ex: expr     {console.log (2)}
 / st:string ws op:operator  {console.log (3)}
 / op:operator ws str:string {console.log (4)}
 / st:string ws              {console.log (5)}
 / op:operator ws            {console.log (6)}

lparen
  = ws "(" ws

rparen
  = ws ")" ws

integer "integer"
  = digits:[0-9]+ { return parseInt(digits.join(""), 10); }

string "string"
  = quotation_mark chars:char* quotation_mark { return chars.join(""); }

ws "whitespace" = [ \t\n\r]*

char
  = unescaped
  / escape
    sequence:(
        '"'
      / "\\"
      / "/"
      / "b" { return "\b"; }
      / "f" { return "\f"; }
      / "n" { return "\n"; }
      / "r" { return "\r"; }
      / "t" { return "\t"; }
      / "u" digits:$(HEXDIG HEXDIG HEXDIG HEXDIG) {
          return String.fromCharCode(parseInt(digits, 16));
        }
    )
    { return sequence; }

escape         = "\\"
quotation_mark = '"'
unescaped      = [\x20-\x21\x23-\x5B\x5D-\u10FFFF]

/* ----- Core ABNF Rules ----- */

/* See RFC 4234, Appendix B (http://tools.ietf.org/html/rfc4627). */
DIGIT  = [0-9]
HEXDIG = [0-9a-f]i

{
  ;
}

When this is tested on: show ("abc") Shouldn't the console show 7 6 ? 在以下情况下进行测试时: show ("abc")控制台不应该显示7 6吗?

Instead it shows 7 7 7 6 而是显示7 7 7 6

Have been racking my brain for the past 3 days. 在过去的三天里,我一直在绞尽脑汁。 Someone, somewhere help me get to parsimoniousness! 有人在某处帮助我节俭!

The parser has to try the rules op: operator ws ex: expr and op:operator ws str:string before it finally succeeds with op:operator ws . 解析器必须尝试规则op: operator ws ex: exprop:operator ws str:string然后才最终通过op:operator ws成功。 It doesn't know that those won't work until it fails to find anything beyond the ) . 它不知道,那些不工作,直到它未能找到任何超出的)

Thus, it gets through the operator rule once, doesn't see another expression, so it backtracks. 因此,它一次通过了operator规则,没有看到另一个表达式,因此它回溯了。 It then sees the operator for the second time, and it still doesn't work because there's no string after it. 然后,它第二次看到该operator ,但它仍然不起作用,因为它后面没有字符串。 So finally, on the third try, it sees the operator and then that last rule succeeds. 因此,最后,在第三次尝试中,它看到了运算符,然后最后一条规则成功了。

Your calls to console.log(7) are made because the operator rule itself succeeds, even though the expr rules don't. 之所以调用console.log(7)是因为operator规则本身会成功,即使expr规则不会成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM