2

语法如下。

S -> SS' | a | b
S' -> a | b

我理解它的方式,这个语法的派生就像SS'S'S'S'... (0 or more S'),每个SorS'都会生成aor b

有人可以提供一个例子来说明这个语法是模棱两可的吗?(解决方案说是。)

4

1 回答 1

2

这并不模棱两可。你的分析是正确的。

这是对语法的机械检查(针对我们的工具进行了重新调整):

S = S Sprime ;
S = a ;
S = b ;
Sprime = a ;
Sprime = b ;

工具的执行:

C:\DMS\Domains\DMSStringGrammar\Tools\ParserGenerator>run ParserGenerator.P0B -interactive C:\
DMS GLR Parser Generator 2.4.1
Copyright (C) 1997-2018 Semantic Designs, Inc.
Opening C:\temp\Example.bnf
*** EOF seen
<<<Rule Collection Completed>>>
NTokens = 5 NRules = 5
LR(1) Parser Generator -- Find Follow and SLR Lookahead sets
Computing MemberSets for Nonterminal Tokens...

What next? ambiguities 100
Print results where (<CR> defaults to console)?
Default paper width: 80
How wide should the printout be (<CR> selects default)?
*** Search for ambiguities to depth 100

Nonterminal < Sprime > is not ambiguous
*** Search for ambiguities to depth 1; trying 2 rule pairs...
*** Search for ambiguities to depth 2; trying 2 rule pairs...
*** Search for ambiguities to depth 3; trying 2 rule pairs...
*** Search for ambiguities to depth 4; trying 2 rule pairs...
Nonterminal < S > is not ambiguous [modulo rule derivation loops]

*** 0 ambiguities found ***
*** All ambiguities in grammar detected ***

这个工具对于带有两个非终结符的语法来说是相当大的。但是当有人给出一组 200 个非终结符时,手动完成要困难得多。

(对于理论家:这个工具显然不能为所有语法决定这一点。它在非终结扩展空间中使用递归迭代加深搜索来查找重复/不明确的扩展。这在实践中效果很好)。

于 2019-10-31T17:13:38.403 回答