State Transition Diagram: Telephone Number

In the United States, a telephone number consists of:
  1. A three-digit area code, known within the industry as an NPA (for Numbering Plan Area).
  2. A three-digit exchange, known within the industry as an NXX (I don't know what that stands for, if anything).
  3. A four-digit line number.
On a service order at my company, there are one or more blank spaces after the NPA, and a hyphen (and possibly more blank spaces) after the NXX. However, the NPA may be omitted, in which case it defaults to a value specified elsewhere on the order. The NXX may also be omitted, in which case it also defaults.

In parsing an order, therefore, my programs may encounter a telephone number in any of three different formats: NPA NXX-Line, NXX-Line, or just a bare Line. An initial block of three digits may represent an NPA or an NXX, depending on what follows it.

While this syntax is a bit confusing, it's not too bad. I could have used more conventional techniques, but I chose to write a finite state machine based on the following state transition diagram:

FSM diagram

To avoid clutter, I left out most of the error paths. Any possible input for which no transition is specified leads to an error state.

At first glance this diagram may look a little scary. If you trace through it step by step, though, it's actually pretty simple. I encoded the states in a WORKING-STORAGE table and stepped through them in a short loop.

Later, after the program was in production, I realized that the diagram is not quite as simple as it could have been. Can you spot a possible improvement?


[home]Cobol Home [gears]Finite State Machines