Construct a CFG for Regular Expression - regular-language

i had problem with my homework, can someone explane me for the question below. thank you
Construct a CFG for RE below :
a* b* (a│c)*
(a│c)* ba

If you want to write CFG for regular expression, try to understand what regular expression is saying.
For e.g. in 1. ab(a|c)*, we are given with regular expression for any number of a's followed by any number b's followed by any number of a or c's.
To write CGF for this,
S--> ABC
A--> aA | ^
B--> bB | ^
C--> aC | cC | ^
Where variable A will take care of a*, variable B will take care of b*, C will take care of (a|c)*
Similarly, for 2. (a|c)*ba, we are given with regular expression for any number of a or c followed by substring ba. CGF for this will be,
S--> Aba
A--> aA | cA | ^

Related

Excel: select the last number or numbers from a cell

Given the following examples,
16A6
ECCB15
I would only like to extract the last number or numbers from the string value. So the end result that I'm looking for is:
6
15
I've been trying to find a way, but can't seem to find the correct one.
Use thisformula:
=MID(A1,AGGREGATE(14,7,ROW($Z$1:INDEX($ZZ:$ZZ,LEN(A1)))/(NOT(ISNUMBER(--MID(A1,ROW($Z$1:INDEX($ZZ:$ZZ,LEN(A1))),1)))),1)+1,LEN(A1))
Try this:
=--RIGHT(A2,SUMPRODUCT(--ISNUMBER(--RIGHT(SUBSTITUTE(A2,"E",";"),ROW(INDIRECT("1:"&LEN(A2)))))))
or this (avoid using INDIRECT):
=--RIGHT(A2,SUMPRODUCT(--ISNUMBER(--RIGHT(SUBSTITUTE(A2,"E",";"),ROW($A$1:INDEX($A:$A,LEN(A2)))))))
Replace A2 in the above formula to suit your case.
Here are the data for testing:
| String |
|-----------|
| 16A6 |
| ECCB15 |
| BATT5A6 |
| 16 |
| A1B2C3E0 |
| 16E |
| TEST00004 |
I have an even shorter version: --RIGHT(A2,SUMPRODUCT(--ISNUMBER(--RIGHT(SUBSTITUTE(A2,"E",";"),ROW(INDIRECT("1:"&LEN(A2)))))))
The difference is the use of SUBSTITUTE in my final formula. I used SUBSTITUTE to replace letter E with a symbol because in the fifth string in the above list, the RIGHT function in my formula will return the following: {"0";"E0";"3E0";"C3E0";"2C3E0";"B2C3E0";"1B2C3E0";"A1B2C3E0"} where the third string 3E0 will return TRUE by ISNUMBER function, and this will result in an incorrect answer. Therefore I need to get rid of letter E first.
Let me know if you have any questions. Cheers :)

Replace specific characters (a list of them) in a string without removing blanks

I'm trying to work with manually inserted strings in SAS and I need to remove specific special characters (maybe by inserting a list of them) without removing blank spaces between words.
I've found a possible solution with a combination of compbl and transtrn to remove special characters and substitute them with blanks, reduced to one by compbl but this requires multiple steps.
I'm wondering if there is a function that allows me to do this in a single step. I've tried with the compress function (with the 'k' modifier to keep only letters and digits) but it removes blanks between words.
I'd like to go from a string like this one:
O'()n?e /, ^P.iece
To:
One Piece
With a single blank between the two words.
If someone can help me it would be awesome!
Use the next tags for compress function:
k -- Keep chars instead replace it
a -- Alphabetic chars
s -- Space characters
d -- Digits
And after it, use function COMPBL.
Code:
data have;
value="O'()n?e /, ^P.iece";
run;
data want;
set have;
value_want=COMPBL((compress(value,,"kasd"));
run;
So:
+--------------------+------------+
| value | value_want |
+--------------------+------------+
| O'()n?e /, ^P.iece | One Piece |
+--------------------+------------+
You could use regex and prxchage.
data have;
value="O'()n?e /, ^P.iece";
run;
data want;
set have;
value_want=value_want=prxchange("s/\s\s+/ /",-1,prxchange("s/[^a-zA-Z0-9\s]*//",-1,value));
run;
Result:
+--------------------+------------+
| value | value_want |
+--------------------+------------+
| O'()n?e /, ^P.iece | One Piece |
+--------------------+------------+

Replacing a character from standard output on the fly

I'm not sure this is possible, but I'm trying to replace a character from standard output on the fly.
The issue is like this. A command c1 produces the output
So, c1 | less gives me ABC
I would like to replace occurrences of B with D, so I get ADC.
If possible that my command chain should be something like
c1 | <something> | less
and print ADC instead of ABC.
use sed:
c1 | sed 's/B/D/' |less
For the given example of replacing "ABC" with "ADC".
If you want to replace all occurrences of B on D use the option g (global)
sed 's/B/D/g'
You can find more using:
man sed

Vim - Count number of certain character

I'm trying to define a function that does A if there's an even number of dollar signs (in my buffer), and B if there's an odd number of dollar signs. However, I'm unsure how to count the number of dollar signs, and then report that number for defining the function. For instance, entering
:%s/\$//gn
will spit out this number on the status line. But how can one apply the result for defining a function?
Based on #DaveNewman's comment, you may write:
:%s/\$//gn | let i = split(v:statusmsg)[0] | if i % 2 | ... | else | ... | endif

Defining a datatype in Haskell

Greetings, I am new to Haskell and I've gotten stuck in defining a datatype for an assignment.
I need to create the "Strategy" type, it's basically a string with 1-6 characters each representing a numeric value, and I must represent values greater than 9 as a letter (up to 35 total different values), I tried defining an auxiliary type representing each possible value and using that to create my type, but my code isn't working and I have ran out of ideas. This is the definition I have been trying:
data Value = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'A' | 'B' |
'C' | 'D' | 'E' | 'F' | 'G' | 'I' |'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' |
'Q' |'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'Y' | 'X' | 'Z'
data Strategy = Value | Value:Value | Value:Value:Value |
Value:Value:Value:Value | Value:Value:Value:Value:Value |
Value:Value:Value:Value:Value:Value
The Value type isn't accepting the numbers, and the Strategy type "sort of" works up to the second constructor after which it goes bust. Thanks for your help!
Just like your previous (deleted) question, this looks a lot like homework. It also helps if you pick a real name and not use this throw-away account.
This page tells you how to use haskell's data type declarations. What are you doing different/wrong?
You use characters instead of constructors for Value. The '1' '2', etc are all characters, you need a constructor along with zero or more fields like ValueCon1 Char and ValueCon2 String
You are using list constructors and what I assume is a field of Value instead of defining any new constructors for Strategy. Perhaps you want data Strategy = Strat [Value], which will accept a list of any size. While more painful you can define many many strategy constructors with a finite number of separate Value fields.
BTW - where is your course taught? It seems late in the term to be covering these basics.
type Strategy = [Char]
^^ how I'd actually do it.
Or, maybe
newtype Strategy = Strategy [Char]
This is less principled, as the obligation is on you to not fill it with nonsense. Using the newtype solution, you can use "smart constructors" and say, e.g.
mkStrategy :: [Char] -> Strategy
mkStrategy x
| {- list is valid -} = Strategy x
| otherwise = error "you gave me a bad strategy!"
in real code, you don't want to throw error, but return an option type, but that's another story...

Resources