Question: The following is a C compiler assembled into a program that includes emitter, symbol, parser, and lexical analyzer components. Compile it , then execute it
The following is a C compiler assembled into a program that includes emitter, symbol, parser, and lexical analyzer components. Compile it then execute it
Once you've saved this as a c file lets call it compiler.c compile it with:
gcc compiler.c o compiler
And then run it:
compiler
Display the results and explain them if it does not work, appropriate modifications can be made
global.h
#include
#include
#include
#include
#define BSIZE
#define NONE
#define NUM
#define ID
#define DIV
#define MOD
#define EOS
#define SYMMAX
#define STRMAX
extern int tokenval;
extern int lineno;
extern struct entry symtable;
lexer.c
char lexbufBSIZE;
int lineno ;
int lexan
int t;
while
t getchar;
if t t t
strip out white space
else if t
lineno;
else if isdigitt
ungetct stdin;
scanfd &tokenval;
return NUM;
else if isalphat
t is a letter
int p b ;
while isalnumt
lexbufb t;
t getchar;
b;
if b BSIZE
printferror: compiler error";
lexbufb EOS;
if t EOF
ungetct stdin;
p lookuplexbuf;
if p
p insertlexbuf ID;
tokenval p;
return symtableptoken;
else if t EOF
return DONE;
else
tokenval NONE;
return t;
parser.c
int lookahead;
void parse
lookahead lexan;
while lookahead DONE
expr;
if lookahead ;
match;;
lookahead lexan;
else
printfSyntax error: expected ; at line d
lineno;
exit;
void expr
int t;
term;
while
switch lookahead
case :
case :
t lookahead;
matchlookahead;
term;
emitt NONE;
continue;
default:
return;
void term
int t;
factor;
while
switch lookahead
case :
case :
case DIV:
case MOD:
t lookahead;
matchlookahead;
factor;
emitt NONE;
continue;
default:
return;
void factor
switch lookahead
case :
match;
expr;
match;
break;
case NUM:
emitNUM tokenval;
matchNUM;
break;
case ID:
emitID tokenval;
matchID;
break;
default:
printfSyntax error: unexpected token at line d
lineno;
exit;
void matchint t
if lookahead t
lookahead lexan;
else
printfSyntax error: unexpected token at line d
lineno;
exit;
emitter.c
void emitint t int tval
switch t
case :
case :
case :
case :
printfc
t;
break;
case DIV:
printfDIV
;
break;
case MOD:
printfMOD
;
break;
case NUM:
printfd
tval;
break;
case ID:
printfs
symtabletvallexptr;
break;
default:
printftoken d tokenval d
t tval;
symbol.c
struct entry
char lexptr;
int token;
;
char lexemesSTRMAX;
int lastchar ;
int lastentry ;
int lookupchar s
int p;
for p lastentry; p ; p
if strcmpsymtableplexptr, s
return p;
return ;
int insertchar s int tok
int len strlens;
if lastentry SYMMAX
printferror: symbol table full";
if lastchar len STRMAX
printferror: lexemes array full";
lastentry;
symtablelastentrytoken tok;
symtablelastentrylexptr &lexemeslastchar;
strcpysymtablelastentrylexptr, s;
lastchar len ;
return lastentry;
init.c
struct entry keywords
div DIV
mod MOD
NULL;
void init
struct entry p;
for p keywords; plexptr NULL; p
insertplexptr, ptoken;
int main
init;
parse;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
