简体   繁体   English

我的Regex / DFA的常规语法

[英]Regular Grammar to my Regex/DFA

I have following regular expression: ((abc)+d)|(ef*g?) 我有以下正则表达式:((abc)+ d)|(ef * g?)

I have created a DFA (I hope it is correct) which you can see here 我创建了一个DFA(希望它是正确的),您可以在此处看到

http://www.informatikerboard.de/board/attachment.php?attachmentid=495&sid=f4a1d32722d755bdacf04614424330d2 http://www.informatikerboard.de/board/attachment.php?attachmentid=495&sid=f4a1d32722d755bdacf04614424330d2

The task is to create a regular grammar (Chomsky hierarchy Type 3) and I don't get it. 任务是创建一个常规语法(Chomsky层次结构类型3),但我不明白。 But I created a regular grammar, which looks like this: 但是我创建了一个常规语法,如下所示:

S → aT →→

T → b T→b

T → c T→c

T → dS T→dS

S → eT →→

S → eS S→eS

T → ε T→ε

T → f T→f

T → fS T→fS

T → gS T→gS

Best Regards Patrick 最好的问候帕特里克

Type 3 Chomsky are the class of regular grammars constricted to the use of following rules: Type 3 Chomsky是常规语法的一类,只能使用以下规则:

X -> aY
X -> a,

in which X is an arbitrary non-terminal and a an arbitrary terminal. 其中X是任意的非末端和任意的末端。 The rule A -> eps is only allowed if A is not present in any of the right hand sides. 仅当在任何右侧都不存在A才允许规则A -> eps

Construction 施工

We notice the regular expression consists of two possibilities, either (abc)+d or ef*g?, our first rules will therefor be S -> aT and S -> eP . 我们注意到正则表达式包含两种可能,(abc)+ d或ef * g ?,因此我们的第一个规则是S -> aTS -> eP These rules allow us to start creating one of the two possibilities. 这些规则使我们可以开始创建两种可能性之一。 Note that the non-terminals are necessarily different, these are completely different disjunct paths in the corresponding automaton. 请注意,非终端必须不同,它们是相应自动机中完全不同的分离路径。 Next we continue with both regexes separately: 接下来,我们分别继续处理两个正则表达式:

(abc)+ We have at least one sequence abc followed by 0 or more occurrences, it's not hard to see we can model this like this: (abc)+我们至少有一个序列abc,然后是0个或多个出现,不难看出我们可以像这样建模:

S -> aT
T -> bU
U -> cV
V -> aT   # repeat pattern
V -> d    # finish word

ef*g? ef * g? Here we have an e followed by zero or more f characters and an optional g, since we already have the first character (one of the first two rules gave us that), we continue like this: 在这里,我们有一个e,后跟零个或多个f个字符和一个可选的g,因为我们已经有第一个字符(前两个规则之一给了我们),我们将像这样继续:

S -> eP
S -> e    # from the starting state we can simply add an 'e' and be done with it,
          # this is an accepted word!
P -> fP   # keep adding f chars to the word
P -> f    # add f and stop, if optional g doesn't occur
P -> g    # stop and add a 'g'

Conclusion 结论

Put these together and they will form a grammar for the language. 将它们放在一起,它们将构成该语言的语法。 I tried to write down the train of thought so you could understand it. 我试图写下思路,以便您理解它。

As an exercise, try this regex: (a+b*)?bc(a|b|c)* 作为练习,请尝试以下正则表达式:(a + b *)?bc(a | b | c)*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM