Need help checking if my CFG question is correct - regex

I need to write a CFG for the language below:
L = { w ∈ Σ* | w is a regular expression over a binary alphabet }
I came up with:
S = SΣ* | ε
X = S | ε
I'm starting the discipline now, and if it isn't correct, i would apreciate an explanation.
Many thanks!
PS.: This is not part from a homework, it is just an exercise from a Computer Science Theory book.
ANSWER BASED ON Mo B. POST
S = e | {0,1}
X = S | S* | Z
Y = X | YX
Z = Y | Z | Y
SECOND ANSWER BASED ON Mo B. POST
S = e | {0,1}
X = S | S'*' | '(' Z ')'
Y = X | YX
Z = Y | Z '|' Y

No, this is not correct. The language is the set of regular expressions (which itself isn't regular, but it's luckily context-free), so you need to come up with a context-free grammar for regular expressions. (First, make sure you know the formal definition for regular expressions.)
The meta-alphabet Σ has not been defined in your question, but it only works if Σ at least contains the mentioned binary alphabet, say 0 and 1, and the symbols ε, *, |, ( and ).
Here is one way of doing it:
Basic ::= 'ε' // Note that this is the symbol 'ε' which is not the same as ε
| c // for c in {0, 1} or whatever the binary alphabet is
Star ::= Basic
| Star '*'
| '(' Regular ')'
Concat ::= Star
| Concat Star
Regular ::= Concat
| Regular '|' Concat

Related

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)

Context free grammar for CFL

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> .

Parsing Logic Expressions with Pyparsing

To start of, this is not homework, I'm trying to learn pyparsing and I got stuck here.
My question is as follows, I'm trying to parse statements like (abc or def) or def)
My program goes to shit on an infix expression a or b, since both sides can be expressions themselves, which can again be infix expressions, the parser recurses until recursion depth is reached and no work gets done.
Code below:
# infix operators are automatically created and dealt with
infix_operators = ['and', '&', 'or', '|', 'implies', '->']
variable = Word(alphas)
infix_op = oneOf(infix_operators, caseless=True)
expr = Forward()
infix_expr = (expr + infix_op + expr)
complex_expr = nestedExpr('(', ')', content=expr)
expr << (infix_expr | complex_expr | variable)
print str(expr.parseString("(abc or def) or def)")[0])
My question is fairly simple; how would one go about avoiding an infinite loop in these kinds of situations?
The canonical solution is something that implements this BNF:
atom := variable | 'True' | 'False' | '(' expr ')'
factor := [ 'not' ]... atom
term := factor [ '&' factor ]...
expr := term [ '|' term ]...
The left-recursion problem is addressed because, even though expr eventually recurses through term -> factor -> atom, when it gets to expr, it first has to parse a leading '('. So an expr never has to first parse a deeper expr before parsing some other elements first.
This BNF translates almost directly to pyparsing as:
and_ = Keyword('and')
or_ = Keyword('or')
not_ = Keyword('not')
true_ = Keyword('true')
false_ = Keyword('false')
not_op = not_ | '~'
and_op = and_ | '&'
or_op = or_ | '|'
expr = Forward()
identifier = ~(and_ | or_ | not_ | true_ | false_) + Word(alphas)
atom = identifier | Group('(' + expr + ')')
factor = Group(ZeroOrMore(not_op) + atom)
term = Group(factor + ZeroOrMore(and_op + factor))
expr <<= Group(term + ZeroOrMore(or_op + term))
Or you can use pyparsing's infixNotation helper:
expr = infixNotation(true_ | false_ | identifier,
[
(not_op, 1, opAssoc.RIGHT),
(and_op, 2, opAssoc.LEFT),
(or_op, 2, opAssoc.LEFT),
])
infixNotation is constructed with a base operand (in this case, either an alpha variable name or one of the boolean literals true or false), followed by a list of (operator, arity, associativity) tuples, given in order of operator precedence. infixNotation takes care of all the recursion definitions, the parsing of right-associative vs. left-associative operators, and also does some lookahead for operators, to avoid extra nesting of operations for a given precedence level if there are no operators.
You can test this expression using pyparsing's runTests method:
expr.runTests("""
p and not q
not p or p
r and (p or q)
r and p or q
not q
q
""", fullDump=False)
Giving:
p and not q
[['p', 'and', ['not', 'q']]]
not p or p
[[['not', 'p'], 'or', 'p']]
r and (p or q)
[['r', 'and', ['p', 'or', 'q']]]
r and p or q
[[['r', 'and', 'p'], 'or', 'q']]
not q
[['not', 'q']]
q
['q']

Changing an expression to string

I need to convert an arithmetic sequence that uses
this type:
type expr =
VarX
| VarY
| Sine of expr
| Cosine of expr
| Average of expr * expr
| Times of expr * expr
| Thresh of expr * expr * expr * expr
here are the definitions for all the things in it:
e ::= x | y | sin (pi*e) | cos (pi*e) | ((e + e)/2) | e * e | (e<e ? e : e)
need to convert something like this:
exprToString (Thresh(VarX,VarY,VarX,(Times(Sine(VarX),Cosine(Average(VarX,VarY))))));;
to this:
string = "(x<y?x:sin(pi*x)*cos(pi*((x+y)/2)))"
I know I have to do this recursively by matching each expr with its appropriate string but Im not sure where the function begins matching or how to recurse through it. Any help or clues would be appreciated
Here is a simplified version of what you probably want:
type expr =
| VarX
| Sine of expr
let rec exprToString = function
| VarX -> "x"
| Sine e -> "sin(" ^ exprToString e ^ ")"
let () = print_endline (exprToString (Sine (Sine (Sine (Sine VarX)))))
It recurses over the AST nodes and create the string representation of the input by concatenating the string representations of the nodes.
This approach may not work nicely for bigger real world examples since:
String concatenation (^) creates a new string from two, this is slower than using some more appropriate data structure such as Buffer.t
Too many parentheses, ex, (2*(2*(2*2))), not 2*2*2*2. If you want minimize the number of parentheses, your algorithm must be aware of operator precedence and connectivity.

Construct finite automata

Would you agree that in the regular expression:
((a|b)*(e|c)*)*
is any combination of a,b, and c's? Or would you say c always comes after a and b.
Through I always prefer to describe Regular Expressions RE semantically. But there is also a rule, one of – "distributed law", that is very helpful to write cleanup and optimized RE:
(P | Q)* == (P*Q*)* == (P* | Q*)*
Note: | is union operation and P | Q is same as P | Q. Here P, Q are regular expressions.
So you expression:
((a|b)*(e|c)*)* # P = (a|b)* and Q = (e|c)*
=> ((a|b) | (e|c))* # (P* | Q*)* = (P | Q)*
As I said in union order is not important, so here ( ) are redundant. and
((a|b) | (e|c))*
=> (a | b | c | e)*
Now * means repetition any number of times of some pattern on which * is applied. Here in above expression * is applied on a | b | c | e, and in each iteration you can pick any one symbol, That means any symbol an be appear after any other symbol in regular expression – that means any combination of 'a', 'b', 'c', 'e' is possible.
And its FA is very simple: consist of single state Q0 with a self loop labeled all four symbols. as follows:
__
|| a, b, c, e
▼|
––►((Q0))
Q0 is both initial and final state