Regular Expression in Compiler Design

In Compiler Design, regular expressions are concise notations used to define and recognize patterns in source code. They play a crucial role in lexical analysis, where they are used to identify and extract tokens (e.g., keywords, identifiers) from code. Regular expressions help in defining the syntax of programming languages, facilitating the transformation of human-readable code into machine-readable forms during compilation.

Regular Expression in Compiler Design

Regular Expression 

  • The language accepted by finite automata can be easily described by simple expressions called Regular Expressions. It is the most effective way to represent any language.
  • The languages accepted by some regular expression are referred to as Regular languages.
  • A regular expression can also be described as a sequence of pattern that defines a string.
  • Regular expressions are used to match character combinations in strings. String searching algorithm used this pattern to find the operations on a string.
For instance: 
  • In a regular expression, x* means zero or more occurrence of x. It can generate {ε, x, xx, xxx, xxxx, .....}
  • In a regular expression, x+ means one or more occurrence of x. It can generate {x, xx, xxx, xxxx, .....}

Operations on Regular Language

The various operations on regular language are:

Union: If L and M are two regular languages then their union L U M is also a regular language. 
L U M = {s | s is in L or s is in M} 

Intersection: If L and M are two regular languages then their intersection is also a regular language.
L ⋂ M = {st | s is in L and t is in M} 

Kleen closure: If L is a regular language then its Kleen closure L1* will also be a regular language. 
L* = Zero or more occurrence of language L.

Examples

Example 1: Write the regular expression for the language accepting all combinations of a's, over the set ∑ = {a}

Solution: All combinations of a's means 'a' may be zero, single, double and so on. If a is appearing zero times, that means a null string. That is we expect the set of {ε, a, aa, aaa, ....}. So we give a regular expression for this as: 
R = a*
That is Kleen closure of a. 

Example 2: Write the regular expression for the language accepting all combinations of a's except the null string, over the set ∑ = {a} 

Solution: The regular expression has to be built for the language L = {a, aa, aaa, ....}. This set indicates that there is no null string. So we can denote regular expression as: 
R = a+

Example 3: Write the regular expression for the language accepting all the string containing any number of a's and b's. 

Solution: The regular expression will be: 
R.E. = (a + b)* 
This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, .....}, any combination of a and b. The (a + b)* shows any combination with a and b even a null string.

Example 4: Write the regular expression for the language accepting all the string which are starting with 1 and ending with 0, over ∑ = {0, 1}. 

Solution: In a regular expression, the first symbol should be 1, and the last symbol should be 0. The R.E. is as follows: 
R = 1 (0+1)* 0 

Example 5: Write the regular expression for the language starting and ending with a and having any having any combination of b's in between. 

Solution: The regular expression will be: 
R = a b* a

Example 6: Write the regular expression for the language starting with a but not having consecutive b's.

Solution:  The regular expression has to be built for the language: L = {a, aba, aab, aba, aaa, abab, .....}. The regular expression for the above language is: 
R = {a + ab}* 

Example 7: Write the regular expression for the language accepting all the string in which any number of a's is followed by any number of b's is followed by any number of c's. 

Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be:
R = a*b*c*
Next Post Previous Post
No Comment
Add Comment
comment url