Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Help fix the code! Help needed ASAP Can anyone get this to run with these directions? Use the following token names instead of numbers in

Help fix the code! Help needed ASAP

Can anyone get this to run with these directions? Use the following token names instead of numbers in your implementation: code is below the instructions.

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! inputfile name = lexInput.txt

Run your program on the following sequence of statements as described below. You may want to copy these statements and paste them into your input file. (47 + sum) / total Area = (length+width) * 2 C = age - 5 * (D / C)

a. Create your program, called LexicalAnalyzer, in a package called lexicalAnayzerPackage

b. Store all the statements in a single input file called lexInput.

c. Enter each statement on a separate line.

d. Store the lexInput file in the above package.

e. In the output, display each statement before you list its corresponding lexemes and tokens. X Assignment-Lexical Analyzer-DESCRIPTION Version: Refer to the version in the file name Last printed 9/26/2018 12:28 PM Page 3 of 3 Filename: Assignment-Lexical Analyzer-DESCRIPTION-Ver02.00.doc

f. Before and after you display each statement and corresponding lexemes and tokens, display a line of 80 asterisks (*)

g. Display your name, class information and a program title before you display the result of analyzing the input file.

code:

package lexicalAnayzerPackage;

import java.io.File;

import java.util.Scanner;

public class LexicalAnalyzer

{

/* Variables */

static int charClass;

static char lexeme;

static int lexLen;

int token;

static String nextToken;

static char nextChar;

static //private static final Object LETTER = null; // object or int specify as string look up c vs java enumerator 'enum' **/

int LETTER;

int DIGIT;

String UNKNOWN;

/* Token codes */

String INT_LIT;

String IDENT;

String ASSIGN_OP;

String ADD_OP;

String SUB_OP;

String MULT_OP;

String DIV_OP;

String LEFT_PAREN;

String RIGHT_PAREN;

/* Main Driver */

public static void main(String[] args) throws Exception {

/* Function declarations */

getChar();

addChar();

getNonBlank();

lex();

System.out.println(", Student, CSCI4200-DA, Fall 2018, Lexical Analyzer");

System.out.println("********************************************************************************");

/* Open the input data file and process its contents */

if ((in_fp = fopen("front.in", "r")) == null)

System.out.println("ERROR - cannot open front.in ");

else {

getChar();

do {

lex();

} while (nextToken != nextToken);

}

}

/*

Scanner file = new Scanner(new File("lexInput.txt"));

while (file.hasNext())

{

String str = file.nextLine();

System.out.println("Output:" + str);

}

System.out.println("********************************************************************************");

}// public static void

/*****************************************************/

//lex - a simple lexical analyzer for arithmetic expressions

static String lex() {

lexLen = 0;

getNonBlank();

int DIGIT;

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;

int UNKNOWN;

/* Parentheses and operators */

case UNKNOWN:

lookup(nextChar);

getChar();

break;

int EOF;

/* EOF */

case EOF:

nextToken = "EOF";

break;

} /* End of switch */

System.out.printf("Next token is: %d, Next lexeme is %s ", nextToken, lexeme);

return nextToken;

} /* End of function lex */

/*****************************************************/

/*

* getChar - a function to get the next character of input and determine its

* character class

*/

static void getChar() {

Object charClass;

if ((nextChar = getc()) != null) {

if (isalpha(nextChar))

charClass = "LETTER";

else if (isdigit(nextChar))

charClass = "DIGIT";

else

charClass = "UNKNOWN";

} else

charClass = "EOF";

}

/*****************************************************/

/* lookup - a function to lookup operators and parentheses

and return the token */

static String 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;

case '=':

addChar();

nextToken = "Assign_OP";

break;

default:

addChar();

nextToken = "EOF";

break;

}

return nextToken;

}

/*****************************************************/

/* addChar - a function to add nextChar to lexeme */

static void addChar() {

if (lexLen <= 98) {

char[] lexeme = null;

lexeme[lexLen++] = nextChar;

lexeme[lexLen] = 0;

} else

System.out.println("Error - lexeme is too long ");

}

/*****************************************************/

/*

* getNonBlank - a function to call getChar until it returns a non-whitespace

* character

*/

static void getNonBlank()

{

while (isspace(nextChar))

getChar();

}

*/

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students explore these related Databases questions

Question

What are on-the-run and off-the-run issues?

Answered: 3 weeks ago