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:
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?