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 (let's 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 128
#define NONE -1
#define NUM 256
#define ID 257
#define DIV 258
#define MOD 259
#define EOS '\0'
#define SYMMAX 100
#define STRMAX 999
extern int tokenval;
extern int lineno;
extern struct entry symtable[];
// lexer.c
char lexbuf[BSIZE];
int lineno =1;
int lexan(){
int t;
while (1){
t = getchar();
if (t ==''|| t =='\t'){
// strip out white space
} else if (t =='
'){
lineno++;
} else if (isdigit(t)){
ungetc(t, stdin);
scanf("%d", &tokenval);
return NUM;
} else if (isalpha(t)){
// t is a letter
int p, b =0;
while (isalnum(t)){
lexbuf[b]= t;
t = getchar();
b++;
if (b >= BSIZE)
printf("error: compiler error");
}
lexbuf[b]= EOS;
if (t != EOF)
ungetc(t, stdin);
p = lookup(lexbuf);
if (p ==0)
p = insert(lexbuf, ID);
tokenval = p;
return symtable[p].token;
} 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 {
printf("Syntax error: expected ';' at line %d
", lineno);
exit(1);
}
}
}
void expr(){
int t;
term();
while (1){
switch (lookahead){
case '+':
case '-':
t = lookahead;
match(lookahead);
term();
emit(t, NONE);
continue;
default:
return;
}
}
}
void term(){
int t;
factor();
while (1){
switch (lookahead){
case '*':
case '/':
case DIV:
case MOD:
t = lookahead;
match(lookahead);
factor();
emit(t, NONE);
continue;
default:
return;
}
}
}
void factor(){
switch (lookahead){
case '(':
match('(');
expr();
match(')');
break;
case NUM:
emit(NUM, tokenval);
match(NUM);
break;
case ID:
emit(ID, tokenval);
match(ID);
break;
default:
printf("Syntax error: unexpected token at line %d
", lineno);
exit(1);
}
}
void match(int t){
if (lookahead == t)
lookahead = lexan();
else {
printf("Syntax error: unexpected token at line %d
", lineno);
exit(1);
}
}
// emitter.c
void emit(int t, int tval){
switch (t){
case '+':
case '-':
case '*':
case '/':
printf("%c
", t);
break;
case DIV:
printf("DIV
");
break;
case MOD:
printf("MOD
");
break;
case NUM:
printf("%d
", tval);
break;
case ID:
printf("%s
", symtable[tval].lexptr);
break;
default:
printf("token %d, tokenval %d
", t, tval);
}
}
// symbol.c
struct entry {
char *lexptr;
int token;
};
char lexemes[STRMAX];
int lastchar =1;
int lastentry =0;
int lookup(char s[]){
int p;
for (p = lastentry; p >0; p--)
if (strcmp(symtable[p].lexptr, s)==0)
return p;
return 0;
}
int insert(char s[], int tok){
int len = strlen(s);
if (lastentry >= SYMMAX)
printf("error: symbol table full");
if (lastchar + len +1>= STRMAX)
printf("error: lexemes array full");
lastentry++;
symtable[lastentry].token = tok;
symtable[lastentry].lexptr = &lexemes[lastchar];
strcpy(symtable[lastentry].lexptr, s);
lastchar += len +1;
return lastentry;
}
// init.c
struct entry keywords[]={
{"div", DIV},
{"mod", MOD},
{NULL,0}}}};
void init(){
struct entry *p;
for (p = keywords; p->lexptr != NULL; p++)
insert(p->lexptr, p->token);
}
int main(){
init();
parse();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!