简体   繁体   English

从给定的正则表达式创建语法树(对于RE到DFA)

[英]Create Syntax tree from given Regular Expressions (For RE to DFA)

I'm learning Automaton Theory and facing some difficulties for converting a RE directly to DFA. 我正在学习自动机理论并面临将RE直接转换为DFA的一些困难。 This method requires to create syntax tree from the RE. 此方法需要从RE创建语法树。 But i am not able to generate. 但我无法生成。
I am given a regular expression eg a*b*(a|b)abc 给我一个正则表达式, 例如 a*b*(a|b)abc

Now i want to generate a syntax tree from this. 现在我想从中生成一个语法树。 I don't want program for it but i want to do it manually. 我不想要它的程序,但我想手动完成它。 Can anyone help me? 谁能帮我?

You need to figure out what are the operations represented by this expression, in which order they apply, and what are their operands. 您需要弄清楚此表达式表示的操作是什么,它们应用的顺序以及它们的操作数。

For instance a* means: "apply the * operator to a " . 比如a*表示:“应用*运营商a ”。 In the expression tree, you'd get a node for the star operator, and an a node for the symbol the operator acts upon. 在表达式树中,您将获得星型运算符a节点,以及操作符所依据的符号的节点。

Similarly, the | 同样, | denotes an alternation, so it would be a node in the tree, and the child nodes would be the subexpressions of the alternation. 表示交替,因此它将是树中的节点,并且子节点将是交替的子表达。

The top-level operation is simply a concatenation, that would be your root node. 顶级操作只是一个连接,它将是您的根节点。

Here's the full AST derived from these rules: 以下是从这些规则派生的完整AST:

a*b*(a|b)abc

--+ CONCAT
  |
  +-+ STAR
  | |
  | +-- a
  |
  +-+ STAR
  | |
  | +-- b
  |
  +-+ OR
  | |
  | +-- a
  | |
  | +-- b
  |
  +-- a
  |
  +-- b
  |
  +-- c

EDIT: You asked for a binary tree, the transformation should be straightforward. 编辑:你要求一个二叉树,转换应该是直截了当的。 I'll leave both trees so you can figure it out: 我会留下两棵树让你弄清楚:

              .
             / \
            .   c
           / \
          .   b
         / \
        .   a
       / \
      /   \
     /     \
    .       |
   / \     / \
  *   *   a   b
 /     \
a       b

Note that the above tree uses a left-associative concatenation operator. 请注意,上面的树使用左关联连接运算符。

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

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