📜  正则表达式与上下文无关语法

📅  最后修改于: 2021-09-27 22:42:57             🧑  作者: Mango

正则表达式能够描述令牌的语法。任何可以用正则表达式描述的句法结构也可以用上下文无关文法描述。

正则表达式:

(a|b)(a|b|01) 

上下文无关语法:

S --> aA|bA
A --> aA|bA|0A|1A|e 

*e 表示 epsilon。

使用以下构造规则的正则表达式的上下文无关语法形式 NFA:

  1. 对于每个状态,都有一个非终端符号。
  2. 如果状态 A 在符号 a 上转移到状态 B
  3. IF状态A进入状态B,输入符号为e
  4. 如果 A 是接受状态。
  5. 用文法的开始符号制作NFA的开始符号。

每个正则集都可以用上下文无关语法来描述,这就是我们使用正则表达式的原因。有几个原因,它们是:

Regular Expressions Context-free grammar
Lexical rules are quite simple in case of Regular Expressions. Lexical rules are difficult in case of Context free grammar.
Notations in regular expressions are easy to understand. Notations in Context free grammar are quite complex.
A set of string is defined in case of Regular Expressions. In Context free grammar the language is defined by the collection of productions.
It is easy to construct efficient recognizer from Regular Expressions. By using the context free grammar, it is very difficult to construct the recognizer.
There is proper procedure for lexical and syntactical analysis in case of Regular Expressions. There is no specific guideline for lexical and syntactic analysis in case of Context free grammar.
Regular Expressions are most useful for describing the structure of lexical construct such as identifiers, constant etc. Context free grammars are most useful in describing the nested chain structure or syntactic structure such as balanced parenthesis, if else etc.
and these can’t be define by Regular Expression.