简体   繁体   English

解释面试时的上下文无关语法歧义

[英]explaining context free grammar ambiguity for job interview

I was at a job interview, and this is the question they asked me, 我当时正在接受面试,这是他们问我的问题,

Are these two below ambigous? 这两个模棱两可吗? If they are, provide a string. 如果是,请提供一个字符串。 If they are not, prove why they are not. 如果不是,请证明为什么不是。

I couldn't solve it, and would like to know the answer and the reason for the future. 我无法解决,并想知道答案和未来的原因。

Question 1 问题1

S-->XaaaX
X-->aX | bX | e(epsilon)

Question 2 问题2

S-->aaS | aaaS | a

Again, this is not HW. 同样,这不是硬件。

Thank you. 谢谢。 An explanation would help. 一个解释会有所帮助。

We recall that a grammar is ambiguous if (and only if) some production from the grammar has more than one possible derivation. 我们记得,当(且仅当)语法的某些产物具有多个可能的推导时,语法才是模棱两可的。

In question 1 the symbol S expands to XaaaX and then the available alternatives for expanding the symbol X include aX and epsilon (ε). 在问题1中,符号S扩展为XaaaX,然后扩展符号X的可用替代方法包括aX和epsilon(ε)。 Conventionally the symbol epsilon represents an empty string. 按照惯例,符号epsilon代表一个空字符串。 Expanding X as epsilon in aX produces a. 将X扩展为aX中的epsilon会生成a。 So there are at least two ways to get aaaa. 因此,至少有两种方法获得aaaa。 Richard Mckenna, I'll leave it to you to find them. 理查德·麦肯纳,我留给你找到他们。

In question 2 the symbol S expands to aaS, aaaS, or a. 在问题2中,符号S扩展为aaS,aaaS或a。 There are at least two ways to get aaaaaa. 至少有两种获取aaaaaa的方法。 Again I'll leave it to you to find the derivations. 再次,我将留给您查找派生。

If you wish, you may write your derivations on this page. 如果需要,可以在此页面上写出派生词。

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

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