简体   繁体   中英

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

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

Question 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 (ε). Conventionally the symbol epsilon represents an empty string. Expanding X as epsilon in aX produces a. So there are at least two ways to get aaaa. Richard Mckenna, I'll leave it to you to find them.

In question 2 the symbol S expands to aaS, aaaS, or a. There are at least two ways to get aaaaaa. Again I'll leave it to you to find the derivations.

If you wish, you may write your derivations on this page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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