将奇数甚至正则表达式与常规语法相结合?

时间:2017-10-27 22:08:46

标签: regex prolog grammar dcg

我有这门课,我需要为其编写常规语法。语法是{a,b,c},其中有一个奇数的a和c,但偶数个b。

良好字符串的示例:

  • BABC
  • abcb
  • cbba
  • accaccac
  • AC

坏字符串

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

我的正则表达式是public void LiveAnalysisIsDone() { string LiveAnalysisCompleteText = System.Windows.Application.Current.TryFindResource("LiveAnalysisComplete").ToString(); if (CustomControlContent != null) { //Remove Children from UserControl if (CustomControlContent.GetType() != typeof(BioRad.ddPCR.UI.Views.Analysis.Reports.ReportGeneratorUserControl)) { this.Dispatcher.BeginInvoke((Action)(() => { var content = CustomControlContent.Content as Grid; if (content != null) { var Panel = content.Children[1] as StackPanel; (Panel.Children[0] as TextBlock).Text = LiveAnalysisCompleteText; } })); } } } (我不知道在哪里包含c)

我对奇数a的正则表达式为b∗(ab∗ab∗)∗b∗

奇怪的我的正则表达式是(c|a(b|c)*a)*a(b|c)*

我认为常规语法看起来像这样:

(c|a(b|c)*c)*c(b|c)*

我认为很明显我很失落。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

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