Question
The following is a C implementation of a lexical analyzer specified in the state diagram of Figure 4.1, including a main driver function for testing
The following is a C implementation of a lexical analyzer specified in the state diagram of Figure 4.1, including a main driver function for testing purposes: /* front.c a lexical analyzer system for simple arithmetic expressions */ #include #include /* (Global declarations */ * Variables */ int charclass; char lexeme [100]; char next Char; int lexLen; int token; int nextToken; FILE *in_fp, *fopen(); /* Function declarations */ void addChar(); void getChar(); void getNonBlank(); int lex(); /* Character classes */ #define LETTER O #define DIGIT 1 #define UNKNOWN 99 /* Token codes */ #define INT_LIT 10 #define IDENT 11 #define ASSIGN_OP 20 => no code #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 \"); else { you can use input string get Char(); 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 I*': addChar(); nextToken = break; MULT_OP; case '/' : addChar(); nextToken = break; DIV_OP; default: addChar(); nextToken = EOF; break; } return nextToken; } /* addChar - a function to add next Char to lexeme */ void addChar() { if (lexLen =>
nextToken = INT LIT; break; /* Parentheses and operators */ case UNKNOWN : lookup (next Char); get Char(); 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 */ Consider the following expression: (sum + 47) / total Following is the output of the lexical analyzer of front.c when used on this expression: Next token is: 25 Next lexeme is ( Next token is: 11 Next lexeme is sum Next token is: 21 Next lexeme is + Next token is: 10 Next lexeme is 47 Next token is: 26 Next lexeme is) Next token is: 24 Next lexeme is / Next token is: 11 Next lexeme is total Next token is: -1 Next lexeme is EOF Update the C code implementing lexical analyzer (given in Section 4.2) so that it will recognize the following lexemes: comma (,), semicolon (;) and a reserved word int.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started