Regular Expression Vs Context Free Grammar

Regular Expressions are capable of describing the syntax of Tokens. Any syntactic construct that can be described by Regular Expression can also be described by the Context free grammar.

Regular Expression:

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

Context-free grammar:

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

*e denotes epsilon.

The Context-free grammar form NFA for the Regular Expression using the following construction rules:

  1. For each state there is a Non-Terminal symbol.
  2. If state A has a transition to state B on a symbol a
  3. IF state A goes to state B, input symbol is e
  4. If A is accepting state.
  5. Make the start symbol of the NFA with the start symbol of the grammar.

Every Regular set can be described the Context-free grammar that’s why we are using Regular Expression. There are several reasons and they are:

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.