Generate context free grammar for the following language: - grammar

The set of string from the alphabet {j,k} where the string can be reversed and then all j's changed to k's and all k's changed to j's.
For example "jjkk" would be in the language because when you reverse it: "kkjj" and when you flip all of the characters to the other character: "jjkk"
"kjk" would not be in the language because when you reverse it: "kjk" (the same) and flipping the characters yields "jkj" which is not the same as the starting string.

The string can't be an odd length because then the middle character can't be flipped. Every ith character from the left that is a j must have a corresponding k i characters from the right. The same holds for ks on the left and js on the right. So:
S ::= ε | jSk | kSj

Related

T-SQL RegEx Matching "One or More" Operator

In MS SQL, is there an operator that allows the matching of one or more character? (I'm curious about whether its implemented explicitly in T-SQL - other solutions are certainly possible, one of which I use in my question example below . . .)
I know in SQL, this could be explicitly implemented to varying degrees of success with the wildcard/like approach:
SELECT *
FROM table
-- finds letters aix and then anything following it
WHERE column LIKE 'aix_x%'
In Python, the '+' operator allows for this:
import re
str = "The rain in Spain falls mainly in the plain!"
#Check if the string contains "ai" followed by 1 or more "x" characters:
# finds 'ai' + one or more letters x
x = re.findall("aix+", str)
print(x)
if (x):
print("Yes, there is at least one match!")
else:
print("No match")
Check if the string contains "ai" followed by 1 or more "x" characters:
finds 'ai' + one or more letters x
If this is what you want, then:
where str like '%aix%'
does what you want.
If you want an underscore, then an underscore is a wildcard in LIKE expressions. Probably the simplest method in SQL Server is to use a character class:
where str like '%ai[_]x%'
another solution is:
where str like '%ai$_x%' escape '$'

How can I prove this language is regular?

I'm trying to prove if this language:
L = { w={0,1}* | #0(w) % 3 = 0 } (number of 0's is divisble by 3)
is regular using the pumping lemma, but I can't find a way to do it. All other examples I got, have a simple form or let's say a more defined form such as w = axbycz etc.
I don't think you can use pumping lemma to prove that a language is regular. To prove a language is regular, you just need to give a regular expression or a DFA. In this case the regular expression is quite easy:
1*(01*01*01*)*
(proof: the regular expression clearly does not accept any string which has the number of 0's not divisible by 3, so we just need to prove that all possible strings which has the number of 0's divisible by 3 is accepted by this regular expression, which can be done by confirming that for strings that contain 3n 0's, the regular expression matches it since 1n001n101n201n3...01n3n-201n3n-101n3n has the same number of 0's and the nk's can be substituted so that it matches the string, and that this format is clearly accepted by the regular expression)
Pumping lemma cannot be used to prove that a language is regular because we cannot set the y as in Daniel Martin's answer. Here is a counter-example, in a similar format as his answer (please correct me if I'm doing something fundamentally different from his answer):
We prove that the language L = {w=0n1p | n ∈ N, n>0, p is prime} is regular using pumping lemma as follows: note that there is at least one occurrence of 0, so we take y as 0, and we have xykz = 0n+k-11p, which still satisfy the language definition. Therefore L is regular.
But this is false, since we know that a sequence with prime-numbered length is not regular. The problem here is we cannot just set y to any character.
Any string in this language with at least three characters in it has this property: either the string has a "1" in it, or there are three "0"s in a row.
If the string contains a 1, then you can split it as in the pumping lemma and set y equal to some 1 in the string. Then obviously the strings xyz, xyyz, xyyyz, etc. are all in the language because all those strings have the same number of zeros.
If the string does not contain a 1, it contains three 0s in a row. Setting y to those three 0s, it should be obvious that xyz, xyyz, xyyyz, etc. are all in the language because you're adding three 0 characters each time, so you always have a number of 0s divisible by 3.
#justhalf in the comments is perfectly correct; the pumping lemma can be used to prove that a regular language can be pumped or that a language that cannot be pumped is not regular, but you cannot use the pumping lemma to prove that a language is regular in the first place. Mea Culpa.
Instead, here's a proof that the given language is regular based on the Myhill-Nerode Theorem:
Consider the set of all strings of 0s and 1s. Divide these strings into three sets:
E0, all strings such that the number of 0s is a multiple of three,
E1, all strings such that the number of 0s is one more than a multiple of three,
E2, all strings such that the number of 0s is two more than a multiple of three.
Obviously, every string of 0s and 1s is in one of these three sets.
Furthermore, if x and z are both strings of 0s and 1s, then consider what it means if the concatenation xz is in L:
If x is in E0, then xz is in L if and only if z is in E0
If x is in E1, then xz is in L if and only if z is in E2
If x is in E2, then xz is in L if and only if z is in E1
Therefore, in the language of the theorem, there is no distinguishing extension for any two strings in the same one of our three Ei sets, and therefore there are at most three equivalence classes. A finite number of equivalence classes means the language is regular.
(in fact, there are exactly three equivalence classes, but that isn't needed)
A language is regular if and only if some nondeterministic finite automaton recognizes it.
Automaton is a finite state machine.
We have to build an automaton that regonizes L.
For each state, thinking like:
"Where am I?"
"Where can I go to, with some given entry?"
So, for L = { w={0,1}* | #0(w) % 3 = 0 }
The possibilites (states) are:
The remainder (rest of division) is 0, 1 or 2. Which means we need three states.
Let q0,q1 and q2 be the states that represent the remainderes 0,1 and 2, respectively.
q0 is the start and final state.
Now, for "0" entries, do the math #0(w)%3 and go to the aproppriated state.
Transion functions:
f(q0, 0) = q1
f(q1, 0) = q2
f(q2, 0) = q0
For "1" entries, it just loops wherever it is, 'cause it doesn't change the machine state.
f(qx, 1) = qx
The pumping lemma proves if some language is not regular.
Here is a good book for theory of computation: Introduction to the Theory of Computation 3rd Edition
by Michael Sipser.

Solve three letter string into regular expression way

I need help to solve in regular expression way. The language of all strings defined over Σ = {X, Y, Z} with Y as the third letter and Z being the second last letter.
If you are allowed to use intersection (which does preserve rationality), I would state it simply as ΣΣYΣ* & Σ*ZΣ. If you feed this to Vcsn to normalize it, you get:
In [1]: import vcsn
In [2]: vcsn.B.expression('([XYZ]{2}Y[XYZ]*)&([XYZ]*Z[XYZ])').derived_term().expression()
Out[2]: (X+Y+Z)ZY+(X+Y+Z)(X+Y+Z)Y(X+Y+Z)*Z(X+Y+Z)
The call to derived_term is to build an automaton from the expression, and the last call to expression is to extract a rational expression from this automaton.
As given Σ = {X, Y, Z} , you need to construct the language of all strings defined over it with Y as third letter and Z being the second last letter.
"ΣΣYΣ*ZΣ | ΣZY" will be the required regular expression.
Σ* has all strings that are 0 or more concatenations of strings from Σ.
As you can see, here Y being the third element and Z is placed in second last position. And, Σ can be replaced with any of the X,Y or Z element.
i think regular expression should be like this....
(x+y+z)zy(x+y+z)^*

Contains at least a count of different character in a set

Assume that, I have a character set like this:
['a','b','c','x','y','z']
I want to build a regular expression which matches a certain number of these characters (for example 3).
Here are some examples of it:
ab - no match
xy - no match
abt - no match
aaa - no match
abc - match
yaz - match
yazx - match
ytaz - match
Can this be accomplished with a regular expression?
A simple solution would be a pattern like this:
(.*[abcxyz]){3}
This will match zero or more of any character, followed by one of a, b, c, x, y, or z, all of which must appear at least 3 times in the subject string.
To match only strings that contain different letters, you could use a negative lookahead ((?!…)) and a backreference (\N):
(.*([abcxyz])(?!.*\2)){3}
This will match zero or more of any character, followed by one of a, b, c, x, y, or z, as long as another instance of that character does not appear later in the string (i.e. it will match the last instance of that character in the string), all of which must appear at least 3 times in the subject string.
Of course, you can change the {3} to anything you like, but note that will not work if you need to specify a maximum number of times these characters can appear in your string, only the minimum.

Special characters to int, and then back

So what I want, is for example to convert the letter 'a' into 97 (such as it is in the ASCII table), and then convert 67 into 'a'.
I actually perform a load of mathematics and stuff to the letter, treating it as binary number - so the transition is necessary.
However for special characters it is not working nicely.
char c = 'ÿ';
int i = int(c);
wchar_t wTemp = static_cast<wchar_t>(i);
wchar_t* w = &wTemp;
String^ newI = gcnew String(w);
That symbol is just a random one I found in an image (the type of character that will need to be read). It just comes out as a completely different symbol. I have no idea why, or what to do?
Characters above 0x7f (127) are probably converting to negative integer values. Maybe change c to unsigned:
unsigned char c = 'ÿ';
int i = c;
Your code doesn't look quite right to me though I didn't run it. Here is a good example from MSDN how to convert from and to wchar_t:
http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx
I don't believe there is anything special about 'special' characters.