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

📅  最后修改于: 2021-06-28 07:44:18             🧑  作者: Mango

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

正则表达式:

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

上下文无关的语法:

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

* e表示ε。

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

  1. 每个州都有一个非终端符号。
  2. 如果状态A在符号a上过渡到状态B
  3. 如果状态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.