将正则表达式转换为 CFG

发布于 2024-08-29 09:14:34 字数 173 浏览 2 评论 0原文

如何将一些常规语言转换为其等效的上下文无关语法? 是否有必要构建与该正则表达式相对应的DFA,或者这种转换是否有某种规则?

例如,考虑以下正则表达式

01+10(11)*

我该如何描述上面RE对应的语法?

How can I convert some regular language to its equivalent Context Free Grammar?
Is it necessary to construct the DFA corresponding to that regular expression or is there some rule for such a conversion?

For example, consider the following regular expression

01+10(11)*

How can I describe the grammar corresponding to the above RE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十秒萌定你 2024-09-05 09:14:34
  • 将A+B改为语法

    <前><代码>G ->一个
    G->乙

  • 将 A* 更改为

    <前><代码>G -> (空的)
    G->股份公司

  • 将 AB 更改为

    <前><代码>G -> AB

并对 A 和 B 进行递归处理。基本情况是空语言(无产生式)和单个符号。

在您的情况下

 A -> 01
 A -> 10B
 B -> (empty)
 B -> 11B

如果语言是由有限自动机描述的:

  • 使用状态作为非终结符号
  • 使用语言作为终结符号
  • 集添加转换 p -> aq 对于任何转换 p ->原始自动机中字母 a 上的 q
  • 使用初始状态作为语法中的初始符号
  • Change A+B to grammar

    G -> A
    G -> B
    
  • Change A* to

    G -> (empty)
    G -> A G
    
  • Change AB to

    G -> AB
    

and proceed recursively on A and B. Base cases are empty language (no productions) and a single symbol.

In your case

 A -> 01
 A -> 10B
 B -> (empty)
 B -> 11B

If the language is described by finite automaton:

  • use states as nonterminal symbols
  • use language as set of terminal symbols
  • add a transition p -> aq for any transition p -> q on letter a in the original automaton
  • use initial state as initial symbol in the grammar
っ左 2024-09-05 09:14:34

我猜你的意思是用 V->w 形式的规则将其转换为正式语法,其中 V 是非终结符,w 是终结符/非终结符字符串。首先,您可以简单地说(混合 CFG 和正则表达式语法):

S -> 01+10(11)*

其中 S 是开始符号。现在让我们稍微分解一下(为了清晰起见,添加空格):

S -> 0 A 1 0 B
A -> 1+
B -> (11)*

关键是将 *+ 转换为递归。首先,我们将通过插入接受空字符串的中间规则将 Kleene 星号转换为加号:

S -> 0 A 1 0 B
A -> 1+
B -> (empty)
B -> C
C -> (11)+

最后,我们将 + 表示法转换为递归:

S -> 0 A 1 0 B
A -> 1
A -> A 1
B -> (empty)
B -> C
C -> 11
C -> C 11

要处理 x?,只需将其拆分为生成空的规则和生成 x 的规则。

I guess you mean convert it to a formal grammar with rules of the form V->w, where V is a nonterminal and w is a string of terminals/nonterminals. To start, you can simply say (mixing CFG and regex syntax):

S -> 01+10(11)*

Where S is the start symbol. Now let's break it up a bit (and add whitespace for clarity):

S -> 0 A 1 0 B
A -> 1+
B -> (11)*

The key is to convert *es and +es to recursion. First, we'll convert the Kleene star to a plus by inserting an intermediate rule that accepts the empty string:

S -> 0 A 1 0 B
A -> 1+
B -> (empty)
B -> C
C -> (11)+

Finally, we'll convert + notation to recursion:

S -> 0 A 1 0 B
A -> 1
A -> A 1
B -> (empty)
B -> C
C -> 11
C -> C 11

To handle x?, simply split it into a rule producing empty and a rule producing x .

嘿看小鸭子会跑 2024-09-05 09:14:34

实际上,不同的CFG语法可以产生相同的语言。因此,给定一个正则表达式(正则语言),它映射回 CFG 不是唯一的。

当然,您可以构造一个产生给定正则表达式的 CFG。上面的答案展示了实现这一目标的一些方法。

希望这能给您一个高层次的想法。

Actually, different CFG grammars can produce the same language. So given a regular expression (regular language), its mapping back a CFG is not unique.

Definitely, you can construct a CFG that result in a given regular expression. The above answers shown some ways to achieve this.

Hope this gives you a high level idea.

音盲 2024-09-05 09:14:34

对于示例,以下语法是等效的:

S -> 01|10A
A -> 11A|empty

For the example, the following grammar is equivalent:

S -> 01|10A
A -> 11A|empty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文