上下文无关语法-计算理论

发布于 2024-10-07 08:11:35 字数 507 浏览 0 评论 0原文

我正在为期末考试和期末考试而学习。我正在阅读维基百科上的上下文无关语法文章,并发现了以下示例。

S → SS- (1st production rule)

S → (S) - (2nd production rule)

S → () - (3rd production rule)

我很清楚左推导和右推导。当我尝试解决这个问题时,我从开始符号开始

S-> SS -> (S)S-> ()S-> ()(S) -> ()() 

,但是当我查看答案时,就像这样

S → SS → SSS → (S)SS → ((S))SS → ((SS))S(S)
→ ((()S))S(S) → ((()()))S(S) → ((()()))()(S)
→ ((()()))()(())

我不确定我的答案出了什么问题?是否有必要使用第一个产生式规则两次?有人可以帮我解决这个问题吗?

I am studying for my finals & I was reading context free grammars article from wikipedia and came across following example.

S → SS- (1st production rule)

S → (S) - (2nd production rule)

S → () - (3rd production rule)

I am well aware of left and right derivation. When I tried to solve this problem I start with start symbol

S-> SS -> (S)S-> ()S-> ()(S) -> ()() 

but when I looked the answer, it was like this

S → SS → SSS → (S)SS → ((S))SS → ((SS))S(S)
→ ((()S))S(S) → ((()()))S(S) → ((()()))()(S)
→ ((()()))()(())

I am not sure what goes wrong with my answer? Is it necessary to use first production rule twice? Could anybody help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2024-10-14 08:11:35

您的方法没有任何“错误” - 您刚刚导出了与维基百科文章不同的符号序列。

关键点是,可以使用语法派生出任何匹配、嵌套的括号序列,但不能派生出像 (())()(< 这样的序列/代码>。

There's nothing "wrong" with your approach - you've just derived a different sequence of symbols to the Wikipedia article.

The crucial point is that it's possible to derive any sequence of matching, nested parentheses using the grammar, but not sequences like (() or )()(.

只为一人 2024-10-14 08:11:35

当我尝试解决这个问题时,我从开始符号开始

什么问题?维基百科的文章没有造成任何问题。它只是显示了描述匹配括号的语言的语法,并给出了该语言中的单词的示例以及如何导出它。

<前><代码>S-> SS-> (S)S-> ()S-> ()(S)-> ()()

这是一个完全有效的推导。

但是当我查看答案时,它是这样的

不是答案(没有问题)。这只是一个例子。

我不确定我的答案出了什么问题?

你的推导没有任何问题。 ()()((()()))()(()) 都是该语言中的有效单词。

是否有必要使用第一个产生式规则两次?

您可以根据需要多次应用产生式规则(当然假设您要替换的非终结符存在于术语中)并按您想要的任何顺序。这完全取决于您想要派生哪个词。

When I tried to solve this problem I start with start symbol

What problem? The wikipedia article doesn't pose any problem. It just shows a grammar describing the language of well-matched parentheses and gives an example of a word in that language and how to derive it.

S-> SS -> (S)S-> ()S-> ()(S) -> ()() 

That's a perfectly valid derivation.

but when I looked the answer, it was like this

That's not the answer (there was no question). It's just an example.

I am not sure what goes wrong with my answer?

There's nothing wrong with your derivation. Both ()() and ((()()))()(()) are valid words in the language.

Is it necessary to use first production rule twice?

You can apply the production rules as often as you want (assuming of course the non-terminal you want to replace is present in the term) and in any order you want. It all just depends on which word you want to derive.

雅心素梦 2024-10-14 08:11:35

这篇文章只是给出了一个可以使用该语法表示的可能字符串......您给出了另一个可能的字符串。您可以使用推导来证明这些字符串根据语法是有效的(即反向)。

编辑:被打败了:P

The article is simply giving one possible string which can be represented using that grammar... you are giving another possible string. You could use derivation to prove that those strings are valid according to the grammar (i.e. going in the reverse direction).

EDIT: Beaten to the punch :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文