# 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:

- For each state there is a Non-Terminal symbol.
- If state A has a transition to state B on a symbol a

- IF state A goes to state B, input symbol is e

- If A is accepting state.

- 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. |

## Recommended Posts:

- Ambiguity in Context free Grammar and Context free Languages
- Ambiguity in Context free Grammar and Context free Languages
- Converting Context Free Grammar to Chomsky Normal Form
- Converting Context Free Grammar to Greibach Normal Form
- Regular Expressions, Regular Grammar and Regular Languages
- TOC | Context-sensitive Grammar (CSG) and Language (CSL)
- Star Height of Regular Expression and Regular Language
- Simplifying Context Free Grammars
- Check if the language is Context Free or Not
- Classification of Context Free Grammars
- Theory of Computation | Closure Properties of Context Free Languages
- TOC | Regular expression to ∈-NFA
- TOC | Designing Finite Automata from Regular Expression (Set 5)
- TOC | Designing Finite Automata from Regular Expression (Set 2)
- TOC | Designing Finite Automata from Regular Expression (Set 3)

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.