Home / Expert Answers / Computer Science / i-have-a-c-lexer-program-that-outputs-the-specific-type-of-token-as-a-string-using-the-expression-t-pa224

(Solved): I have a c lexer program that outputs the specific type of token as a string using the expression T ...



I have a c lexer program that outputs the specific type of token as a string using the expression

The existing code outputs identifiers and operators and parenthesis. What modifications need to be made to the existing code to output keywords as well?

Example:

Next token is: KEYWORD, Next lexeme is int

Next token is: IDENT, Next lexeme is sum

Next token is: EQUAL_OP, Next lexeme is =

Next token is: INTEGER, Next lexeme is 3

Etc...

Code:

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

/* Global declarations */
/* Variables */
int charClass;
char lexeme[100];
char nextChar;
int lexLen;
int nextToken;
FILE* in_fp;

/* Function declarations */
void addChar();
void getChar();
void getNonBlank();
int lex();
int lookup(char ch);

/* 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
#define LESSER_OP 27
#define GREATER_OP 28
#define EQUAL_OP 29
#define NOT_EQUAL_OP 30
#define LESSER_EQUAL_OP 31
#define GREATER_EQUAL_OP 32
#define AND_OP 33
#define OR_OP 34
#define NOT_OP 35
#define IF_KEYWORD 36
#define ELSE_KEYWORD 37
#define WHILE_KEYWORD 38
#define DO_KEYWORD 39
#define FOR_KEYWORD 40
#define SWITCH_KEYWORD 41
#define CASE_KEYWORD 42
#define DEFAULT_KEYWORD 43
#define BREAK_KEYWORD 44
#define CONTINUE_KEYWORD 45
#define RETURN_KEYWORD 46
#define VOID_KEYWORD 47
#define INT_KEYWORD 48
#define FLOAT_KEYWORD 49
#define CHAR_KEYWORD 50
#define BOOL_KEYWORD 51
/* main driver */
int 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);
}
return 0;
}

/* 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;
case '<':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = LESSER_EQUAL_OP;
}
else if (nextChar == '>') {
addChar();
nextToken = NOT_EQUAL_OP;
}
else {
nextToken = LESSER_OP;
}
break;
case '>':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = GREATER_EQUAL_OP;
}
else {
nextToken = GREATER_OP;
}
break;
case '=':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = EQUAL_OP;
}
else {
nextToken = ASSIGN_OP;
}
break;
case '&':
addChar();
getChar();
if (nextChar == '&') {
addChar();
nextToken = AND_OP;
getChar();
}
else {
nextToken = UNKNOWN;
}
break;
case '|':
addChar();
getChar();
if (nextChar == '|') {
addChar();
nextToken = OR_OP;
getChar();
}
else {
nextToken = UNKNOWN;
}
break;
case '!':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = NOT_EQUAL_OP;
getChar();
}
else {
nextToken = NOT_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();
}
}

int lex() {

lexLen = 0;

getNonBlank();

switch (charClass) {

/* Parse identifiers */

case LETTER:

addChar();

getChar();

while (charClass == LETTER || charClass == DIGIT) {

addChar();

getChar();

}

nextToken = IDENT;

printf("Next token is: IDENT, Next lexeme is %s\n", lexeme);

break;

/* Parse integer literals */

case DIGIT:

addChar();

getChar();

while (charClass == DIGIT) {

addChar();

getChar();

}

nextToken = INT_LIT;

printf("Next token is: INT_LIT, Next lexeme is %s\n", lexeme);

break;

/* Parentheses and operators */

case UNKNOWN:

lookup(nextChar);

getChar();

printf("Next token is: ");

switch (nextToken) {

case LEFT_PAREN:

printf("LEFT_PAREN");

break;

case RIGHT_PAREN:

printf("RIGHT_PAREN");

break;

case ADD_OP:

printf("ADD_OP");

break;

case SUB_OP:

printf("SUB_OP");

break;

case MULT_OP:

printf("MULT_OP");

break;

case DIV_OP:

printf("DIV_OP");

break;

case LESSER_OP:

printf("LESSER_OP");

break;

case GREATER_OP:

printf("GREATER_OP");

break;

case EQUAL_OP:

printf("EQUAL_OP");

break;

case NOT_EQUAL_OP:

printf("NOT_EQUAL_OP");

break;

case LESSER_EQUAL_OP:

printf("LESSER_EQUAL_OP");

break;

case GREATER_EQUAL_OP:

printf("GREATER_EQUAL_OP");

break;

case AND_OP:

printf("AND_OP");

break;

case OR_OP:

printf("OR_OP");

break;

case NOT_OP:

printf("NOT_OP");

break;

default:

printf("UNKNOWN");

break;

}

printf(", Next lexeme is %s\n", lexeme);

break;

/* End of input */

case EOF:

nextToken = EOF;

lexeme[0] = 'E';

lexeme[1] = 'O';

lexeme[2] = 'F';

lexeme[3] = 0;

printf("Next token is: EOF\n");

break;

}

return nextToken;

}



We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe