[英]Combing odd and even regular expression to regular grammar?
I have this class that I need to write regular grammar for. 我有这个课我需要写常规语法。 The grammar is {a,b,c} where there are an odd number of a's and c's, but an even number of b's.
语法是{a,b,c},其中有一个奇数的a和c,但偶数个b。
Examples of good strings: 良好字符串的示例:
Bad strings 坏字符串
My regex for even b's is b∗(ab∗ab∗)∗b∗
(I don't know where to include c) 甚至b的我的正则表达式是
b∗(ab∗ab∗)∗b∗
(我不知道在哪里包括c)
My regex for odd a's is (c|a(b|c)*a)*a(b|c)*
我对奇数a的正则表达式是
(c|a(b|c)*a)*a(b|c)*
My regex for odd c's is (c|a(b|c)*c)*c(b|c)*
我的奇数c的正则表达式是
(c|a(b|c)*c)*c(b|c)*
I'm thinking that a regular grammar would look something like this: 我认为常规语法看起来像这样:
s -> [a], a
s -> [c], c
a -> [a], a
a -> [b], b
a -> [c], c
b -> [b]
b -> [b], b
b -> [a], a
b -> [c], c
c -> [c], c
c -> [a], a
c -> [b], b
I think it's evident that I'm very lost. 我觉得我很遗憾。 Any help is appreciated!
任何帮助表示赞赏!
Here is a possible solution in SWI-Prolog: 这是SWI-Prolog中可能的解决方案:
:- use_module(library(clpfd)).
:- use_module(library(lambda)).
odd_even(Lst) :-
variables_signature(Lst, Sigs),
automaton(Sigs, _, Sigs,
% start in s, end in i
[source(s), sink(i)],
% if we meet 0, counter A of a is incremented of one modulo 2
% the others are unchanged
[arc(s, 0, s, [(A+1) mod 2, B, C]),
arc(s, 1, s, [A, (B+1)mod 2, C]),
arc(s, 2, s, [A, B, (C+1) mod 2]),
arc(s, 0, i, [(A+1) mod 2, B, C]),
arc(s, 1, i, [A, (B+1)mod 2, C]),
arc(s, 2, i, [A, B, (C+1) mod 2])],
% name of counters
[A, B, C],
% initial values of counters
[0, 0, 0],
% needed final values of counters
[1,0,1]).
% replace a with 0, b with 1, c with 2
variables_signature(Lst, Sigs) :-
maplist(\X^Y^(X = a -> Y = 0; (X = b -> Y = 1; Y = 2)), Lst, Sigs).
Example : 示例:
?- odd_even([a,c,c,a,c,c,a,c]).
true.
?- odd_even([a,c,c,a,c,c,a]).
false.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.