Question: Review Lexical Analysis and Example Source Code given , and then implement a lexical analyzer that reads its input from a file and recognizes only

Review Lexical Analysis and Example Source Code given , and then
implement a lexical analyzer that reads its input from a file and recognizes only arithmetic
expressions, including variable names and 64-bit integer literals as operands. Write your
program in Python.
a. Simple library function is acceptable; however, you should not use any library function
that significantly reduces workload (e.g., java.util.regex)
Variable names
o consist of strings of uppercase letters, lowercase letters, and digits but must begin
with a letter.
o have no length limitation.
Arithmetic Expression may have the following operators: +,-,*,/.
Output:
Token, Lexeme pair like the example in the class.
/* front.c - a lexical analyzer system for simple
arithmetic expressions */
#include
#include
/* Global declarations */
/* Variables */
int charClass;
char lexeme [100];
char nextChar;
int lexLen;
int token;
int nextToken;
FILE *in_fp;
/* Function declarations */
void addChar();
void getChar();
void getNonBlank();
int lex();
/* Character classes */
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99
/* Token codes */
#define INT_LIT 10
#define IDENT 11
#define ASSIGN_OP 20
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PAREN 25
#define RIGHT_PAREN 26
/******************************************************/
/* main driver */
main(){
/* Open the input data file and process its contents */
errno_t err;
err = fopen_s(&in_fp,"D:/Academic_Research/Course Teaching/_Marshall/CS 300/lecture slides/cs300_lecture_07/front.in","r"); // If you face problem here, move the front.in file to a directory where the path does not have any space.
if (err !=0)
printf("ERROR - cannot open front.in
");
else {
getChar();
do {
lex();
} while (nextToken != EOF);
}
}
/*****************************************************/
/* lookup - a function to lookup operators and parentheses
and return the token */
int lookup(char ch){
switch (ch){
case '(':
addChar();
nextToken = LEFT_PAREN;
break;
case ')':
addChar();
nextToken = RIGHT_PAREN;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case '-':
addChar();
nextToken = SUB_OP;
break;
case '*':
addChar();
nextToken = MULT_OP;
break;
case '/':
addChar();
nextToken = DIV_OP;
break;
default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}
/*****************************************************/
/* addChar - a function to add nextChar to lexeme */
void addChar(){
if (lexLen =98){
lexeme[lexLen++]= nextChar;
lexeme[lexLen]=0;
}
else
printf("Error - lexeme is too long
");
}
/*****************************************************/
/* getChar - a function to get the next character of
input and determine its character class */
void getChar(){
if ((nextChar = getc(in_fp))!= EOF){
if (isalpha(nextChar))
charClass = LETTER;
else if (isdigit(nextChar))
charClass = DIGIT;
else charClass = UNKNOWN;
}
else
charClass = EOF;
}
/*****************************************************/
/* getNonBlank - a function to call getChar until it
returns a non-whitespace character */
void getNonBlank(){
while (isspace(nextChar))
getChar();
}
/*****************************************************/
/* lex - a simple lexical analyzer for arithmetic
expressions */
int lex(){
lexLen =0;
getNonBlank();
switch (charClass){
/* Parse identifiers */
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT){
addChar();
getChar();
}
nextToken = IDENT;
break;
/* Parse integer literals */
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT){
addChar();
getChar();
}
nextToken = INT_LIT;
break;
/* Parentheses and operators */
case UNKNOWN:
lookup(nextChar);
getChar();
break;
/* EOF */
case EOF:
nextToken = EOF;
lexeme[0]='E';
lexeme[1]='O';
lexeme[2]='F';
lexeme[3]=0;
break;
}/* End of switch */
printf("Next token is: %d, Next lexeme is %s
",
nextToken, lexeme);
return nextToken;
}/* End of function lex */
(sum +480000000000000000000000000000000000000000000000000000000000000000)/ total
 Review Lexical Analysis and Example Source Code given , and then

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!