簡體   English   中英

將奇數甚至正則表達式與常規語法相結合?

[英]Combing odd and even regular expression to regular grammar?

我有這個課我需要寫常規語法。 語法是{a,b,c},其中有一個奇數的a和c,但偶數個b。

良好字符串的示例:

  • BABC
  • ABCB
  • CBBA
  • accaccac
  • AC

壞字符串

  • babcb
  • ABC
  • cbbca
  • accacca
  • AA
  • *空字符串

甚至b的我的正則表達式是b∗(ab∗ab∗)∗b∗ (我不知道在哪里包括c)

我對奇數a的正則表達式是(c|a(b|c)*a)*a(b|c)*

我的奇數c的正則表達式是(c|a(b|c)*c)*c(b|c)*

我認為常規語法看起來像這樣:

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

我覺得我很遺憾。 任何幫助表示贊賞!

這是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).

示例:

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM