Skip to content

Compiler that generates a finite state machine in different languages

Notifications You must be signed in to change notification settings

adizhavo/FSM_compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FSM Compiler

FSMC is a scala application that generates a finite state machine from a file with a specific syntax and outputs it as a nested switch case statement in C#

Requirements

json4s was used to build the json of the syntax data structure

Usage

The repository has a compiler folder with fsmcompiler.jar and a sample car.fsm file showing the syntax of fsmc.

In the same directory run the command java -jar fsmcompiler.jar <language> <.fsm file path>

  • <language>is only C#
  • <.fsm file path> is one or multiple paths to the .fsm files
  • Additional command gen=<generated code directory> which by default is the current directory
  • Additional command json=<syntax structure directory> to output the data structure of the finite state machine

Syntax

Actions : Key
FSM : Car
Initial : Locked
{
	Locked {
		Open	Unlocked   unlock
		Force	Alarming   {lock notify}
	}

	Alarming >alarmOn <alarmOff {
		Reset	Locked	 -
	}

	Unlocked {
		Close	Locked	lock
		Reset	Locked	lock
	}
}

Required

  • Actions will generated an enum containing all the actions
  • FSM will be the file name and also part of the enum containing the fsm states
  • Initial is the initial state of the finite state machine

Transition

Locked >entryFunction <exitFunction {
	Open	Unlocked    unlock
	Force	Alarming    {lock notify}
}
  • Locked is the state of the fsm to execute all the subtransitions
  • >entryFunction is the function that will be executed when transitioning in the state, this is optional
  • <exitFunction is the function that will be executed when transitioning out of the state, this is optional

Subtransitions

  • { action nextState - } no function will be called
  • { action nextState function}
  • { action nextState {function1 function2} }

Internal Structure

FSMC Structure

  • Lexer converts the stream of data into a stream of tokens used by the Parser as events
  • Parser is a finite state machine that transfers a sequence of events to the builder
  • Syntax Builder creates the language data structure
  • Generator converts the syntax data structure into a syntax node which is easier to parse from different visitors
  • Language Visitors generate the finite state machine in different programming languages

How to support other languages

Implement the Language Visitor interface just like the CSharpVisistor.scala update the visitors map in the Config.scala file

Reference

The idea comes from the fsm compiler of Uncle Bob with a different implementation

About

Compiler that generates a finite state machine in different languages

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages