正规文法、正规表达式、有限自动机及其之间的转换(笔记)

这篇具有很好参考价值的文章主要介绍了正规文法、正规表达式、有限自动机及其之间的转换(笔记)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

The Equivalent Transforming among RG, RE and FA

正规文法

A Grammar G is a quadruple (四元组):G = (VN, VT, S, P )

Where,

  • VN is a finite set of nonterminals.
  • VT is a finite set of terminals.
  • S is the start symbol, S ∈ \in VN.
  • P is a finite set of productions (产生式).

Regular Grammar (RG) (正规文法):

α∈VN and β ∈VT∪VTVN

正规表达式

Regular Expression:

Regular expressions over ∑ are defined as :

  1. ε and ϕ \phi ϕ are RE’s, denoting the sets {ε} and Φ , respectively ;
  2. Any a ∈ \in ∑ , a is an RE, denoting the set { a };
  3. If a and b are RE’s, denoting the sets A and B respectively, then a b , a | b, and a* are RE’s, denoting the sets AB, AUB and A* respectively;
  4. Nothing is an RE unless it follows from 1 to 3 finite times.

有限自动机

确定的有限自动机

Deterministic Finite Automata:

A Deterministic Finite Automaton (DFA) is a quintuple (五元组)

M = (S, ∑ , f, s0 , Z),

where

  • S is a finite set of states;
  • ∑ is the set of input symbols;
  • f : S×∑ →S, the transition function;
  • s0 ∈ \in S, the initial state;
  • Z ⊆ \subseteq S, the set of final states.

非确定有限自动机

Nondeterministic Finite Automata:

An Nondeterministic Finite Automata (NFA) is also a quintuple

M = ( S, ∑ , f, s0 , Z),

where:

  • S is a finite set of states;
  • ∑ is the set of input symbols;
  • f : S×∑* →ρ(S), the transition function;
  • s0 ∈ \in S, the initial state;
  • Z ⊆ \subseteq S, the set of final states.

Difference between DFA & NFA

The definitions of transition functions of DFA and NFA are different:

​ DFA f: S×∑ →S

​ NFA f: S×∑* →ρ(S)

That means: a state of DFA has a unique next state and can not transit without input, but a state of NFA may have more than one next states and can transit without input.

正规表达式转换为正规文法

Transforming RE to RG

Let r be an RE on∑. Construct G = (VN, VT, S, P):

  1. Initialization: VT=∑; P = {S → r}; VN ={S};

  2. For any production in P, rewrite it as follows:

    ⑴ A → x*y => A → xA |y.

    ⑵ A→x | y => A → x | y;

    ⑶A → xy => A → xB B → y; VN = VN ∪{B};

    ⑷A→(x | y)B =>A → xB | yB; VN = VN ∪{B};

    where x and y be RE’s, B be a new nonterminal.

  3. Repeat step 2 until each rule has one termianal.

样例

Transform a(a | d)* to RG

VT={a, b}; P = {S → a(a | d)* }; VN ={S};

S → a(a | d)* => S → aA A → (a | d)*

A → (a | d)* => A → (a | d)A |ε

A → (a | d)A => A → aA | dA

Finally we get the regular grammar G is

({S, A}, {a, b}, S, {S→ aA A→ aA|dA|ε})

正规文法转换为正规表达式

Transforming RG to RE

  1. Transform RG to a group of equations by replacing → and | as = and +, respectively.
  2. For equations X = aY + a and Y = b, we replace Y by b, and get X = ab + a.
  3. If there is an equation X=rX + t, we have the solution X = r*t;
  4. Repeat step 2 and 3, until the solution S=r is got. Then r is the corresponding RE.

样例

Consider the grammar G:S → aS | aA A → bB B → aB | a

  • Transform the rules into a group of equations:

    S = aS + aA (1)

    A = bB (2)

    B = aB + a (3)

  • For equation 3 we have: B = a*a;

  • For equation 2 we have: A = ba*a;

  • For equation 1 we have: S = aS + aba*a;

  • Finally we have: S = a*aba*a;

正规文法转换为有限自动机

Transforming RG to FA

G = (VN, VT, S, P) is a right linear grammar, there is an FA M such that L(M) = L(G).

Let M = (Q, ∑, f, s0 , {Z}), where

∑= VT; Q = VN∪{Z}; Z ∉ \notin / VN; s0= S;

For ∀ \forall A→tB∈P, t∈VT∪{e}, A, B∈VN, then f(A, t) = B;

For ∀ \forall A→t∈P, A∈VN, t∈VT∪{ε}, then f(A, t) = Z.

样例

Given G=({A,B,C,D},{0,1},f,A,P),

P = {A → 0 | 0B | 1D B → 0D | 1C

​ C → 0 | 0B| 1D D → 0D | 1D }

  • Construct M = (Q,∑,f,A,{Z}):
  • Q={A,B,C,D,Z}, ∑ = {0, 1},
  • f(A, 0)=B, f(A, 0)=Z, f(A, 1)=D,
  • f(B, 0)=D, f(B, 1)=C,
  • f(C, 0)=B, f(C, 0)=Z, f(C, 1)=D,
  • f(D, 0)=D, f(D, 1)=D.

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

有限自动机转换为正规文法

Transforming FA to RG

M=(Q, ∑, f, s0 , F) is a DFA, there is a right linear grammar G such that L(M) = L(G).

Let G = (VN, VT, S, P), where

VT = ∑; VN = Q; S = s0;

For ∀ \forall t∈VT, ∀ \forall A, B∈VN, if f(A, t) = B

then If B ∈F then A →tB | t∈P

​ else A→tB∈P;

样例

Given M=({0,1},{A,B,C,D},f,A,{B})

  • Construc G = (VN,VT, A, P),

  • VN = {A, B, C, D},

  • VT = {0, 1},

  • P = { A → 0B | 0 | 1D

    ​ B → 0D | 1C

    ​ C → 0B | 0 | 1D D → 0D | 1D }

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

正规表达式转换为有限自动机

Transforming RE to FA

Given an RE r, there is an FA M, such that L(M) = L®.

  • Basis: |r| = 1.
  • We prove it by induction on the length of r.

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

Assume that for any RE r1 and r2, if |r1| < k and |r2| < k, they have their NFA M1 and M2.

  • For any RE r, |r| = k, we have:

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

Initialization: set up two state X and Y, such as:

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

Repeat dividing the RE r on the arcs according to the following rules (1) to (3):

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

Until each arc labeled by a symbol.

样例

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

有限自动机转换为正规表达式

Transforming FA to RE

Initialization: Add two state : X , Y. X is the unique initial state and Y is the unique final state

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

Repeat linking the RE r on the arcs according to the following rules (1) to (3):

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

正规文法、正规表达式、有限自动机及其之间的转换(笔记)

样例

正规文法、正规表达式、有限自动机及其之间的转换(笔记)文章来源地址https://www.toymoban.com/news/detail-473252.html

到了这里,关于正规文法、正规表达式、有限自动机及其之间的转换(笔记)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 自动化测试学习(七)-正则表达式,你真的会用吗?

    目录 一、正则表达式在python中如何使用 二、用正则表达式匹配更多模式 三、常用字符分类的缩写代码 总结 所谓正则表达式(regex),就是一种模式匹配,学会用正则匹配,就可以达到事半功倍的效果。 1.导入正则表达式模块 2.创建正则表达式对象,以电话号码为例 Tips:

    2023年04月09日
    浏览(33)
  • 谷粒商城p45-自动装配-stream流-lambda表达式

            自动装配CategoryService对象         调用对象中的listWithTree() 自动装配相关,为什么用resource 不用 autowired:参考下面文章 http://t.csdn.cn/bS1GM http://t.csdn.cn/bS1GM @Autowired注入方式是byType,如果只有一个实现类,会装配那个实现类。如果有两个实现类,会报错。

    2024年02月10日
    浏览(30)
  • 【数据结构】超详细讲解:算术表达式转化为后缀表达式、前缀表达式、表达式树的构建

    作者: 努力学习的大一在校计算机专业学生,热爱学习和创作。目前在学习和分享:算法、数据结构、Java等相关知识。 博主主页: @是瑶瑶子啦 所属专栏: 【数据结构】:该专栏专注于数据结构知识,持续更新,每一篇内容优质,浅显易懂,不失深度! 近期目标: 写好专栏

    2024年02月08日
    浏览(41)
  • 《数据结构》:中缀表达式转后缀表达式 + 后缀表达式的计算

    补充了一个判断输入中缀表达式 合法性 的代码: 《数据结构》:中缀表达式合法性判断_Amentos的博客-CSDN博客   目录 一、基本概念 二、中缀表达式转后缀表达式    例       中缀表达式  2*(3+5)+7/1-4  转换为后缀表达式 三、后缀表达式的计算    例       后缀表达式

    2024年02月03日
    浏览(49)
  • 【数据结构与算法】【12】前缀表达式、中缀表达式、后缀表达式

    什么是前缀表达式、中缀表达式、后缀表达式 前缀表达式、中缀表达式、后缀表达式,是通过树来存储和计算表达式的三种不同方式 以如下公式为例 ( a + ( b − c ) ) ∗ d ( a+(b-c) )*d ( a + ( b − c ) ) ∗ d 通过树来存储该公式,可以表示为 那么问题就来了,树只是一种抽象的数据

    2024年02月08日
    浏览(38)
  • 【数据结构】13:表达式转换(中缀表达式转成后缀表达式)

    从头到尾依次读取中缀表达式里的每个对象,对不同对象按照不同的情况处理。 如果遇到空格,跳过 如果遇到运算数字,直接输出 如果遇到左括号,压栈 如果遇到右括号,表示括号里的中缀表达式已经扫描完毕,将栈顶的运算符弹出并输出, 直至遇到左括号(左括号出栈

    2024年02月19日
    浏览(42)
  • 中缀表达式转后缀表达式

      一种不需要括号的后缀表达法,也把它称为 逆波兰 表示,是波兰逻辑学家卢卡西维奇(Lukasiewicz)发明的一种表示表达式的方法。   中缀表达式指的是“9+(3-1)×3+8÷2”,这种就是我们通常见到的书写算式顺序,要计算中缀表达式则首先要将字符串转换为后缀表达式,即

    2023年04月08日
    浏览(36)
  • Golang通过栈实现表达式运算(中缀表达式转后缀表达式解析语法)

    需求背景:将string表达式数组 [title == AUSU ( header == Wecome || brand != AC68U )] 解析并使用ES查询得到运算结果。 分析:带有括号的表达式,需要根据优先级解析,可将中缀表达式转换为后缀表达式,去除括号

    2024年02月14日
    浏览(38)
  • 【正则表达式】正则表达式常见匹配模式

    模式 描述 w 匹配字母数字及下划线 W 匹配非字母数字下划线 s 匹配任意空白字符,等价于 [tnrf]. S 匹配任意非空字符 d 匹配任意数字,等价于 [0-9] D 匹配任意非数字 A 匹配字符串开始 Z 匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串 z 匹配字符串结

    2024年02月09日
    浏览(65)
  • 正则表达式 (用于灵活匹配文本的表达式)

    目录 . * 用于匹配任意单个字符,除了换行符。 例如使用正则表达式 a.b, 它可以匹配aab、acb、a#b 用于匹配前一个字符零次或多次。 例如,使用正则表达式 ab*c ,它可以匹配 \\\"ac\\\"、\\\"abc\\\"、\\\"abbc\\\",因为 b* 表示匹配零个或多个字符 \\\"b\\\"。所以,这个表达式可以匹配 \\\"ac\\\"(零个 \\\"b\\\"),

    2024年01月16日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包