- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the Rules of Regular Expressions in Compiler Design?
The language accepted by finite automata can be simply defined by simple expressions known as Regular Expressions. It is an effective approach to describe any language. A regular expression can also be represented as a sequence of patterns that represent a string. Regular expressions are used to connect character sequence in strings. The string searching algorithm used this pattern to discover the operations on a string.
There are various rules for regular expressions which are as follows −
ε is a Regular expression.
Union of two Regular Expressions R1 and R2.
i.e., R1 + R2 or R1|R2 is also a regular expression.
Concatenation of two Regular Expressions R1 and R2.
i.e., R1 R2 is also a Regular Expression.
Closure of Regular Expression R, i.e., R* is also a Regular Expression.
If R is a Regular Expression, then (R) is also a Regular Expression.
Algebraic Laws
R1|R2=R2|R1 or R1+ R2=R2+ R1 (Commutative) R1| (R2|R3)=(R1| R2)|R3 (Associative) Or R1+ (R2+ R3)=(R1+ R2)+R3 R1 (R2|R3)=(R1R2)R3 (Associative) R1| (R2|R3)=R1R2| R1R3 (Distributive) Or R1 (R2+ R3)=R1R2+R1R3 ε R=R ε=R (Concatenation)
Example1 − Write Regular Expressions for the following language over $\sum$ ={a,b}
- String of length zero or one.
Answer: ε | a | b or (ε+a+b)
- Strings of length two.
Answer: aa | ab | bb or (aa+ab+ba +bb)
- Strings of Even Length
Answer: (aa| ab| ba | bb)* or (aa+ab+ba +bb)*
- Set of all strings of a’s and b’s having at least two occurrences of aa.
Answer − (a+b)*aa(a+b)aa(a+b)*
Example2 − Find Regular Expressions for following language.
- L={ε,1,11,111,….}
{∴ 10=ε,11=1,12=11,13=111…..}
Answer: 1*
- L={ε,11,1111,111111,…..}
Answer: (11) $\ast$
L = Set of all strings of 0’s and 1’s = {ε,0,1,01,11,00,000,101,……}
Answer: (0+1) $\ast$ or (0|1) $\ast$
L = Set of all strings of 0’s and 1’s ending with 11.
Answer: (0+1) $\ast$ 11
L = Set of all strings of 0’s and 1’s beginning with 0 and ending with 1.
Answer: 0(0+1) $\ast$ 1
Example3 − Write Regular Expression in which the second letter from the right end of the string is 1 where $\sum$ ={0,1}.
Answer: (0+1) $\ast$ 1(0+1)
Example4 − Write Regular Expressions for the following language over $\sum$ ={a,b}
L=Set of strings having at least one occurrence of the double letter
Answer: (a+b)*(aa+bb)(a+b)*
L = Set of strings having double letter at Beginning and Ending of string.
Answer: (aa+bb)(a+b)*(aa+bb)
L = Set of strings having double letter at Beginning or on Ending of string.
Answer: (aa+bb)(a+b)*+ (a+b)*(aa+bb)+(aa+bb)(a+b)*(aa+bb)