Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the lexical analyzer program given to Java. ----------------------------

ID: 3756984 • Letter: C

Question

Convert the lexical analyzer program given to Java.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Lexical Analyzer for simple arithmetic expressions

/* front.c - a lexical analyzer system for simple
arithmetic expressions */

#include <stdio.h>
#include <ctype.h>


/* Global declarations */
/* Variables */
int charClass;
char lexeme [100];
char nextChar;
int lexLen;
int token;
int nextToken;
FILE *in_fp, *fopen();
/* 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 */
if ((in_fp = fopen("front.in", "r")) == NULL)
printf("ERROR - cannot open front.in n");
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 n");
}

/*****************************************************/
/* 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 n",
nextToken, lexeme);
return nextToken;
} /* End of function lex */

----------------------------------------------------------------------------------------------------------------------------------------------------


Use the following token names instead of numbers in your implementation:

INT_LIT

IDENT

ASSIGN_OP

ADD_OP

SUB_OP

MULT_OP

DIV_OP

LEFT_PAREN

RIGHT_PAREN

END_OF_FILE

Example Input and Output

Given the following as the only expression in an input file:

(sum + 47) / total

Your lexical analyzer must produce the following output

. Please note the format of the output.

John D. Student, CSCI4200-DA, Fall 2018, Lexical Analyzer

********************************************************************************

Input: (sum + 47) / total

Next token is: LEFT_PAREN Next lexeme is (
Next token is: IDENT Next lexeme is sum
Next token is: ADD_OP Next lexeme is +
Next token is: INT_LIT Next lexeme is 47
Next token is: RIGHT_PAREN Next lexeme is )
Next token is: DIV_OP Next lexeme is /
Next token is: IDENT Next lexeme is total

********************************************************************************

Next token is: END_OF_FILE Next lexeme is EOF

Lexical analysis of the program is complete!

Explanation / Answer

package lexicalanalyser; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.logging.Level; import java.util.logging.Logger; enum CharacterClass{ LETTER(0), DIGIT(1), UNKNOWN(99), EOF(-1); private int value; private CharacterClass(int value){ this.value = value; } public int getValue(){ return value; } } enum TokenCodes{ INT_LIT(10), IDENT(11), ASSIGN_OP(20), ADD_OP(21), SUB_OP(22), MULT_OP(23), DIV_OP(24), LEFT_PAREN(25), RIGHT_PAREN(26), SEMI_COLUMN(27), COMP_OP_SMALL(28), COMP_OP_LARGE(29), COMP_OP_EQUAL(30), EOF(-1), EOL(-2); private int value; private TokenCodes(int value){ this.value = value; } public int getValue(){ return value; } } public class LexicalAnalyser { /* Global Declarations */ /* Variables */ static CharacterClass charClass; static char[] lexeme = new char[100]; static char nextChar; static int count = 0; static int lexLen; static int token; static TokenCodes nextToken; static File in_fp; static FileReader fr; public static void main(String[] args) throws FileNotFoundException, IOException { /* Open the input data file and process its contents */ try{ in_fp = new File("front.in"); fr = new FileReader(in_fp); getChar(); do{ lex(); }while (nextToken != TokenCodes.EOF); System.out.println("Parsing Complete!"); }catch (FileNotFoundException e){ System.out.println("Error - File Not Found"); }catch (IOException e){ System.out.println("Error - IO Exception"); } } /* lookup - a function to lookup operators and parentheses and return the token*/ static void lookup(char ch){ switch(ch){ case '(': addChar(); nextToken = TokenCodes.LEFT_PAREN; break; case ')': addChar(); nextToken = TokenCodes.RIGHT_PAREN; break; case '+': addChar(); nextToken = TokenCodes.ADD_OP; break; case '-': addChar(); nextToken = TokenCodes.SUB_OP; break; case '*': addChar(); nextToken = TokenCodes.MULT_OP; break; case '/': addChar(); nextToken = TokenCodes.DIV_OP; break; case '=': addChar(); nextToken = TokenCodes.ASSIGN_OP; break; case ';': addChar(); nextToken = TokenCodes.SEMI_COLUMN; break; case '>': addChar(); nextToken = TokenCodes.COMP_OP_LARGE; break; case '