Question: The Listing /*************** global.h ************************/ #include #include #define BSIZE 128 #define NONE -1 #define EOS 0 #define NUM 256 #define DIV 257 #define MOD 258

The Listing

/*************** global.h ************************/

#include

#include

#define BSIZE 128

#define NONE -1

#define EOS \0

#define NUM 256

#define DIV 257

#define MOD 258

#define ID 259

#define DONE 260

int tokenval; /* value of token attribute */

int lineno;

struct entry { /* form of symbol table entry */

char *lexptr;

int token;

};

struct entry symtable[ ];

/*************** lexer.c ************************/

#include global.h

char lexbuf[BSIZE];

int lineno = 1;

int tokenval = NONE;

int lexan( ) /* lexical analyzer */

{

int t;

int p, b = 0;

while(1) {

t = getchar();

if(t == ' ' || t == '\t')

; /* strip out white space */

else if (t == ' ')

lineno++;

else if(isdigit(t)) { /* t is a digit */

ungetc(t, stdin);

scanf("%d", &tokenval);

return NUM;

}else if (isalpht(t)) { /* t is a letter */

while(isalnum(t) {

lexbuf[b] = t;

t = getchar();

b++;

if(b >= BSIZE) error("compiler error");

}

lexbut[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;

}

}

}

}else if (isalpht(t)) { /* t is a letter */

while(isalnum(t) {

lexbuf[b] = t;

t = getchar();

b++;

if(b >= BSIZE) error("compiler error");

}

lexbut[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 ********************/

#include "global.h"

int lookahead;

void parse(){ /*pasers and translates expression list */

lookahead = lexan();

while(lookahead != DONE} {

expr(); match(';');

}

}

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, takenval); match(NUM);

break;

case ID:

emit(ID, tokenval); match(ID);

break;

default:

error("syntax error");

}

}

void match(int t) {

if(lookahead == t)

lookahead = lexan();

else

error("syntax error");

}

/************** emitter.c ********************/

#include "global.h"

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 ",symtalbe[tval].lexptr); break;

default:

printf("token %d, tokenval %d ",t,tval);

}

}

/************** symbol.c ********************/

#include "global.h"

#define STRMAX 999 /*size of lexeme array */

#define SYMMAX 100 /*size of symtable */

char lexemes[STRMAX];

int lastchar = -1; /* last used position in lexemes */

struct entry symtalbe[SYMMAX];

int lastentry = 0;

int lookup(char *s) /* return position of entry for 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;

len = strlen(s);

if(lastentry + 1 > SYMMAX)

error("symbol table full");

if(lastchar + len + 1 >= STRMAX)

error("lexemes array full");

lastentry++;

symtable[lastentry].token = tok;

symtable[lsatentry].lexptr = &lememes[lastchar+1];

lastchar = lastchar + len + 1;

strcpy(symtable[lastentry].lexptr,s);

return lastentry;

}

/************ init.c ***************************/

#include "global.h"

struct entry keywords[]={ "div", DIV,

"mod", MOD,

0, 0 };

void init()

{

struct entry *p

for(p = keywords; p->token; p++)

insert(p->lexptr, p->token);

}

/******* error.c **************/

#include "global.h"

void error(char *m)

{

fprintf(stderr, "line %d, %s ",lineno, m);

exit(1);

}

/************ main.c ***************************/

#include "global.h"

int main()

{

init();

parse();

return 0;

}

We have to combine all the above prorgams and Based on the code of small compiler fix all compiling errors and run it.

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 Databases Questions!