Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use this lex ( ) program token.h #ifndef TOKEN _ H #define TOKEN _ H / / Token types enum { IDENT = 1 1

use this lex() program token.h
#ifndef TOKEN_H
#define TOKEN_H
// Token types
enum {
IDENT =11,
INT_LIT =10,
FLOAT_LIT =12,
ADD_OP =21,
SUB_OP =22,
MULT_OP =23,
DIV_OP =24,
LEFT_PAREN =25,
RIGHT_PAREN =26,
ASSIGN_OP =27,
LT_OP =28,
GT_OP =29,
EQ_OP =30,
NEQ_OP =31,
FOR_CODE =32,
IF_CODE =33,
ELSE_CODE =34,
WHILE_CODE =35,
DO_CODE =36,
INT_CODE =37,
FLOAT_CODE =38,
SEMICOLON =39,
LEFT_BRACE =40,
RIGHT_BRACE =41,
MOD_OP =42,
NOT_EQ =43,
EOF_TOKEN =-1// Ensure EOF_TOKEN is defined
};
// Global variables
extern char lexeme[];
extern int nextToken;
extern int lexLen;
extern int token;
extern int charClass;
extern FILE *in_fp;
// Function prototypes
void addChar();
void getChar();
void getNonBlank();
int lookup(char ch);
int lex();
int isKeyword(char *lexeme);
#endif // TOKEN_H
lexer.c
#include
#include
#include
#include "token.h"
// Global variables
char lexeme[100];
int lexLen;
int token;
int nextToken;
int charClass;
FILE *in_fp;
// Character classes
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99
void addChar(){
if (lexLen <=98){
lexeme[lexLen++]=(char)token;
lexeme[lexLen]=0;
} else
printf("Error: lexeme is too long
");
}
void getChar(){
if ((token = getc(in_fp))!= EOF){
if (isalpha(token))
charClass = LETTER;
else if (isdigit(token))
charClass = DIGIT;
else
charClass = UNKNOWN;
} else
charClass = EOF_TOKEN;
}
void getNonBlank(){
while (isspace(token))
getChar();
}
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(); nextToken = ASSIGN_OP; break;
case '<': addChar(); nextToken = LT_OP; break;
case '>': addChar(); nextToken = GT_OP; break;
case ';': addChar(); nextToken = SEMICOLON; break;
case '{': addChar(); nextToken = LEFT_BRACE; break;
case '}': addChar(); nextToken = RIGHT_BRACE; break;
case '%': addChar(); nextToken = MOD_OP; break;
default: addChar(); nextToken = EOF_TOKEN; break;
}
return nextToken;
}
int isKeyword(char *lexeme){
if (strcmp(lexeme, "for")==0) return FOR_CODE;
if (strcmp(lexeme,"if")==0) return IF_CODE;
if (strcmp(lexeme, "else")==0) return ELSE_CODE;
if (strcmp(lexeme, "while")==0) return WHILE_CODE;
if (strcmp(lexeme,"do")==0) return DO_CODE;
if (strcmp(lexeme, "int")==0) return INT_CODE;
if (strcmp(lexeme, "float")==0) return FLOAT_CODE;
return IDENT;
}
int lex(){
lexLen =0;
getNonBlank();
switch (charClass){
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT){
addChar();
getChar();
}
nextToken = isKeyword(lexeme);
break;
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT){
addChar();
getChar();
}
if (token =='.'){
addChar();
getChar();
while (charClass == DIGIT){
addChar();
getChar();
}
nextToken = FLOAT_LIT;
if (token =='e'|| token =='E'){
addChar();
getChar();
if (token =='+'|| token =='-'){
addChar();
getChar();
}
while (charClass == DIGIT){
addChar();
getChar();
}
nextToken = FLOAT_LIT;
}
} else {
nextToken = INT_LIT;
}
break;
case UNKNOWN:
lookup(token);
getChar();
break;
case EOF_TOKEN:
nextToken = EOF_TOKEN;
lexeme[0]='E';
lexeme[1]='O';
lexeme[2]='F';
lexeme[3]=0;
break;
}
printf("Next token is: %d, Next lexeme is %s
", nextToken, lexeme);
return nextToken;
}
main.c
#include
#include "token.h"
int main(){
if ((in_fp = fopen("front.in","r"))== NULL){
printf("ERROR - cannot open front.in
");
return 1;
} else {
getChar();
do {
lex();
} while (nextToken != EOF_TOKEN);
}
return 0;
}
front.in
for (int i =0; i <10; i++){
if (i %2==0){
float x =3.14;
} else {
while (x <10){
x +=1.0e+2;
}
}
}
do {
int y =5;
} while (y <10);to create a recursive descent parser as follows:
Modify/Extend the recursive descent parser given in Section 4.4.1(which recognises ,, and ), such that it ALSO recognises the following language constructs as well (follow C-like or Java-like syntax):
The (for) statement.
The simple (if) and the (if-else) statements.
The (while) statement
The (do-while) statement

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

Step: 3

blur-text-image

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

e. What difficulties did they encounter?

Answered: 1 week ago