Answered step by step
Verified Expert Solution
Question
1 Approved Answer
homework from Programming Language Processor class.. I really need help with the codes. Very much thanks! /*************** compile.c *************/ #include getSource.h #ifndef TBL #define TBL
homework from Programming Language Processor class.. I really need help with the codes. Very much thanks!
/*************** compile.c *************/
#include "getSource.h"
#ifndef TBL
#define TBL
#include "table.h"
#endif
#include "codegen.h"
#define MINERROR 3 /**/
#define FIRSTADDR 2 /**/
static Token token; /**/
static void block(int pIndex); /**/
/*pIndex */
static void constDecl(); /**/
static void varDecl(); /**/
static void funcDecl(); /**/
static void statement(); /**/
static void expression(); /**/
static void term(); /**/
static void factor(); /**/
static void condition(); /**/
static int isStBeginKey(Token t); /*t*/
int compile()
{
int i;
printf("start compilation ");
initSource(); /*getSource*/
token = nextToken(); /**/
blockBegin(FIRSTADDR); /**/
block(0); /*0 */
finalSource();
i = errorN(); /**/
if (i!=0)
printf("%d errors ", i);
/* listCode(); */ /**/
return i /**/
}
void block(int pIndex) /*pIndex */
{
int backP;
backP = genCodeV(jmp, 0); /**/
while (1) { /**/
switch (token.kind){
case Const: /**/
token = nextToken();
constDecl(); continue;
case Var: /**/
token = nextToken();
varDecl(); continue;
case Func: /**/
token = nextToken();
funcDecl(); continue;
default: /**/
break;
}
break;
}
backPatch(backP); /**/
changeV(pIndex, nextCode()); /**/
genCodeV(ict, frameL()); /**/
statement(); /**/
genCodeR(); /**/
blockEnd(); /*able*/
}
void constDecl() /**/
{
Token temp;
while(1){
if (token.kind==Id){
setIdKind(constId); /**/
temp = token; /**/
token = checkGet(nextToken(), Equal); /*"="*/
if (token.kind==Num)
enterTconst(temp.u.id, token.u.value); /**/
else
errorType("number");
token = nextToken();
}else
errorMissingId();
if (token.kind!=Comma){ /**/
if (token.kind==Id){ /**/
errorInsert(Comma);
continue;
}else
break;
}
token = nextToken();
}
token = checkGet(token, Semicolon); /*";"*/
}
void varDecl() /**/
{
while(1){
if (token.kind==Id){
setIdKind(varId); /**/
enterTvar(token.u.id); /*table*/
token = nextToken();
}else
errorMissingId();
if (token.kind!=Comma){ /**/
if (token.kind==Id){ /**/
errorInsert(Comma);
continue;
}else
break;
}
token = nextToken();
}
token = checkGet(token, Semicolon); /*";"*/
}
void funcDecl() /**/
{
int fIndex;
if (token.kind==Id){
setIdKind(funcId); /**/
fIndex = enterTfunc(token.u.id, nextCode()); /**/
/*nextCode()*/
token = checkGet(nextToken(), Lparen);
blockBegin(FIRSTADDR); /**/
while(1){
if (token.kind==Id){ /**/
setIdKind(parId); /**/
enterTpar(token.u.id); /**/
token = nextToken();
}else
break;
if (token.kind!=Comma){ /**/
if (token.kind==Id){ /**/
errorInsert(Comma);
continue;
}else
break;
}
token = nextToken();
}
token = checkGet(token, Rparen); /*")"*/
endpar(); /**/
if (token.kind==Semicolon){
errorDelete();
token = nextToken();
}
block(fIndex); /**/
token = checkGet(token, Semicolon); /*";"*/
} else
errorMissingId(); /**/
}
void statement() /**/
{
int tIndex;
KindT k;
int backP, backP2; /**/
while(1) {
switch (token.kind) {
case Id: /**/
tIndex = searchT(token.u.id, varId); /**/
setIdKind(k=kindT(tIndex)); /**/
if (k != varId && k != parId) /**/
errorType("var/par");
token = checkGet(nextToken(), Assign); /*":="*/
expression(); /**/
genCodeT(sto, tIndex); /**/
return;
case If: /*if*/
token = nextToken();
condition(); /**/
token = checkGet(token, Then); /*"then"*/
backP = genCodeV(jpc, 0); /*jpc*/
statement(); /**/
backPatch(backP); /*jpc*/
return;
case Ret: /*return*/
token = nextToken();
expression(); /**/
genCodeR(); /*ret*/
return;
case Begin: /*begin . . end*/
token = nextToken();
while(1){
statement(); /**/
while(1){
if (token.kind==Semicolon){ /*";"*/
token = nextToken();
break;
}
if (token.kind==End){ /*nd*/
token = nextToken();
return;
}
if (isStBeginKey(token)){ /**/
errorInsert(Semicolon); /*";"*/
break;
}
errorDelete(); /**/
token = nextToken();
}
}
case While: /*while*/
token = nextToken();
backP2 = nextCode(); /*whilejmp*/
condition(); /**/
token = checkGet(token, Do); /*"do"*/
backP = genCodeV(jpc, 0); /*pc*/
statement(); /**/
genCodeV(jmp, backP2); /*while*/
backPatch(backP); /*pc*/
return;
case Write: /*write*/
token = nextToken();
expression(); /**/
genCodeO(wrt); /*rt*/
return;
case WriteLn: /*writeln*/
token = nextToken();
genCodeO(wrl); /*rl*/
return;
case End: case Semicolon: /**/
return;
default: /**/
errorDelete(); /**/
token = nextToken();
continue;
}
}
}
int isStBeginKey(Token t) /*t*/
{
switch (t.kind){
case If: case Begin: case Ret:
case While: case Write: case WriteLn:
return 1;
default:
return 0;
}
}
void expression() /**/
{
KeyId k;
k = token.kind;
if (k==Plus || k==Minus){
token = nextToken();
term();
if (k==Minus)
genCodeO(neg);
}else
term();
k = token.kind;
while (k==Plus || k==Minus){
token = nextToken();
term();
if (k==Minus)
genCodeO(sub);
else
genCodeO(add);
k = token.kind;
}
}
void term() /**/
{
KeyId k;
factor();
k = token.kind;
while (k==Mult || k==Div){
token = nextToken();
factor();
if (k==Mult)
genCodeO(mul);
else
genCodeO(div);
k = token.kind;
}
}
void factor() /**/
{
int tIndex, i;
KeyId k;
if (token.kind==Id){
tIndex = searchT(token.u.id, varId);
setIdKind(k=kindT(tIndex)); /**/
switch (k) {
case varId: case parId: /**/
genCodeT(lod, tIndex);
token = nextToken(); break;
case constId: /**/
genCodeV(lit, val(tIndex));
token = nextToken(); break;
case funcId: /**/
token = nextToken();
if (token.kind==Lparen){
i=0; /*i*/
token = nextToken();
if (token.kind != Rparen) {
for (; ; ) {
expression(); i++; /**/
if (token.kind==Comma){ /* */
token = nextToken();
continue;
}
token = checkGet(token, Rparen);
break;
}
} else
token = nextToken();
if (pars(tIndex) != i)
errorMessage("\\#par"); /*pars(tIndex)*/
}else{
errorInsert(Lparen);
errorInsert(Rparen);
}
genCodeT(cal, tIndex); /*call*/
break;
}
}else if (token.kind==Num){ /**/
genCodeV(lit, token.u.value);
token = nextToken();
}else if (token.kind==Lparen){ /*()*/
token = nextToken();
expression();
token = checkGet(token, Rparen);
}
switch (token.kind){ /**/
case Id: case Num: case Lparen:
errorMissingOp();
factor();
default:
return;
}
}
void condition() /**/
{
KeyId k;
if (token.kind==Odd){
token = nextToken();
expression();
genCodeO(odd);
}else{
expression();
k = token.kind;
switch(k){
case Equal: case Lss: case Gtr:
case NotEq: case LssEq: case GtrEq:
break;
default:
errorType("rel-op");
break;
}
token = nextToken();
expression();
switch(k){
case Equal: genCodeO(eq); break;
case Lss: genCodeO(ls); break;
case Gtr: genCodeO(gr); break;
case NotEq: genCodeO(neq); break;
case LssEq: genCodeO(lseq); break;
case GtrEq: genCodeO(greq); break;
}
}
}
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