简体   繁体   中英

Context-free grammar for the language L = {a^(n)b^(m)c^(k): m = |i - k|}

I have this language L = {a^nb^mc^k: m = |n - k|} .

I know m = |n - k| can be expressed in two ways 1) m = n - k for n >= k or n = m + k 2) m = k - n for k >= n or k = m + n
Therefore, I get two languages where
L1 = {a^nb^mc^k: n = m + k} and L2 = {a^nb^mc^k: k = m + n} .
Then I claimed L is the union of the two, L = L1 U L2 .

I don't quite understand how to generate a grammar where one exponent of a terminal is the summation of the other two terminals. ie, in L1 you have n = m + k .
Also L1 can be simplified further to
a^n => a^(m+k) => a^(m)a^(k) so L1 becomes
L1 = {a^ma^kb^mc^k: m, k >= 0}

Attempt answer for L1 = {a^ma^kb^mc^k: m, k >= 0}
A grammar G1
S -> A|B
A -> aAb|lambda
B -> aBc|lambda

For L1 you can do it with

S -> aSc
S -> T
T -> aTb
T -> 

and analogous for L2.

a^nb^n :

Consider the CFG:

S ::= aSb | <empty string>

This generates all strings a^nb^n , with correctly matching exponents. The reason this works is that adding an a using this grammar requires adding an additional b as well; by making sure that every production preserves the desired property (that the number of a s is the same as the number of b s), we've ensured (by induction, since the property holds initially, and every production preserves it) that it will hold for every sentence we generate from the grammar.

a^nb^mc^(n+m) :

If we want to make a grammar to generate the slightly more complex a^nb^mc^(n+m) , we can apply similar reasoning: we encode in the structure of the grammar that adding an a or a b requires adding a c :

S ::= aSc | T | <empty string>
T ::= bTc | <empty string>

Again, since every production preserves our desired property (that the number of c s is the number of a s plus the number of b s), it will hold for any sentence we generate in the grammar.

You can apply similar reasoning to figure out grammars that will preserve the other mathematical properties you mentioned in the OP.

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