Question: You will implement a [ deterministic ] finite automaton / transducer that takes in a string representing an arithmetic statement, and produces an output sequence
You will implement a deterministic finite automatontransducer that takes in a string representing an arithmetic statement, and produces an output sequence of tokens that represent the numbers and operators. Your program should work like a finite automaton, with an explicit representation of state, and reading one character at a time. You will have to decide when to emit tokens ie add them to the output list The one specific exception I will allow to the model is the keeping of some sort of buffer to collect the digits of the numbers in as the task is effectively impossibly otherwise
Your program should also handle two types of errors incorrect representation of a number, and incorrect representation of an expression.
Numbers will be in the following format:
A string of digits possibly followed by a decimal point and a nonempty string of digits.
If there is a decimal point, the string before the decimal point should consist only of
If the number starts with it is either just or something where the something is a string of digits.
The following expressions are valid:
A valid number is a valid expression.
Any valid expression, followed by an operator, followed by a valid number.
The operators and numbers may or may not be separated by white space.
In all cases, the skeleton code has a analyse method which takes a string of the appropriate type for each language and returns or should return a list of Tokens again typed appropriately for each language The Token type is also given as part of the skeleton, along with suitable error types as classes in Java and Python, and types in Haskell
The program will be implemented in Python with the following skeleton code
from enum import Enum, auto
class NumberExceptionException:
pass
class ExpressionExceptionException:
pass
class TokenTypeEnum:
Number auto
Plus auto
Minus auto
Times auto
Divide auto
class Token:
def initself valueOrType:
if isinstancevalueOrType float:
self.value valueOrType
self.type TokenType.Number
else:
self.value
self.type valueOrType
def isNumberself:
return self.type TokenType.Number
def getTypeself:
return self.type
def getValueself:
if self.isNumber:
return self.value
else:
return None
def typeOfsymbol:
types
: TokenType.Plus,
: TokenType.Minus,
: TokenType.Times,
: TokenType.Divide
return types.getsymbol None
def strself:
strings
TokenType.Number: strselfvalue
TokenType.Plus:
TokenType.Minus:
TokenType.Times:
TokenType.Divide:
return strings.getselftype, None
def reprself:
return strself
def eqself other:
if not isinstanceother Token:
return False
if self.isNumber:
if other.isNumber:
return self.value other.value
else:
return False
else:
if other.isNumber:
return False
else:
return self.type other.type
def neself other:
return not self other
class LexicalAnalyser:
@classmethod
def analysecls input:
# Complete this method.
# You will probably need to add more to this class.
return
def main:
printLexicalAnalyseranalysePut something here to test"
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
