Regular Expression Operator Precedence

For Complete YouTube Video: Click Here

In this class, We discuss Regular Expression Operator Precedence.

The reader should have a prior understanding of C operators evaluation and NFA Operators. Click Here.

Let’s refresh the concept of operator evaluation in c, and we move to regular expression operator evaluation.

In the c language, a+b*c is evaluated as b*c first, and b*c is the sum with a.

star is considered first because the star has the highest precedence.

Given below are the regular expression operators precedence highest to lowest.

Star has the highest precedence.

dot having second highest precedence.

+ is the least precedence.

Take a few examples and understand regular expression operators evaluation.

Example 1: Take a regular expression 01*+1.

The given regular expression is evaluated as (0(1)*)+1

Star is assigned to one because star has the highest precedence.

Dot has second-highest precedence, so 0.1* is taken next.

Plus, having the least precedence, so expression is evaluated as zero one star or one.

Example 2: the given regular expression is 00 + 11.

we have dot and + operators.

Dot has the highest precedence, so 0.0 is done, and 1.1 is done. Then we apply plus operator.

Our expression is 00 or 11

Example 3: The given regular expression is 111 + 001 + 101.

1.1.1 is done first.

Here we have two dot operators. So left to right is taken.

Point to understand: When we have the same precedence operator, it is evaluated left to right.

The above regular expression is evaluated as 111 or 001 or 101.