× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Finite state machines
  • From: "James W. Kilgore" <qappdsn@xxxxxxxxxxxxx>
  • Date: Fri, 18 Aug 2000 14:40:12 -0700
  • Organization: Progressive Data Systems, Inc.

Silvio,

The News/400 online archives only goes back to 1995, so I'll try to explain, by
example, finite state machines the best I can without all the fancy diagrams 
that
were in the article.

I'm sure that others will point out where I missed something, but here goes:

Finite state machines for the purpose of parsing a character string (In this
example the string is: <ItemCode>1234abc</ItemCode>)

A finite state machine operates under the principle that upon reading a single
character of an input string, the program is in a particular state and the data
just read may cause the program to change to a new state. This transaction to a 
new
state may also result in additional action.

By analyzing your potential input string, you create a list of all possible
occurrences.
In this case I have:
    1) the possibility of a blank(s)
    2) a '<'
    3) a '/'
    4) a '>'
    5) a non blank not previously mentioned
    6) end of string

You will want to create an array of these values, but make the elements longer 
than
1 position like this:

 *************** Beginning of data ******************************
 D                 DS            12
 D DataARRAY                     12
 D  Data                          2    Dim(6) Overlay(DataARRAY)
 D                         1      2    Inz('  ')
 D                         3      4    Inz(' <')
 D                         5      6    Inz(' /')
 D                         7      8    Inz(' >')
 D                         9     10    Inz('nb')
 D                        11     12    Inz('es')
 ****************** End of data *********************************

You will also define arrays to contain your input string, an array to contain 
the
extracted keyword and an array to contain the extracted value.  I won't bother
posting that code.

You will also need to create a two dimensional array for the state transition
table.  For those that have not created two dimensional arrays it's simply where
the element of one array in movea'd into a second array therefore breaking up 
the
elements.

One array will be 6 elements of let's say 7 characters per.  It's 6 elements
because we only have 6 possible input values.  They are 7 characters each in the
format sss/aaa where sss is the new state and aaa is the action to be performed.
The '/' is there for looks.

This is 'finite'. :)  Let's see, 6 times 7 is 42 (-the- answer) so the other 
array
has elements that are 42 characters each.  The number of elements will be
determined by the number of states that you could possible have.  Which state 
you
are in will determine the array index.  So that definition will look something 
like
this:

*************** Beginning of data ******************************
 D                 DS            42
 D StateRowDS                    42
 D  StateRow                      7    Dim(6)Overlay(StateRowDS)

 D                 DS           168
 D StateColDS                   168
 D  StateCol                     42    Dim(4) Overlay(StateColDS)
 D*    These columns match array Data:      '  '   ' <'   ' /'   ' >'   'nb'   
'es'

 D                         1     42    Inz('001/   002/inz000/   000/   000/
000/   ')
 D                        43     84    Inz('002/   000/   000/   003/
002/str000/   ')
 D                        85    126    Inz('003/dta004/   000/   000/
003/dta000/   ')
 D                       127    168    Inz('004/   000/   004/   000/LR 004/
000/   ')
  ****************** End of data *********************************

Your mainline code will look something like this:

>
  CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq....
  *************** Beginning of data ******************************************
 C                   Z-ADD     1             State             3 0
 C     State         DOWNE     *ZERO
 C                   EXSR      ReadInpChr
 C* Determine the character just read (default to element 5 'nb')
 C                   Z-ADD     1             Indx              1 0
 C     Character     LOOKUP    Data(Indx)                             90
 C  N90              Z-ADD     5             Indx
 C* Extract the state row to determine what to do
 C                   EVAL      StateRowDS = StateCol(State)
 C                   MOVEL     StateRow(Indx)State
 C                   MOVE      StateRow(Indx)Action            3
 C                   EXSR      doAction
 C                   ENDDO
  ****************** End of data *********************************************

Routine ReadInpChr performs a simple loop through your input array.  If at end 
of
array Character will contain 'es' (end of string).

So let's walk through this:
The program starts at State = 1
The first character is read and contains '<'
The LOOKUP sets Indx to 2
StateRow is set to contain the first element of StateCol
Having done that, the second element of StateRow is used to determine the new 
state
and what action should be performed.  In this case, the state is changed to 2 
and
'inz' action is set.

Had the first character been blank, the state would have stayed at 1 and no 
action
set.

Had the first character been anything but a blank or a '<' the state would have
been set to zero and the program would drop out of the loop.

Now back to the top of the loop, the second character is read and contains a non
blank character, but not any character in array Data so Indx is set to 5.

StateRow is set to contain the second element of StateCol (we are at state = 2) 
and
the fifth element is used to set the new state and action.  In this case, the 
state
stays at 2 and 'str' action is set.  'str' is used in the doAction routine to 
store
the keyword.

Had the character been blank the state would have stayed at 2 and no action set.

This looping will continue until Character = '>' at which point the state will
change to 3 and the previous 'str' action would have built the keyword array.

During state 3 we are storing the data value, including imbedded blanks, until 
we
read a '<' and switch to state 4.

As you can see this simple example only has 5 states and as different conditions
occur you add more states.  But a whole lot less code than a brut force method.

HTH,
J. Kilgore


Silvio Santos wrote:

> Hi James can you arrange me that article ?

+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.