Craig Otis
Craig Otis

Reputation: 32054

Is this regular expression simplification correct?

I was reading the "How Browsers Work" article here: How Browsers Work (It's a great read.)

But at one point, they mention this as a rule of their grammar defining an integer:

INTEGER :0|[1-9][0-9]*

Is it, or is it not, exactly the same (and simpler) to write:

INTEGER :[1-9]*[0-9]

I couldn't think of a case that did not satisfy both rules, nor a reason why the first rule would be preferred.

Is there a reason for including a simple base case (such as 0), or is it just pedantics?

Upvotes: 3

Views: 67

Answers (2)

Craig Otis
Craig Otis

Reputation: 32054

They are not the same, as my simplification can only include one zero, and only if it is a trailing zero. Eg. this case does not match for the simplified rule:

101

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

These two expressions are different: the first one will take 101, but the second one wouldn't. The expression from the book is pretty good at matching integers while disallowing leading zeros.

Upvotes: 2

Related Questions