473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nested FSM implementation

Hello.

I've done a bit of searching here on c.l.c, and I also checked out SMC at
soureforge, but haven't been able to find what I'l looking for.

I've got two FSMs: A and B, where I want to embed B inside one of A's
states. Plus I want to be able to use FSM B separately. Can anyone think of
an elegant way to this using C code.

SMC looked promising, however, it doesn't support nested FSMs.

Many thanks,
Paul
Aug 21 '06 #1
6 4312
In article <Mp********************@phobos.telenet-ops.be>,
Polomora <po*******@hotmail.comwrote:
>Hello.

I've done a bit of searching here on c.l.c, and I also checked out SMC at
soureforge, but haven't been able to find what I'l looking for.

I've got two FSMs: A and B, where I want to embed B inside one of A's
states. Plus I want to be able to use FSM B separately. Can anyone think of
an elegant way to this using C code.

SMC looked promising, however, it doesn't support nested FSMs.

Many thanks,
Paul
LBJ took the IRT...

Aug 21 '06 #2
Polomora wrote:
Hello.

I've done a bit of searching here on c.l.c, and I also checked out SMC at
soureforge, but haven't been able to find what I'l looking for.

I've got two FSMs: A and B, where I want to embed B inside one of A's
states. Plus I want to be able to use FSM B separately. Can anyone think of
an elegant way to this using C code.

SMC looked promising, however, it doesn't support nested FSMs.

Many thanks,
Paul

Acronyms are a plague

if (FSM == "Finite State Machine") {
printf("Off topic in this newsgroup\n");
printf("Use another newsgroup\n");
}
else
{
printf("What is a \"FSM\"\n");
}
Aug 21 '06 #3
Polomora wrote:
Hello.

I've done a bit of searching here on c.l.c, and I also checked out SMC at
soureforge, but haven't been able to find what I'l looking for.

I've got two FSMs: A and B, where I want to embed B inside one of A's
states. Plus I want to be able to use FSM B separately. Can anyone think of
an elegant way to this using C code.

SMC looked promising, however, it doesn't support nested FSMs.

Many thanks,
Paul

Think of a deterministic FSM as a 2-dim table. The row labels are
states, the column labels are inputs. The table entries have two
parts: an action and a transition to the appropriate state following
that action.

The FSM must have a place to keep the current state. Before you
enter the FSM you must initialize the state to 0. You also need
a function that translates your input data to a column label. To
get to the appropriate table entry you compute an offset from
the base address:

offset_in_address_units = column# + row# * width

where width is the number of cells in a row.

Perhaps a struct would be the best way to program this, with each
cell holding two function addresses, one for the action and one
for the next state transition. Alternatively you might represent
the FSM as a giant switch construct.

I don't know any elegant way to do this in C. It would probably
be easier in an object-oriented language like C++ where you can
package code with the data structure. I have only done it in Forth
where it was also relatively easy.

You might try to devise a mini-language for constructing FSMs and
implementing its translation to C with YACC.

Once you have done this, however, and represented the FSM as a
function, you can easily embed that function as an action in
another FSM.

Finally, I have an online article on FSMs that mentions some
C software tools that were available several years ago:

http://galileo.phys.virginia.edu/cla...all01/fsm.html

You might try to see whether some of these are still available
before trying to roll your own.

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
Aug 21 '06 #4

"Julian V. Noble" <jv*@virginia.eduwrote in message
news:ec**********@murdoch.acc.Virginia.EDU...
Think of a deterministic FSM as a 2-dim table. The row labels are
states, the column labels are inputs. The table entries have two
parts: an action and a transition to the appropriate state following
that action.

The FSM must have a place to keep the current state. Before you
enter the FSM you must initialize the state to 0. You also need
a function that translates your input data to a column label. To
get to the appropriate table entry you compute an offset from
the base address:

offset_in_address_units = column# + row# * width

where width is the number of cells in a row.

Perhaps a struct would be the best way to program this, with each
cell holding two function addresses, one for the action and one
for the next state transition. Alternatively you might represent
the FSM as a giant switch construct.
Using a struct eliminates any correlation with the data in the FSM table.
Also, alignment issues and padding problems may slow the code down. I would
recommend creating two 2-dim tables ("arrays" in C).
Finally, I have an online article on FSMs
<snip>
>
http://galileo.phys.virginia.edu/cla...all01/fsm.html

I don't know any elegant way to do this in C. It would probably
be easier in an object-oriented language like C++ where you can
package code with the data structure. I have only done it in Forth
where it was also relatively easy.
I found your article to be interesting, so here is a Public Domain C version
of the example on your web-page. Unfortunately, in C there is no portable
way to get a character without echoing it to the screen. C always combines
FORTH's KEY and EMIT. Since this is necessary for your FSM example, the
code has some DOS specific code which should be easy to modify for other
OS's such as Linux.

/* port of a FORTH Finite State Machine by JV Noble */
/* C version by Rod Pemberton */
/* released to the Public Domain */
/* September 8th, 2006 */
#if 0
http://galileo.phys.virginia.edu/cla...all01/fsm.html
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h/* for non-echo get() */

void getstate(unsigned char *ch)
{
/* These two commented out are ISO, */
/* but they echo which we don't want... */
/* *ch=getchar(); */
/* scanf("%c",ch); */

/* non-ISO, non-echo getc() */
/* this will need to be modified */
/* to work for non-DOS systems */
while(!kbhit());
*ch=getch();
if(*ch==0x1a) /* ctrl-Z for EOF */
exit(EXIT_SUCCESS);
}

void echo(unsigned char ch)
{
printf("%c",ch);
fflush(stdout);
}

int other(unsigned char ch)
{
int result=0;

/* empty */
return(result);

}

int digit(unsigned char ch)
{
int result=0;

/* isdigit() is fine too... */
if(strchr("0123456789",ch)!=NULL)
result=1;
return(result);
}

int minus(unsigned char ch)
{
int result=0;

if(ch=='-')
result=1;
return(result);
}

int dp(unsigned char ch)
{
int result=0;

if(ch=='.')
result=1;
return(result);
}
#define MAXSTATES 3
#define MAXINPUTS 4

int main(void)
{
unsigned char state=0,input=0,mystate;

/* RP chose to use two arrays instead */
/* of a struct with pointers to functions */
/* to keep the initialization similar to */
/* JV Noble's FORTH FSM tables */

unsigned char action[MAXSTATES][MAXINPUTS]
={
{'X','E','E','E'},
{'X','E','X','E'},
{'X','E','X','X'} /* no comma */
};
unsigned char transition[MAXSTATES][MAXINPUTS]
={
{0,1,1,2},
{1,1,1,2},
{2,2,2,2} /* no comma */
};

while(1)
{
getstate(&mystate);
input=0*other(mystate)
+1*digit(mystate)
+2*minus(mystate)
+3*dp(mystate);
switch(action[state][input])
{
case 'X': /* no action */
break;
case 'E': echo(mystate);
break;
}
state=transition[state][input];
}
}
Sincerely,

Rod Pemberton
Sep 8 '06 #5
Polomora wrote:
I've done a bit of searching here on c.l.c, and I also checked out SMC at
soureforge, but haven't been able to find what I'l looking for.

I've got two FSMs: A and B, where I want to embed B inside one of A's
states. Plus I want to be able to use FSM B separately. Can anyone think of
an elegant way to this using C code.

SMC looked promising, however, it doesn't support nested FSMs.
I've never used any kind of infrastructure for implementing a finite
state machine (though I have heard of Teja). I just usually code them
up directly. But it seems to me that you can always have multiply
declared states, and transition through them easily, and that nesting
should not be a problem. I would say that an infrastructure that
didn't allow nesting is pretty weak and not really adding any value at
all versus just straight coding it (presumably a useful infrastructure
would make the nesting itself comparably useful.)

Unfortunately, I don't really have anything insightful to add, except
to say -- is just coding it directly really that bad?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 8 '06 #6
I've done a bit of searching here on c.l.c, and I also checked out SMC at
soureforge, but haven't been able to find what I'l looking for.

I've got two FSMs: A and B, where I want to embed B inside one of A's
states. Plus I want to be able to use FSM B separately. Can anyone think of
an elegant way to this using C code.

SMC looked promising, however, it doesn't support nested FSMs.
I would recommend moving over to C++. The following articles would
help:

http://www.eventhelix.com/RealtimeMa...ateMachine.htm

--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Model in Plain Text; Generate Call Flow Diagrams in PDF/Word

Sep 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12187
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method...
3
363
by: Vinodh Kumar P | last post by:
I abstract a Car, and the parts wihtin a car, with a C++ class. Since only my version of class Car uses the classes defined for its parts, is it a good practice to do like this? class Car {...
5
3383
by: Andy | last post by:
How can you unit test nested functions? Or do you have to pull them out to unit test them, which basically means I will never use nested functions. Also, same thing with private member functions...
11
3564
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
8
16846
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
1
1115
by: Adam Knight | last post by:
Hi all, I have a data list whose item template contains a user control which consists of a datagrid. Everything is working fine, except one glaring problem!!! I enter updated info into the...
2
2030
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
37
2731
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
1
1363
by: Nike | last post by:
I have a small question w.r.t nested class.. I see in many codes nested classes are declared as public.. First, of all my understanding is that you go in for a nested class if you are sure that...
2
1645
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.