DSL to describe a fsm? - Page 1
Forum section: electronics / general

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 09:58:28 am
content: 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 programmingI 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 screwdriverCode: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 screwdriverI'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 themSo, hey, you guessed right, DSL needs because I spent a lot of time to straighten the code)

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 10:51:42 am
content: Need to describe state-transition tables

author: tggzzz
author_rank: Super Contributor
timestamp: January 18, 2023, 10:56:16 am
content: There have been many over the decades. Too many. But that's typical of DSLanguages IMNSHOI 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.

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 11:48:55 am
content: Ehehehthisevil monster still torments me in my sleep with its sprawling details(Xasm, dunno if works, but for sure bad dreams are made of this)

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 12:04:39 pm
content: 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.Amazingedit:I access the forum via smartphone, I do not receive notifications about it.Sorry if I missed some answers.

author: Sherlock Holmes
author_rank: Frequent Contributor
timestamp: January 18, 2023, 04:38:46 pm
content: 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 theproblemas a DSL rather than the desired FSM itself...This doesn't really answer your question as such but might be food for thought...

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 05:37:21 pm
content: Years ago, I (ab-)used the ErLangGen_Fsmtrick (what?) 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

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 18, 2023, 08:55:27 pm
content: 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.

author: tggzzz
author_rank: Super Contributor
timestamp: January 18, 2023, 09:33:39 pm
content: Quote from: SiliconWizard on January 18, 2023, 08:55:27 pmMakeitthe specification and design clean.Make the implementation clean.Make the correspondence between bits of the design and bits of the implementation clean and obvious.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 theimplementationchange does not accidentally cause other incorrect changes.I've seen what happens if those concepts are ignored.Obvious, but still worth repeatingIn the long term, not the short termThat precludes perverse use of tools, cf using a hammer to insert a nail.Clearing up somebody else's droppings was expensive and unpleasant.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: January 26, 2023, 09:34:17 pm
content: I thought thishttps://github.com/AlexanderBrevig/PicoFSM/blob/master/PicoFSMSample/Program.cswas 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 posthttps://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!