0
$\begingroup$

I need to define a grammar for $L=\{a^ib^ic^jd^j| i \ge j \ge 0\}$ as I wasn't told about any restrictions for the grammar, e.g context-free or context-sensitive, I assume any derivation rules can be used here.

Because the number of $a$s and $b$s is greater than, or equals to, the number of $c$s and $d$s I have this first set of rules $$ S \rightarrow \varepsilon | AS | ASD $$ now I have a series of $A$s and $D$s and I'm looking for a way to derive the desired words from this series. I came up with these rules $$ AD \rightarrow abcd \\ aAb \rightarrow aabb \\ Aa \rightarrow aA \\ cDd \rightarrow ccdd \\ dD \rightarrow Dd $$ with which I can get for example $$ S\Longrightarrow AS \Longrightarrow AASD \Longrightarrow AAASDD \Longrightarrow AAADD \\ \Longrightarrow AAabcdD \Longrightarrow AaAbcdD \Longrightarrow AaabbcdD \Longrightarrow aAabbcdD \\ \Longrightarrow aaAbbcdD \Longrightarrow aaabbbcdD \Longrightarrow aaabbbcDd \Longrightarrow aaabbbccdd \in L $$

As pointed out by Nathaniel's comment the word $ab\in L$ isn't derived from my proposed grammar, so I've revised my derivation rules $$ S \rightarrow \varepsilon | ABR | ABRCD \\ R \rightarrow \varepsilon | BR | BRC \\ AB \rightarrow ab \\ bB \rightarrow Bb \\ aBb \rightarrow aabb \\ CD \rightarrow cd \\ Cc \rightarrow cC \\ cCd \rightarrow ccdd \\ $$ Now I've $A$ and $D$ as place holders for the beginning and the ending of the string so I know where to insert the initial $ab$ and $cd$ and then I move the $B$s one by one to be in the context of $a$ and $b$ to replace them with $ab$ (the same for $C$s)

Is this a valid grammar for $L$, i.e can I have derivation rules of the form $XY\rightarrow YX$ (so far I've only worked with context-free or context-sensitive grammars so I'm not sure to what extent can I go with derivation rules of unrestricted grammar)? If not how can a define a grammar for this type of language? And If it is, is there a better grammar than the one above?

$\endgroup$
4
  • 1
    $\begingroup$ Your grammar cannot generate the word $ab$ which is in the language. $\endgroup$
    – Nathaniel
    Commented May 10, 2022 at 19:37
  • 1
    $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. $\endgroup$
    – D.W.
    Commented May 10, 2022 at 21:48
  • $\begingroup$ The way to tell whether your grammar is correct is to use random testing, and then prove it correct. See cs.stackexchange.com/q/11315/755 for how to approach that. (See also cs.stackexchange.com/q/106101/755.) This is your exercise, so I recommend that you go through the work of trying to do that for your proposed grammar. $\endgroup$
    – D.W.
    Commented May 10, 2022 at 22:02
  • 1
    $\begingroup$ @D.W. Bah, when I saw your comment about testing I thought you were linking to tools... but I modified mine now, and when looking for words up to length 18, their grammar was correct (results on bottom). $\endgroup$ Commented May 10, 2022 at 23:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.