# 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 Greibach Normal Form
- Converting Context Free Grammar to Chomsky Normal Form
- Regular Expressions, Regular Grammar and Regular Languages
- Context-sensitive Grammar (CSG) and Language (CSL)
- Star Height of Regular Expression and Regular Language
- Check if the language is Context Free or Not
- Classification of Context Free Grammars
- Simplifying Context Free Grammars
- Closure Properties of Context Free Languages
- Regular expression to ∈-NFA
- Designing Finite Automata from Regular Expression (Set 6)
- Generating regular expression from Finite Automata
- Designing Finite Automata from Regular Expression (Set 2)

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.