开发者

Need help constructing a grammar?

L = ((a^n)(b^n+m)(a^m)) | n, m = 0, 1, 2...)

I'm new to context free grammar and know the basics, but I've been struggling with this for a while.

For starters, I dont know what this part of the code means:

| n, m = 0, 1, 2...)

And secondly, how is it possible to have the same variable with diffe开发者_如何学Crent exponents? I feel like I'm not getting the full concept.

Edit: I also need to be able to construct the rules to construct this grammar.

Edit: added tag


First, describe the language in words. This language has a particularly neat description: it is the language of all strings beginning with a's, followed by b's, followed by a's, where the number of b's is equal to the total number of a's (on both sides).

Next, we are going to try to come up with a rule to take strings in the language and produce new strings. Given a string, you can get the next longest string by adding an a to the front and a b in the middle, or an a to the back and a b in the middle. The empty string is in this language (n = m = 0), too, so this can serve as the base for the induction. If we look at this a little closer, we can get an even better rule: we can split any string a^n b^n+m a^m into two strings (a^n b^n)(a^m b^m). Since concatenation is easy to do with grammars and a^k b^k is easy to do with grammars, that's all there is to it.

I get the rules...

  S := LR
  L := empty | aLb
  R := empty | bRa

To get e.g. aabbba,...

  S := LR := aLbR := aaLbbR := aabbR := aabbbRa := aabbba

If this is homework, tagging it as such would be good. If this is self study, hopefully you'll take away the following tricks: describe the language in English; try to identify easy base cases; try to identify rules for forming more complicated strings (i.e. longer strings) from strings already in the language; reformulate your rules and/or notice tricks so that you can state the rules in terms of things that are easy in grammars; write the grammar and check it on a few strings.

What is easy to do in a grammar? Union of languages, concatenation of languages, even matching of pairs of symbols (e.g. a^k b^k), palindromes, etc.


That is just mathematical notation for a restriction on the values of n and m. It means that for a string consisting of the form (a^n)*(b^n+m)*a^m, "n" and "m" are two separate values (although they may be equal), and the 0, 1, 2... just means they are any integer value greater than or equal to zero.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜