Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Regular Expression Vs Context Free Grammar

  • Difficulty Level : Medium
  • Last Updated : 01 May, 2019

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:

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.


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 ExpressionsContext-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
Recommended Articles
Page :

Start Your Coding Journey Now!