NOTE: PLEASE I HAVE SEEN SOME ANSWERS ON CHEGG BUT I NEED A UNIQUE SOLUTION PLS. IF MY PROFESSOR SHOULD NOTICE ANY SIMILAR SOLUTION WITH OTHER STUDENT I WILL AUTOMATICALLY FAIL THE COURSE. I WILL PREFER IF THE SOLUTION CAN BE EMAILED TO ME INSTEAD OF POSTING IT ON CHEGG WEBSITE BCOS OTHER STUDENT IN MY CLASS CAN HAVE SAME SOLUTION WITH ME IF THE ALSO HAVE A CHEGG ACCOUNT. THANKS
Convert the C implementation of lexical analyzer A2.c to an equivalent Java program. Given a particular input, your Java program should produce the same output that A2.c produces. Use a diff tool to ensure your program produces the correct output. Even minor differences in output will cause you to fail grading tests and lose points.
General Programming Assignment Requirements:
- Classes must be in the default (no package statement) unless otherwise specified. You will lose 20 points (all points) if you put a package statement in your program.
- If your program that does not compile or does not run, you will lose all points.
- If you submit the wrong file your grade will suffer accordingly. Most likely a 0.
- You must add the header and fill it in for every file you submit or you may lose points.
/* lexicalanalyzer.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, *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 */ int main(int argc, char *argv[]) { /* Open the input data file and process its contents */ if ((in_fp = fopen(argv[1], "r")) == NULL) printf("ERROR - cannot open front.in "); else { getChar(); do { lex(); } while (nextToken != EOF); } return 0; } /* lookup - a function to lookup operators and parentheses and return the token */ int lookup(char ch) { // printf(" lookup(): %c ", 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; // printf(" addChar() lexeme[]: %s ", lexeme); } 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) { // printf(" getChar(): %c ", nextChar); 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)) { // printf(" REMOVED SPACE "); getChar(); } } /* lex - a simple lexical analyzer for arithmetic expressions */ int lex() { // printf("lex()---------------------------------------------------------------------- "); lexLen = 0; switch(charClass) { case LETTER: /* printf("charClass before getNonBlank(): LETTER "); */ break; case DIGIT: /* printf("charClass before getNonBlank(): DIGIT "); */ break; case UNKNOWN: /* printf("charClass before getNonBlank(): UNKNOWN "); */ break; } getNonBlank(); switch(charClass) { case LETTER: /* printf("charClass after getNonBlank(): LETTER "); */ break; case DIGIT: /* printf("charClass after getNonBlank(): DIGIT "); */ break; case UNKNOWN: /* printf("charClass after getNonBlank(): UNKNOWN "); */ break; } 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 */