Author Topic: DSL to describe a fsm?  (Read 1246 times)

0 Members and 1 Guest are viewing this topic.

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 4227
  • Country: gb
DSL to describe a fsm?
« on: January 18, 2023, 09:58:28 am »
is there anything concise and compact to describe a finite state machine?
Yup, the request sounds and smells "DSL" (domain specific language).

Or something good to help programming ;D

I am used to describe fsm-s in C, and my whole lib_tokenizer is *FULL* of fsm-s, but some are very long, so the code looks like driving in a nail with the screwdriver

Code: [Select]
switch (status.curr)
{
case ...:
    if (False)
    {
        panic();
    }
    else if (input isEqualTo ...)
    {
        status.next = ...;
        output      = ...;
    }
    else if (input isEqualTo ...)
    {
        status.next = ...;
        output      = ...;
    }
    else
    {
        status.next = ...;
        output      = ...;
    }
    break;
default:
    if (False)
    {
        panic();
    }
    else if (input isEqualTo ...)
    {
        status.next = ...;
        output      = ...;
    }
    else if (input isEqualTo ...)
    {
        status.next = ...;
        output      = ...;
    }
    else
    {
        status.next = ...;
        output      = ...;
    }
    break;
}

(
Oh, I am - "modestly" - a master in driving a nail with the screwdriver  :o

I've practiced so long you wouldn't believe how good I am!

Just, ... results are that ... nails often enter so crooked that I have to spend much more time to straighten them

So, hey, you guessed right, DSL needs because I spent a lot of time to straighten the code  ;D
)
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 4227
  • Country: gb
Re: DSL to describe a fsm?
« Reply #1 on: January 18, 2023, 10:51:42 am »
Need to describe state-transition tables
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 20509
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: DSL to describe a fsm?
« Reply #2 on: January 18, 2023, 10:56:16 am »
There have been many over the decades. Too many. But that's typical of DSLanguages IMNSHO :)

I remember one product from ?Kennedy Carter? that had been used for fly by wire software or similar.

https://smc.sourceforge.net/ is well known; I've never used it.

For anything non-trivial that expands over time, eventually you will get to the point where a serious question is which parts are best specified/designed/tested in diagrams and which in text.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 
The following users thanked this post: DiTBho

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 4227
  • Country: gb
Re: DSL to describe a fsm?
« Reply #3 on: January 18, 2023, 11:48:55 am »
Eheheh this evil monster still torments me in my sleep with its sprawling details   :o :o :o

(Xasm, dunno if works, but for sure bad dreams are made of this)
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 4227
  • Country: gb
Re: DSL to describe a fsm?
« Reply #4 on: January 18, 2023, 12:04:39 pm »
Wow, your suggest for "SMC"(1) is great!

It's Java >=v1.7 application, kind of state machine compiler, which takes a description of a state machine and generates an implementation of that state machine in C.

Amazing  :D


edit:
I access the forum via smartphone, I do not receive notifications about it.
Sorry if I missed some answers.
« Last Edit: January 18, 2023, 04:03:48 pm by DiTBho »
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Sherlock Holmes

  • Frequent Contributor
  • **
  • !
  • Posts: 570
  • Country: us
Re: DSL to describe a fsm?
« Reply #5 on: January 18, 2023, 04:38:46 pm »
Actually the way Antlr4 describes a lexical analyzer (aka "tokenizer) is such a language. The generated lexer code is an FSM to all intents and purposes. Each character read is an "event" and the lexer's iternal state fluctuate as chars are read and transition rules acted upon.

So perhaps, just perhaps, one could do this by representing the problem as a DSL rather than the desired FSM itself...

This doesn't really answer your question as such but might be food for thought...
« Last Edit: January 18, 2023, 04:42:17 pm by Sherlock Holmes »
“When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.” ~ Arthur Conan Doyle, The Case-Book of Sherlock Holmes
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 4227
  • Country: gb
Re: DSL to describe a fsm?
« Reply #6 on: January 18, 2023, 05:37:21 pm »
Years ago, I (ab-)used the ErLang Gen_Fsm trick (what?  :o) on a MIPS32 GNU/Linux router to generate fsm-tab to defeat several attacks.

My tini router looked like a blockhouse attacked by Indians, or a bay attacked by pirates, targeted by I don't know yet by whom or why.

fsm_gen was more a trick I used to defeat an annoying tcp/ip attacks by a fsm-driven-on-the-fly work-around than good practice.

Kind of, see what the attackers're doing, find a pattern in the actions done during the attacks, prepare some fsm-driven code to defend your router. Plus ip2ban

(I know, kernel space full firewalling is better, but my rooter was a "poor man" solution).

Well, maybe it was - albeit unintentional (I was really panicking) - my first "event scheduling" code.

And somehow it worked  ;D ;D ;D
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15254
  • Country: fr
Re: DSL to describe a fsm?
« Reply #7 on: January 18, 2023, 08:55:27 pm »
If your FSMs are "too long", then factor each case in separate functions. Make it clean. It will be more straightforward than spending 6 months designing/implementing a DSL for your needs, the "compiler" of which is likely to itself contain long FSMs written in C. ;D
 
The following users thanked this post: DiTBho

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 20509
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: DSL to describe a fsm?
« Reply #8 on: January 18, 2023, 09:33:39 pm »
If your FSMs are "too long", then factor each case in separate functions. Make it clean. It will be more straightforward than spending 6 months designing/implementing a DSL for your needs, the "compiler" of which is likely to itself contain long FSMs written in C. ;D

Make it the specification and design clean.[1]
Make the implementation clean.[2]
Make the correspondence between bits of the design and bits of the implementation clean and obvious.[3]
Make the implementation design pattern clean, so that one change to the design requires only one change to the implementation, and that it is obvious that the implementation change does not accidentally cause other incorrect changes.[4]

I've seen what happens if those concepts are ignored.

[1]Obvious, but still worth repeating
[2]In the long term, not the short term
[3]That precludes perverse use of tools, cf using a hammer to insert a nail.
[4]Clearing up somebody else's droppings was expensive and unpleasant.
« Last Edit: January 18, 2023, 09:37:19 pm by tggzzz »
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 
The following users thanked this post: DiTBho

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: DSL to describe a fsm?
« Reply #9 on: January 26, 2023, 09:34:17 pm »
I thought this https://github.com/AlexanderBrevig/PicoFSM/blob/master/PicoFSMSample/Program.cs was a good idea ten years ago, and I still often find myself creating analogous implementations.

That is, a type system that represents the states and the machine, the machine processes the current state and the transitions.

Each state has a set of lambdas (or a pointer to function in C) typically `on_enter` `on_update` and `on_exit` but you can consider also things like `on_panic`/`on_exception` and `on_schedule` if you need an easy way of polling/protothreading all the states.

The benefit is that you can separate the noise of all those if/else and switch cases out from the actual interesting parts of your code.

The 2d approach from this post https://aticleworld.com/state-machine-using-c/ is also typical to what I have seen for bigger C-like implementation.

Not sure about C these days but with C++ and Rust I can get the compiler to make a binary with no performance hit over a if/else/switch implementation, probably should consider if the presumed performance hit by using function pointers with C has any impact for your particular solution.


Good luck!
 
The following users thanked this post: DiTBho


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf