Answered step by step
Verified Expert Solution
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 TOKENH
#define TOKENH
Token types
enum
IDENT
INTLIT
FLOATLIT
ADDOP
SUBOP
MULTOP
DIVOP
LEFTPAREN
RIGHTPAREN
ASSIGNOP
LTOP
GTOP
EQOP
NEQOP
FORCODE
IFCODE
ELSECODE
WHILECODE
DOCODE
INTCODE
FLOATCODE
SEMICOLON
LEFTBRACE
RIGHTBRACE
MODOP
NOTEQ
EOFTOKEN Ensure EOFTOKEN is defined
;
Global variables
extern char lexeme;
extern int nextToken;
extern int lexLen;
extern int token;
extern int charClass;
extern FILE infp;
Function prototypes
void addChar;
void getChar;
void getNonBlank;
int lookupchar ch;
int lex;
int isKeywordchar lexeme;
#endif TOKENH
lexer.c
#include
#include
#include
#include "token.h
Global variables
char lexeme;
int lexLen;
int token;
int nextToken;
int charClass;
FILE infp;
Character classes
#define LETTER
#define DIGIT
#define UNKNOWN
void addChar
if lexLen
lexemelexLenchartoken;
lexemelexLen;
else
printfError: lexeme is too long
;
void getChar
if token getcinfp EOF
if isalphatoken
charClass LETTER;
else if isdigittoken
charClass DIGIT;
else
charClass UNKNOWN;
else
charClass EOFTOKEN;
void getNonBlank
while isspacetoken
getChar;
int lookupchar ch
switch ch
case : addChar; nextToken LEFTPAREN; break;
case : addChar; nextToken RIGHTPAREN; break;
case : addChar; nextToken ADDOP; break;
case : addChar; nextToken SUBOP; break;
case : addChar; nextToken MULTOP; break;
case : addChar; nextToken DIVOP; break;
case : addChar; nextToken ASSIGNOP; break;
case : addChar; nextToken LTOP; break;
case : addChar; nextToken GTOP; break;
case ;: addChar; nextToken SEMICOLON; break;
case : addChar; nextToken LEFTBRACE; break;
case : addChar; nextToken RIGHTBRACE; break;
case : addChar; nextToken MODOP; break;
default: addChar; nextToken EOFTOKEN; break;
return nextToken;
int isKeywordchar lexeme
if strcmplexeme "for" return FORCODE;
if strcmplexemeif return IFCODE;
if strcmplexeme "else" return ELSECODE;
if strcmplexeme "while" return WHILECODE;
if strcmplexemedo return DOCODE;
if strcmplexeme "int" return INTCODE;
if strcmplexeme "float" return FLOATCODE;
return IDENT;
int lex
lexLen ;
getNonBlank;
switch charClass
case LETTER:
addChar;
getChar;
while charClass LETTER charClass DIGIT
addChar;
getChar;
nextToken isKeywordlexeme;
break;
case DIGIT:
addChar;
getChar;
while charClass DIGIT
addChar;
getChar;
if token
addChar;
getChar;
while charClass DIGIT
addChar;
getChar;
nextToken FLOATLIT;
if token e token E
addChar;
getChar;
if token token
addChar;
getChar;
while charClass DIGIT
addChar;
getChar;
nextToken FLOATLIT;
else
nextToken INTLIT;
break;
case UNKNOWN:
lookuptoken;
getChar;
break;
case EOFTOKEN:
nextToken EOFTOKEN;
lexemeE;
lexemeO;
lexemeF;
lexeme;
break;
printfNext token is: d Next lexeme is s
nextToken, lexeme;
return nextToken;
main.c
#include
#include "token.h
int main
if infp fopenfrontinr NULL
printfERROR cannot open front.in
;
return ;
else
getChar;
do
lex;
while nextToken EOFTOKEN;
return ;
front.in
for int i ; i ; i
if i
float x ;
else
while x
x e;
do
int y ;
while y ;to create a recursive descent parser as follows:
ModifyExtend the recursive descent parser given in Section which recognises and such that it ALSO recognises the following language constructs as well follow Clike or Javalike syntax:
The for statement.
The simple if and the ifelse statements.
The while statement
The dowhile statement
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