Question: You will need to show the Python source code and the corresponding output. Let us implement a lexical analyzer ( also known as a scanner
You will need to show the Python source code and the corresponding output. Let us implement a lexical analyzer also known as a scanner which is the first phase of a compiler in Python. You must scan for a keywords in Python b identifiers
coperators and d separators. Please translate the C code below to Python.
#include
#include
#include
#include
Returns 'true' if the character is a DELIMITER.
bool isDelimiterchar ch
if ch ch ch ch
ch ch ch ; ch
ch ch ch ch
ch ch ch ch
return true;
return false;
Returns 'true' if the character is an OPERATOR.
bool isOperatorchar ch
if ch ch ch
ch ch ch
ch
return true;
return false;
Returns 'true' if the string is a VALID IDENTIFIER.
bool validIdentifierchar str
if str str str
str str str
str str str
str isDelimiterstr true
return false;
return true;
Returns 'true' if the string is a KEYWORD.
bool isKeywordchar str
if strcmpstrifstrcmpstr "else"
strcmpstr "while"strcmpstrdo
strcmpstr "break"
strcmpstr "continue"strcmpstr "int"
strcmpstr "double"strcmpstr "float"
strcmpstr "return"strcmpstr "char"
strcmpstr "case"strcmpstr "char"
strcmpstr "sizeof"strcmpstr "long"
strcmpstr "short"strcmpstr "typedef"
strcmpstr "switch"strcmpstr "unsigned"
strcmpstr "void"strcmpstr "static"
strcmpstr "struct"strcmpstr "goto"
return true;
return false;
Returns 'true' if the string is an INTEGER.
bool isIntegerchar str
int i len strlenstr;
if len
return false;
for i ; i len; i
if stri && stri && stri
&& stri && stri && stri
&& stri && stri && stri
&& stristri && i
return false;
return true;
Returns 'true' if the string is a REAL NUMBER.
bool isRealNumberchar str
int i len strlenstr;
bool hasDecimal false;
if len
return false;
for i ; i len; i
if stri && stri && stri
&& stri && stri && stri
&& stri && stri && stri
&& stri && stri
stri && i
return false;
if stri
hasDecimal true;
return hasDecimal;
Extracts the SUBSTRING.
char subStringchar str int left, int right
int i;
char subStr charmalloc
sizeofcharright left ;
for i left; i right; i
subStri left stri;
subStrright left ;
return subStr;
Parsing the input STRING.
void parsechar str
int left right ;
int len strlenstr;
while right len && left right
if isDelimiterstrright false
right;
if isDelimiterstrright true && left right
if isOperatorstrright true
printfc IS AN OPERATOR
strright;
right;
left right;
else if isDelimiterstrright true && left right
right len && left right
char subStr subStringstr left, right ;
if isKeywordsubStr true
printfs IS A KEYWORD
subStr;
else if isIntegersubStr true
printfs IS AN INTEGER
subStr;
else if isRealNumbersubStr true
printfs IS A REAL NUMBER
subStr;
else if validIdentifiersubStr
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
