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 automaton/transducer 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 (i.e. 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 non-empty string of digits.
If there is a decimal point, the string before the decimal point should consist only of 0.
If the number starts with 0', it is either just 0, or 0.[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 NumberException(Exception):
pass
class ExpressionException(Exception):
pass
class TokenType(Enum):
Number = auto()
Plus = auto()
Minus = auto()
Times = auto()
Divide = auto()
class Token:
def __init__(self, valueOrType):
if isinstance(valueOrType, float):
self.value = valueOrType
self.type = TokenType.Number
else:
self.value =-1.0
self.type = valueOrType
def isNumber(self):
return self.type == TokenType.Number
def getType(self):
return self.type
def getValue(self):
if self.isNumber():
return self.value
else:
return None
def typeOf(symbol):
types ={
'+': TokenType.Plus,
'-': TokenType.Minus,
'*': TokenType.Times,
'/': TokenType.Divide
}
return types.get(symbol, None)
def __str__(self):
strings ={
TokenType.Number: str(self.value),
TokenType.Plus: "+",
TokenType.Minus: "-",
TokenType.Times: "*",
TokenType.Divide: "/"
}
return strings.get(self.type, None)
def __repr__(self):
return str(self)
def __eq__(self, other):
if not isinstance(other, 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 __ne__(self, other):
return not self == other
class LexicalAnalyser:
@classmethod
def analyse(cls, input):
# Complete this method.
# You will probably need to add more to this class.
return []
def main():
print(LexicalAnalyser.analyse("Put something here to test"))
if __name__=="__main__":
main()

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 Programming Questions!