[英]CFG: Why is this grammar ambiguous?
The grammar is as follows.语法如下。
S -> SS' | a | b
S' -> a | b
The way I understand it, derivations from this grammar will be like SS'S'S'S'... (0 or more S')
, where each S
or S'
will generate a
or b
.我理解它的方式,从这个语法派生就像
SS'S'S'S'... (0 or more S')
,其中每个S
或S'
将生成a
或b
。
Can someone provide an example that shows this grammar is ambiguous?有人可以提供一个例子来说明这个语法是模棱两可的吗? (The solution says it is.)
(解决方案说是。)
It isn't ambiguous.这并不模棱两可。 Your analysis is correct.
你的分析是正确的。
Here's a mechanical check of your grammar (reshaped for our tool):这是对语法的机械检查(针对我们的工具进行了重新调整):
S = S Sprime ;
S = a ;
S = b ;
Sprime = a ;
Sprime = b ;
Execution of tool:工具的执行:
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 ***
This tool is rather overkill for grammar with two nonterminals.这个工具对于带有两个非终结符的语法来说是相当大的。 But when somebody gives a set of 200 nonterminals it is much harder to do by hand.
但是当有人给出一组 200 个非终结符时,手动完成要困难得多。
(For theorists: this tool obviously can't decide this for all grammars. It uses a recursive iterative deepening search in the space of nonterminal expansions to look of duplicate/ambiguous expansions. That's works pretty well in pratice). (对于理论家:这个工具显然不能为所有语法决定这一点。它在非终结扩展空间中使用递归迭代加深搜索来查找重复/不明确的扩展。这在实践中效果很好)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.