Context free grammar for CFL - regex

enter code hereHallo, this is my question
Give context free grammar for CFL
L = {a^nb^mc^n | m, n ∈ N0}
My answer is
S-> ASC| B
A-> aA| a
B-> bB| b
C-> cC| c
Whether my answer or not ? I am not sure about it.
Need some help. thanks in advance

Your grammar generates the language
L = {a^n b^m c^k | m, n, k ∈ N0}
because the numbers of times that the rules A->aA and C->cC are applied are independent. If you want n=k, then you have to generate the a and c in the same rule. For example like this:
S -> aSc | B .
In a second phase you generate an arbitrary number of b in the middle:
B -> bB | <empty string> .

Related

Are there any languages such that they are proper subsets of each other and satisfy these conditions

Are there languages such that A ⊂ B ⊂ C ⊂ D ⊂ E over the alphabet {a,b,c} where:
A is not context-free
B is context-free and non-regular
C is regular
D is non regular
E is regular and not {a,b,c}*
Start by taking non-context-free language A over {a,b}. For example A = { ww | w \in {a,b}*}, but any other would also work.
You can then build the other languages on top of that:
B = {a,b}* U {a^i c^i | i >= 0}
C = {a,b}* U {a,c}*
D = {a,b}* U {a,c}* U {b^i c^i | i>= 0}
E = {a,b}* U {a,c}* U {b,c}*
You can then verify for each of these that they have the desired properties.
First, let us simplify this and take care of E by just not using c in any language and making E the language (a + b)*. Next, let us deal with D by making it the same as E, but with all strings of prime length greater than two removed. We can choose C to be the set of all even-length strings over {a, b}: (aa + ab + ba + bb)*. For a context-free and non-regular language we can choose the set of even-length palindromes over {a, b}: S -> aSa | bSb | e. Finally, we can choose as A the set of even-length palindromes over {a, b} which begin with a prime number of as.
We might have tried getting rid of D by making it the union of C and some language involving only b, then making C equal to a* and then trying to find A and B using only a... but we might have had trouble finding a context-free non-regular language involving only one symbol.

Ocaml- syntax error during a pattern-matching

Here is my code :
type mass = Inf | P of int
let som = fun
|Inf _ | _ Inf -> Inf
| (P a) (P b) -> P (a+b)
I get the following error :
line 5, characters 0-1:
Error: Syntax error
I don't understand at all how I can get a syntax error here. I tried to replace the fun by : match a b with yet I still get the same syntax.
I also tried to put some : ";" yet it still doesn't work.
These patterns:
Inf _
_ Inf
don't make sense in OCaml. Both of them consist of one pattern followed directly by another. (The Inf pattern matches the Inf constuctor, and _ is a wild-card that matches anything.)
But there is no pattern in OCaml that consists of one pattern followed by another.
The same is true of this pattern:
(P a) (P b)
If these patterns did have a meaning, they would seem to match function applications. But a pattern can't pull apart a function application, it can only pull apart data constructors (lists, tuples, etc.).
What is an example OCaml value that you would expect this pattern to match?
Update
You seem to be saying that the value P 2, P 3 should match this second pattern. The value P 2, P 3 in OCaml is a tuple. It will match this pattern:
(P a), (P b)
Note that the comma is required. The comma is the constructor that creates a tuple.
Update 2
Well, the other mistake is that the fun keyword allows only a single pattern. For multiple patterns you need to use the function keyword. Here is a correct version of your function (assuming that you want it to handle pairs of values of type mass).
type mass = Inf | P of int
let som = function
|Inf, _ | _, Inf -> Inf
| (P a), (P b) -> P (a+b)
Update 3
It's more idiomatic in OCaml to have curried functions. It strikes me that this could be the reason you wanted to have adjacent patterns. To get a curried version of som you need to use an explicit match. Neither fun nor function is quite flexible enough.
It would look like this:
let som x y =
match x, y with
| Inf, _ | _, Inf -> Inf
| P a, P b -> P (a + b)

Pattern Matching with Varying types of Tuples

I'm trying to figure out how to pattern match with user defined types. For example I have this type.
Type custom_type = B of bool | I of int | S of string | C of custom_type * custom_type
I want to pattern match these types, and say for example count the number of ints in a value. Example value:
C(C(B true, I 5), C(S "example", B false))
I think I'm very close to figuring it out, I know I need to use wildcards but I can't write out every instance there could be, because there are numerous varying values I need to check.
Thanks!
Edit: Code that isn't working:
let num = 0
let rec count_ints (c: custom_type):int =
match c with
| C (I(_), _) -> num + 1
| C (_, I(_)) -> num + 1
| C (C(_), _) -> count_ints c
| C (_, C(_)) -> count_ints c
You should be thinking of having 4 cases in your function, one for each constructor. You don't need to match what's inside these constructors because you can call yourself recursively to handle that.
Your code calls count_chars, but there's no function of that name. If it's supposed to be count_ints, then this is not a good recursive call. You must call recursively on a smaller problem. If you just pass c along to yourself recursively you'll get infinite recursion.
let rec count_ints (c: custom_type):int =
match c with
| I _ -> 1
| C (c1,c2) -> count_ints c1 + count_ints c2
| _ -> 0

Prolog generating all possible operation for given list

Hello I am facing a problem. Let's say I have 3 operations(+,-,*).
I would like to generate variable that contains all possible expressions for given list of arguments using those 3 operators.
my_problem([1,2],X) would return
X=1-2
X=1*2
X=1+2
my_problem([1,2,3],X) would return
X=1+2+3 X=1-2-3
X=1+2-3 X=1+2*3
X=1-2+3 ...
and so on.
I know that i can build expression using this predicates.
args_expr(Arg1,Arg2,Arg1 + Arg2).
args_expr(Arg1,Arg2,Arg1 - Arg2).
args_expr(Arg1,Arg2,Arg1 * Arg2).
Is there any clever way to generete this variable? I would be grateful for any help or advice.
You can use the so called "univ" operator, =.., to build expressions:
?- Expr =.. [+, A, B].
Expr = A+B.
And you can enumerate the operations you have:
op(+). op(-). op(*).
Then:
?- op(Op), Expr =.. [Op, A, B].
Op = (+),
Expr = A+B ;
Op = (-),
Expr = A-B ;
Op = (*),
Expr = A*B.
Or maybe:
?- op(Op1), op(Op2), E =.. [Op1, A, E0], E0 =.. [Op2, B, C].
From here to what you need is just figuring out how to do this for lists of arbitrary number of elements (not just two or three).
Hint: If you figure out how to define expr/3, you can do:
?- foldl(expr, [B,C], A, E).
E = C+ (B+A) ;
E = C- (B+A) ;
E = C* (B+A) ;
E = C+ (B-A) ;
E = C- (B-A) ;
E = C* (B-A) ;
E = C+B*A ;
E = C-B*A ;
E = C* (B*A).
Of course, the second argument to foldl can be a list of arbitrary length.

Haskell - parse error/ using multiple where clauses

when trying to define a function that would remove the largest subset of set m that is also a subset of set a from set a, I encountered the following error:
filename.hs:7:33:parse error (possibly incorrect indentation)
for the following code:
exclude :: Integral t => [t] -> [t] -> [t]
a `exclude` m
| m == [] = a
| a == (b ++ c) = b
| otherwise = []
where b /= []
where c = [z | z <- m]
how do I implement multiple conditions/definitions (using where or otherwise), or correct the function to properly work in a different way?
One part of your question is easily answerable. You can have multiple definitions in one where clause, as in
foo n
| even r = bar
| s < 12 = baz
| otherwise = quux
where
r = n `mod` 1357
h = a + b
where
(a,b) = r `divMod` 53 -- nested where-clause
s = r - 3*h
and you can have nested where-clauses. But in a where-clause, you can only have definitions. Conditions would go into the guards (or if then else expressions on the right hand side) and can be combined with the boolean operators, (&&), (||), not ...
As for your code, so far I haven't figured out what you intended it to do.
Saying "the largest subset of set m that is also a subset of set a"
is the same as saying "all elements of m that are also elements of a".
Then the solution to your problem is stated simply as:
exclude a = filter (`notElem` a)
which when applied to m will give you a subset of m modulo any elements
that are also members of a. That is, it will "remove the largest subset of
m that is also a subset of a".
In fact,there is a function in Data.List and Data.Set called '\'. I'll show '\' function of Data.List .
import Data.List
exclude :: Integral t => [t] -> [t] -> [t]
a `exclude` m = a\\m