Question
using dev++ ca what is the mistake and in my code and the correct answer with steps global.h #ifndef GLOBAL_H #define GLOBAL_H #include #include #include
using dev++ ca what is the mistake and in my code and the correct answer with steps
global.h
#ifndef GLOBAL_H #define GLOBAL_H
#include #include #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
void expr(); void parse(); void term(); void match (int t); void error( char* m); void factor();
int tokenval;
int lineno;
struct entry { char *lexptr; int token; }; #endif
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); }
symbol.c
#include "global.h"
#define STRMAX 999 #define SYMMAX 100
char lexemes[STRMAX]; int lastchar = -1; struct entry symtable[SYMMAX]; int lastentry = 0;
int lookup(char s[])
{ int p; for (p = lastentry; p > 0; p = p-1) 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 = lastentry + 1;
symtable[lastentry].token = tok;
symtable[lastentry].lexptr = &lexemes[lastchar + 1];
lastchar = lastchar + len + 1;
strcpy(symtable[lastentry].lexptr, s);
return lastentry; }
lexer.c
#include "global.h"
char lexbuf[BSIZE]; int lineno = 1; int tokenval = NONE;
extern struct entry symtable[];
int lexan() {
int t;
while(1) { t = getchar(); if (t == ' ' || t == '\t'); else if (t == ' ') lineno = lineno + 1; else if (isdigit (t)) { ungetc(t, stdin); scanf("%d", &tokenval); return NUM; }
else if (isalpha(t)) { int p, b = 0; while (isalnum(t)) { lexbuf[b] = t; t = getchar(); b = b + 1; if (b >= BSIZE) 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; } } }
errror.c
#include "global.h"
void error(char* m) { fprintf(stderr, "line %d: %s ", lineno, m); exit(1); }
parse.c
#include "global.h"
void match (int t); int lookahead; void expr(); void parse(); void term();
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 parse() { 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 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: error("syntax error"); } }
void match(int t) { if (lookahead == t) lookahead = lexan(); else error ("syntax error"); }
main,c
#include "global.h"
void main() { init(); parse(); exit(0); }
emitter.c
#include "global.h"
extern struct entry symtable[];
void emit(int t,int tval) { switch(t) { case '+': printf("pop r1 pop r2 add r1, r2 npush r2 "); break; case '-': printf("pop r1 pop r2 sub r1, r2 npush r2 "); break; case '*': printf("pop r1 pop r2 mult r1, r2 npush r2 "); break; case '/': printf("pop r1 pop r2 div r1, r2 npush r2 "); break; printf("%c ", t); break; case DIV: printf("pop r1 pop r2 div r1, r2 npush r2 "); break; case MOD: printf("pop r1 pop r2 mod r1, r2 npush r2 "); break; case NUM: printf(" push %d ", tval); break; case ID: printf(" push %s ", symtable[tval].lexptr); break; default: printf("token %d, tokenval %d ", t, tval); } }
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