简体   繁体   English

BNF 中的括号,EBNF

[英]Parens in BNF, EBNF

I could capture a parenthetical group using something like:我可以使用类似的方法捕获括号组:

expr ::=    "(" <something> ")"

However, sometimes it's useful to use multiple levels of nesting, and so it's (theoretically) possible to have more than one parens as long as they match.但是,有时使用多层嵌套很有用,因此(理论上)可以有多个括号,只要它们匹配即可。 For example:例如:

>>> (1)+1
2
>>> (((((-1)))))+2
1
>>> ((2+2)+(1+1))
6
>>> (2+2))
SyntaxError: invalid syntax

Is there a way to specify a "matching-ness" in EBNF, or how is parenthetical-matching handled by most parsers?有没有办法在 EBNF 中指定“匹配度”,或者大多数解析器如何处理括号匹配?

In order to be able to match an arbitrary amount of anything (be it parentheses, operators, list items etc.) you need recursion (EBNF also features repetition operators that can be used instead of recursion in some cases, but not for constructs that need to be matched like parentheses).为了能够匹配任意数量的任何东西(无论是括号、运算符、列表项等),您需要递归(EBNF 还具有重复运算符,在某些情况下可以用来代替递归,但不适用于需要的构造像括号一样匹配)。

For well-matched parentheses, the proper production is simply:对于匹配良好的括号,正确的产生式很简单:

expr ::= "(" expr ")"

That's in addition to productions for other types of expressions, of course, so a complete grammar might look like this:当然,这是对其他类型表达式的产生式的补充,因此完整的语法可能如下所示:

expr ::= "(" expr ")"
expr ::= NUMBER
expr ::= expr "+" expr
expr ::= expr "-" expr
expr ::= expr "*" expr
expr ::= expr "/" expr

Or for an unambiguous grammar:或者对于明确的语法:

expr        ::= expr "+" multExpr
expr        ::= expr "-" multExpr
multExpr    ::= multExpr "*" primaryExpr
multExpr    ::= multExpr "/" primaryExpr
primaryExpr ::= "(" expr ")"
primaryExpr ::= NUMBER

Also, how do you usually go about 'testing' that it is correct -- is there an online tool or something that can validate a syntax?此外,您通常 go 如何“测试”它是否正确——是否有在线工具或可以验证语法的工具?

There are many parser generators that can accept some form of BNF- or EBNF-like notation and generate a parser from it.有许多解析器生成器可以接受某种形式的 BNF 或类似 EBNF 的表示法,并从中生成解析器。 You can use one of those and then test whether the generated parser parses what you want it to.您可以使用其中之一,然后测试生成的解析器是否解析了您想要的内容。 They're usually not available as online tools though.不过,它们通常不能作为在线工具使用。 Also note that parser generators generally need the grammar to be unambiguous or you to add precedence declarations to disambiguate it.另请注意,解析器生成器通常需要语法明确,或者您添加优先级声明来消除歧义。

also wouldn't infinite loop?也不会无限循环?

No. The exact mechanics depend on the parsing algorithm used of course, but if the character at the current input position is not an opening parenthesis, then clearly this isn't the right production to use and another one needs to be applied (or a syntax error raised if none of the productions apply).不,确切的机制当然取决于使用的解析算法,但如果当前输入 position 处的字符不是左括号,那么显然这不是正确的产生式,需要应用另一个(或如果没有任何产品适用,则会引发语法错误)。

Left recursion can cause infinite recursion when using top-down parsing algorithms (though in case of parser generators it's more likely that the grammar will either be rejected or in some cases automatically rewritten than that you get an actual infinite recursion or loop), but non-left recursion doesn't cause that kind of problem with any algorithm.当使用自上而下的解析算法时,左递归会导致无限递归(尽管在解析器生成器的情况下,语法更有可能被拒绝或在某些情况下自动重写,而不是你得到一个实际的无限递归或循环),但非-left 递归不会导致任何算法出现此类问题。

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

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