Question: Can someone convert this Scala code to Python? import scala.io.Source import scala.collection.mutable.Map import scala.collection.mutable.ArrayBuffer /* Trait IncrementalStateMachineBuilder defines the basic operations and attributes for building

Can someone convert this Scala code to Python?

import scala.io.Source import scala.collection.mutable.Map import scala.collection.mutable.ArrayBuffer

/* Trait IncrementalStateMachineBuilder defines the basic operations and attributes for building StateMachine objects incrementally during parsing. It includes the symbol table and the basic context variables. */

trait IncrementalStateMachineBuilder {

// Context variables private var stateMachine: StateMachine = null private var curState: State = null

// Symbol table private val events = Map[String,Event]() private val commands = Map[String,Command]() private val resetEvents = new ArrayBuffer[Event] private val states = Map[String,State]()

// Options private var traceEnabled = true

// Enable/Disable trace output capability def enableTrace { traceEnabled = true } def disableTrace { traceEnabled = false }

// Create State Machine with argument start state unless // already done def primeMachine(state: State) { if (stateMachine == null) stateMachine = new StateMachine(state) state.setMachine(stateMachine) // register machine with state }

// Define a new Event def addEvent(name: String, code: String) { getEvent(name) match { case Some(e) => syntaxError("Attempt to redefine event '" + name + "'.") case None => events(name) = Event(name,Symbol(code)) } }

// Define a new ResetEvent def addResetEvent(name: String) { getEvent(name) match { case Some(e) => resetEvents += e case None => syntaxError("Undefined resetEvent '" + name + "'.") } }

// Define a new Command def addCommand(name: String, code: String) { getCommand(name) match { case Some(c) => syntaxError("Attempt to redefine command '" + name + "'.") case None => commands(name) = Command(name,Symbol(code)) } }

/* Define a new State. If this is the first state, create a new state machine with this as the start state. Also make this the "current state". The first two steps can be done by directly calling obtainState and primeMachine, both of which are also in the public interface. */ def addState(name: String) { curState = obtainState(name) // might be adding to existing state primeMachine(curState) }

// Add a Command to the current state's actions def addAction(name: String) { getCommand(name) match { case Some(ac) => curState.addAction(ac) case None => syntaxError("Attempt to add undefined action '" + name + "' to state '" + curState.name + "'.") } }

// Add a transition to the current state def addTransition(trigger: String, target: String) { getEvent(trigger) match { case Some(ev) => curState.addTransition(ev,obtainState(target)) // target might be forward reference to valid state case None => syntaxError("Undefined event '" + trigger + "' in transition.") } }

// Create or look up existing state def obtainState(name: String): State = states.getOrElseUpdate(name, new State(name))

// Accessors for the attributes def getMachine: StateMachine = stateMachine

def getEvent(name: String): Option[Event] = events.get(name)

def getCommand(name: String): Option[Command] = commands.get(name)

def getState(name: String): Option[State] = states.get(name)

def getCurState: State = curState

// Complete any remaining actions to build state machine def finishMachine { stateMachine.addResetEvents(resetEvents.toArray: _*) }

// Error condition when illegal syntax encountered def syntaxError(msg: String) { throw new RecognitionException("[Error] " + msg) }

// Trace output def trace(msg: String) { if (traceEnabled) println(msg) }

}

/* Exception RecognitionException denotes an error in the input DSL. */

class RecognitionException(ex: String) extends Exception(ex)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!